Skip to content

Instantly share code, notes, and snippets.

@dev4jam
Created March 21, 2022 06:41
Show Gist options
  • Save dev4jam/d57f7d650da767d4afb27e2d956c269c to your computer and use it in GitHub Desktop.
Save dev4jam/d57f7d650da767d4afb27e2d956c269c to your computer and use it in GitHub Desktop.
Expected API call
//Instantiate a service and keep it in your DI container:
final service = NetworkService(baseUrl: 'https://www.domain.com/api');
// Prepare a request:
final request = NetworkRequest(
type: NetworkRequestType.POST,
path: '/authenticate',
data: NetworkRequestBody.json({
'login': 'user_name',
'password': 'password'
}),
);
// Execute a request and convert response to your model:
final response = await service.execute(
request,
AccessTokenResponse.fromJson, // <- Function to convert API response to your model
);
// Handle possible outcomes:
response.maybeWhen(
ok: (authResponse) {
// Save access token or proceed with other parts of your app
},
badRequest: (info) {
// Handle specific error
},
orElse: () {
// Handle generic error
}
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment