morpheme_http

Last updated:

0 purchases

morpheme_http Image
morpheme_http Images
Add to Cart

Description:

morpheme http

Morpheme HTTP #
Morpheme HTTP uses the http library which has been modified as needed. first we store MorphemeHttp into locator. locator is the service locator from get_it.
final locator = GetIt.instance;

locator.registerLazySingleton(
() => MorphemeHttp(
timeout: 30000,
showLog: true,
morphemeInspector: locator(),
authTokenOption: AuthTokenOption(
typeHeader: 'Authorization',
prefixHeader: 'Bearer',
getToken: () => locator<FlutterSecureStorage>().read(key: 'token'),
authCondition: (request, response) =>
request.url == MorphemeEndpoints.login,
onAuthTokenResponse: (response) async {
final map = jsonDecode(response.body);
await locator<FlutterSecureStorage>().write(
key: 'token',
value: map['token'],
);
await locator<FlutterSecureStorage>().write(
key: 'refresh_token',
value: map['refresh_token'],
);
},
clearCondition: (request, response) =>
request.url == MorphemeEndpoints.logout,
onClearToken: () =>
locator<FlutterSecureStorage>().delete(key: 'token'),
excludeEndpointUsageToken: [
MorphemeEndpoints.login,
MorphemeEndpoints.register,
],
),
refreshTokenOption: RefreshTokenOption(
method: RefreshTokenMethod.post,
url: MorphemeEndpoints.refreshToken,
condition: (request, response) =>
request.url != MorphemeEndpoints.login && response.statusCode == 401,
getBody: () async {
final refreshToken =
await locator<FlutterSecureStorage>().read(key: 'refresh_token');

return {
'refresh_token': refreshToken ?? '',
};
},
onResponse: (response) async {
// handle response refresh token
final map = jsonDecode(response.body);
locator<FlutterSecureStorage>().write(
key: 'token',
value: map['token'],
);
},
),
);
copied to clipboard
and to enable http inspector need to add dependency morpheme_inspector and put in locator.
locator.registerLazySingleton(
() => MorphemeInspector(
showNotification: true, // default true
showInspectorOnShake: true, // default true
saveInspectorToLocal: true, // default true
notificationIcon: '@mipmap/ic_launcher', // default '@mipmap/ic_launcher' just for android
),
);
copied to clipboard
Auth Token #
To set the token, it is done after authorization and getting the token. the token is stored to local and setup on MorphemeHttp.
MorphemeHttp(
...
authTokenOption:AuthTokenOption(
typeHeader: 'Authorization',
prefixHeader: 'Bearer',
getToken: () => locator<FlutterSecureStorage>().read(key: 'token'),
authCondition: (request, response) =>
request.url == MorphemeEndpoints.login,
onAuthTokenResponse: (response) async {
final map = jsonDecode(response.body);
await locator<FlutterSecureStorage>().write(
key: 'token',
value: map['token'],
);
await locator<FlutterSecureStorage>().write(
key: 'refresh_token',
value: map['refresh_token'],
);
},
clearCondition: (request, response) =>
request.url == MorphemeEndpoints.logout,
onClearToken: () =>
locator<FlutterSecureStorage>().delete(key: 'token'),
excludeEndpointUsageToken: [
MorphemeEndpoints.login,
MorphemeEndpoints.register,
],
),
...
);
copied to clipboard
After we set the token, every API call will add an Authorization header with a default value of Bearer $token.
Refresh Token #
To set the token, it is done after authorization and getting the token. the token is stored to local and setup on MorphemeHttp.
MorphemeHttp(
...
refreshTokenOption: RefreshTokenOption(
method: RefreshTokenMethod.post,
url: MorphemeEndpoints.refreshToken,
condition: (request, response) =>
request.url != MorphemeEndpoints.login && response.statusCode == 401,
getBody: () async {
final refreshToken =
await locator<FlutterSecureStorage>().read(key: 'refresh_token');

return {
'refresh_token': refreshToken ?? '',
};
},
onResponse: (response) async {
// handle response refresh token
final map = jsonDecode(response.body);
locator<FlutterSecureStorage>().write(
key: 'token',
value: map['token'],
);
},
),
...
);
copied to clipboard
Get #
final MorphemeHttp http = locator();

final response = await http.get(Uri.parse('https://api.morpheme.id'), body: body.toMap());
copied to clipboard
Post #
final MorphemeHttp http = locator();

final response = await http.post(Uri.parse('https://api.morpheme.id'), body: body.toMap());
copied to clipboard
Put #
final MorphemeHttp http = locator();

final response = await http.put(Uri.parse('https://api.morpheme.id'), body: body.toMap());
copied to clipboard
Patch #
final MorphemeHttp http = locator();

final response = await http.patch(Uri.parse('https://api.morpheme.id'), body: body.toMap());
copied to clipboard
Delete #
final MorphemeHttp http = locator();

final response = await http.delete(Uri.parse('https://api.morpheme.id'), body: body.toMap());
copied to clipboard
Post Multipart #
final MorphemeHttp http = locator();
final File file = getImage();

final response = await http.postMultipart(Uri.parse('https://api.morpheme.id'), files: {'image': file}, body: body.toMap());
copied to clipboard

License:

For personal and professional use. You cannot resell or redistribute these repositories in their original state.

Files In This Product:

Customer Reviews

There are no reviews.