Request Api Helper
one code, one style for all restapi;
Installation
Add this to your package's pubspec.yaml file:
dependencies:
request_api_helper:
Usage
import 'package:request_api_helper/request.dart' as req;
New Feature
onUploadProgress :
import 'package:request_api_helper/request.dart' as req;
import 'package:request_api_helper/request_api_helper.dart' show RESTAPI; // use show [class] for selection
upload() async {
await req.send(
name: 'upload',
type: RESTAPI.POST,
data: RequestData(
body: {},
file: RequestFileData(
filePath: [test.path], // list of path
fileRequestName: ['test'], // list of name request
),
),
changeConfig: RequestApiHelperConfigData(
onSuccess: (response) async {
// important
await Session.save('token', 'Bearer ' + response['token']);
},
),
onUploadProgress: (sended, total) {
// always update in every upload size
print(sended);
print(total);
setState((){});
}
);
}
1 . Setting Config
use static Env in main like this (Release):
import 'package:request_api_helper/model/config_model.dart';
void main(
RequestApiHelperConfig.save(
RequestApiHelperConfigData(
url: 'https://official-joke-api.appspot.com/api/',
noapiurl: 'https://official-joke-api.appspot.com/',
logResponse: true,
withLoading: Redirects(toogle: false),
),
);
runApp(MyApp());
}
2 . Make Login And Saving Token
First you must make login request.
login() async {
======================= GET DATA FROM SERVER
await req.send(
name: 'login',
type: RESTAPI.POST,
data: RequestData(
body: {
'username' : username,
'password' : password,
}
),
changeConfig: RequestApiHelperConfigData(
onSuccess: (response) async {
// important
await Session.save('token', 'Bearer ' + response['token']);
// auto save different type data
await Session.save('user_name', response['user']['name']);
await Session.save('user_id', response['user']['id']);
},
),
);
}
other request
login() async {
======================= GET DATA FROM SERVER
await req.send(
name: 'other/name',
type: RESTAPI.POST,
data: RequestData(),
changeConfig: RequestApiHelperConfigData(
onSuccess: (response) async {
// other function after success
),
);
}
3 . What is Session?
session is Shared preferences plugin, implement :
save to Shared Preferences
await Session.save('myname','MIAUW'); // String
await Session.save('myint',100); // int
await Session.save('mybool',false); // bool
load from Shared Preferences
String mystring = await Session.load('myname'); // String
int myint = await Session.load('myint'); // int
bool mybool = await Session.load('mybool'); // bool
Libraries
- background
- this class used For show Dialog or Loading,
Redirects class have 2 attributes
toogle is
true
orfalse
widget for custom Widget you want you can implement like : - base_request
- this class is core of the Request timeout,
exception
,socket
error handler you can use Directly : - model/config_model
- model/error_list
- model/redirect_helper
- request
- used for main request function
- request_api_helper
- response
- rest_api
- core of process request
- server_switcher
- this is a Widget for change server default Widget used : Icon(Icons.setting)
- model/server_switcher_model
- session
- Session is SharedPreferences you can call directly or use this stucture class
- timeout