ConnectionManager<E extends Decodable> class
Class to manage API and network calls. It can be instantiated as a singleton to use a single instance of it all through the app.
- Inheritance
-
- Object
- BaseConnectionManager<
E> - ConnectionManager
Constructors
-
ConnectionManager({required String baseUrl, required Map<
String, String> constantHeaders, E decodeErrorFromMap(int statusCode, Map<String, dynamic> data)?, int? mapStatusCodeFromResponse(Map<String, dynamic> ? data)?, bool onTokenExpiredRuleOverride(Response response)?, Future<String?> onTokenExpired()?, void onResponseReceived(Response response)?, bool returnCatchedErrorMessage = true, Duration timeout = const Duration(minutes: 1), bool persistCookies = false, Client? client}) - Initialise a ConnectionManager to manage API and network calls.
Properties
- baseUrl ↔ String
-
The base url for all the API calls
getter/setter pairinherited
- client → Client?
-
If set, overrides the default http client for API calls
finalinherited
-
constantHeaders
→ Map<
String, String> -
Headers for the API requests that are not supposed to change for different API calls.
finalinherited
-
decodeErrorFromMap
→ E Function(int statusCode, Map<
String, dynamic> data)? -
A function to decode an error body received from an API call can be eventually provided.
The
E
type must extend Decodable.finalinherited - hashCode → int
-
The hash code for this object.
no setterinherited
-
headers
→ Map<
String, String> -
The headers used for the API call
no setterinherited
-
mapStatusCodeFromResponse
→ int? Function(Map<
String, dynamic> ? data)? -
By default, the response of
doApiRequest
method has the status code retrieved by the http header. Sometimes, this status code is overridden by a code in the response body. You can use this method to map that code from body and use it to override the http status code.finalinherited - onResponseReceived → void Function(Response response)?
-
A function fired, if not null, when the
doApiRequest
method receives a response from the BE. This can be useful to manage broadly ahttp.Response
the same way for every api call.finalinherited -
onTokenExpired
→ Future<
String?> Function()? -
A function fired when the http client gives a 401 response after an API call.
It is used to refresh the auth token, if set, and after returning the new token
the ConnectionManager will attempt the API call once again.
finalinherited
- onTokenExpiredRuleOverride → bool Function(Response response)?
-
This method can be used in combination with
onTokenExpired
to define a custom rule to trigger theonTokenExpired
method. By default,onTokenExpired
is fired when the http response has a 401 status code. Eventually, this rule can be expanded thanks to this method.finalinherited - persistCookies → bool
-
If true, creates a persistent instance of a cookie manager to be used for
all the API calls done with this ConnectionManager. Defaluts to false.
finalinherited
- returnCatchedErrorMessage → bool
-
Specify if the error message coming from the try-catch block in
doApiRequest
should be returned in the response (i.e. decoding errors). Default to true.finalinherited - runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
- timeout → Duration
-
Specify the timeout for all the API calls done with this ConnectionManager.
Defaults to 1 minute.
finalinherited
Methods
-
changeBaseUrl(
String newBaseUrl) → void -
Call this method to change the base url of the ConnectionManager.
inherited
-
doApiRequest<
T extends Decodable> ({ApiRequestType requestType = ApiRequestType.get, required String endpoint, ApiBodyType bodyType = ApiBodyType.json, Map< String, String> ? headers, Map<String, String> ? query, Object? body, T decodeContentFromMap(Map<String, dynamic> data)?, dynamic filterMapResponseToDecodeContent(Map<String, dynamic> data)?, E decodeErrorFromMapOverride(int statusCode, Map<String, dynamic> data)?, bool unescapeHtmlCodes = false, bool tryRefreshToken = true, bool useUtf8Decoding = false, Duration? timeout, bool? persistCookies, void uploadPercentage(int)?, bool validateStatus(int)?, void downloadProgress(int, int, int)?, CancelToken? cancelToken}) → Future<APIResponse< T, E> > -
This method is used to generate an API request. It returns a Future to await
with a APIResponse with some useful attributes from http response.
inherited
-
getResponse(
{required ApiRequestType requestType, required String url, required Map< String, String> headersForApiRequest, ApiBodyType bodyType = ApiBodyType.json, Object? body, required Duration timeout, Map<String, String> ? query, required bool persistCookies, void uploadPercentage(int)?, bool validateStatus(int)?, void downloadProgress(int, int, int)?, required Client httpClient, CancelToken? cancelToken}) → Future<Response> -
override
-
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
removeAuthHeader(
) → void -
Deletes the auth header from headers
inherited
-
setAuthHeader(
String token) → void -
Call this method to add or reset an Authorization header after creating the ConnectionManager.
inherited
-
Call this method to add new headers to the Connection Manager to be used
for all the API calls toghether with
constantHeaders
passed to the constructor.inherited -
toString(
) → String -
A string representation of this object.
inherited
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited