-
Notifications
You must be signed in to change notification settings - Fork 7
/
main.dart
152 lines (126 loc) · 4.54 KB
/
main.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
import 'dart:developer';
import 'package:flutter/material.dart';
import 'package:rest_api_client/rest_api_client.dart';
Future main() async {
WidgetsFlutterBinding.ensureInitialized();
//This must be called once per application lifetime
await RestApiClient.initFlutter();
RestApiClient restApiClient = RestApiClientImpl(
options: RestApiClientOptions(
//Defines your base API url eg. https://mybestrestapi.com
baseUrl: 'https://gorest.co.in/public/v2/',
//Enable caching of response data
cacheEnabled: true,
),
authOptions: AuthOptions(
//Define refresh token endpoint for RestApiClient
//instance to use the first time response status code is 401
refreshTokenEndpoint: '/api/auth/refresh-token',
//Define the name of your api parameter name
//on RefreshToken endpoint eg. 'refreshToken' or 'value' ...
refreshTokenParameterName: 'token',
refreshTokenBodyBuilder: (jwt, refreshToken) => {
'accessToken': jwt,
'refreshToken': refreshToken,
},
refreshTokenHeadersBuilder: (jwt, _) => {
RestApiClientKeys.authorization: 'Bearer $jwt',
},
//This method is called on successfull call to refreshTokenEndpoint
//Provides a way to get a jwt from response, much like
//resolveValidationErrorsMap callback
resolveJwt: (response) => response.data['accessToken'],
//Much like resolveJwt, this method is used to resolve
//refresh token from response
resolveRefreshToken: (response) => response.data['refreshToken'],
),
loggingOptions: LoggingOptions(
//Toggle logging of your requests and responses
//to the console while debugging
logNetworkTraffic: true,
),
exceptionOptions: ExceptionOptions(
resolveValidationErrorsMap: (response) {
if (response != null &&
response.data != null &&
response.data['code'] != null) {
return {
'ERROR': [response.data['code']],
};
}
return {};
},
),
cacheOptions: CacheOptions(
useAuthorization: true,
cacheLifetimeDuration: const Duration(days: 10),
resetOnRestart: false,
),
interceptors: [
InterceptorsWrapper(
onRequest: (options, handler) {
log('Logging before request');
return handler.next(options);
},
onResponse: (response, handler) {
log('Logging on response');
return handler.next(response);
},
onError: (DioException e, handler) {
log('Logging on error');
return handler.next(e);
},
),
],
);
//init must be called, preferably right after the instantiation
await restApiClient.init();
//Use restApiClient from this point on
//If you are using authentication in you app
//probably it would look like this
final response = await restApiClient.post(
'/Authentication/Authenticate',
data: {'username': 'john', 'password': 'Flutter_is_awesome1!'},
);
//Extract the values from response
var jwt = response.data['jwt'];
var refreshToken = response.data['refreshToken'];
//Let's asume that somehow we got jwt and refresh token
//Probably pinged our api Authentication endpoint to get these two values
jwt =
'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwiZmx1dHRlciI6IkZsdXR0ZXIgaXMgYXdlc29tZSIsImNoYWxsZW5nZSI6IllvdSBtYWRlIGl0LCB5b3UgY3JhY2tlZCB0aGUgY29kZS4gWW91J3JlIGF3ZXNvbWUgdG9vLiIsImlhdCI6MTUxNjIzOTAyMn0.5QJz8hhxYsHxShS4hWKdHzcFH_IsQQZAnWSEcHJkspE';
refreshToken = 'c91c03ea6c46a86cbc019be3d71d0a1a';
//set the authorization
restApiClient.authorize(jwt: jwt, refreshToken: refreshToken);
//Create authorized requests safely
restApiClient.get('/Products');
//Ignore server errors that might happen in the next request
restApiClient.exceptionHandler.exceptionOptions.showInternalServerErrors =
false;
try {
restApiClient.get(
'/Products',
queryParameters: {'name': 'darts'},
);
} catch (e) {
print(e);
}
//Ignore all exceptions that might happen in the next request
restApiClient.exceptionHandler.exceptionOptions.disable();
restApiClient.post(
'/Products/Reviews/234',
data: {
'grade': 5,
'comment':
'Throwing dart is not safe but upgrading to Dart 2.12.1 is. #nullsafety'
},
);
restApiClient.put(
'/Products/Reviews/234',
data: {
'grade': 5,
'comment': 'On the other hand throwing dartz is fun',
},
);
restApiClient.delete('/Products/Reviews/234');
}