Created
March 21, 2022 06:41
-
-
Save dev4jam/d57f7d650da767d4afb27e2d956c269c to your computer and use it in GitHub Desktop.
Expected API call
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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