gits_http

Last updated:

0 purchases

gits_http Image
gits_http Images
Add to Cart

Description:

gits http

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

locator.registerLazySingleton(
() => GitsHttp(
timeout: 30000,
showLog: true,
gitsInspector: locator(),
authTokenOption: AuthTokenOption(
typeHeader: 'Authorization',
prefixHeader: 'Bearer',
getToken: () => locator<FlutterSecureStorage>().read(key: 'token'),
authCondition: (request, response) =>
request.url == GitsEndpoints.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 == GitsEndpoints.logout,
onClearToken: () =>
locator<FlutterSecureStorage>().delete(key: 'token'),
excludeEndpointUsageToken: [
GitsEndpoints.login,
GitsEndpoints.register,
],
),
refreshTokenOption: RefreshTokenOption(
method: RefreshTokenMethod.post,
url: GitsEndpoints.refreshToken,
condition: (request, response) =>
request.url != GitsEndpoints.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 gits_inspector and put in locator.
locator.registerLazySingleton(
() => GitsInspector(
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 GitsHttp.
GitsHttp(
...
authTokenOption:AuthTokenOption(
typeHeader: 'Authorization',
prefixHeader: 'Bearer',
getToken: () => locator<FlutterSecureStorage>().read(key: 'token'),
authCondition: (request, response) =>
request.url == GitsEndpoints.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 == GitsEndpoints.logout,
onClearToken: () =>
locator<FlutterSecureStorage>().delete(key: 'token'),
excludeEndpointUsageToken: [
GitsEndpoints.login,
GitsEndpoints.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 GitsHttp.
GitsHttp(
...
refreshTokenOption: RefreshTokenOption(
method: RefreshTokenMethod.post,
url: GitsEndpoints.refreshToken,
condition: (request, response) =>
request.url != GitsEndpoints.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 GitsHttp http = locator();

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

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

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

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

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

final response = await http.postMultipart(Uri.parse('https://api.gits.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.