request_api_helper 2.2.1+1 copy "request_api_helper: ^2.2.1+1" to clipboard
request_api_helper: ^2.2.1+1 copied to clipboard

outdated

http, shared_preferences helper for Lavarel and other.

Request Api Helper #

Post, Get, Save, Helper for Flutter;

New Updates #

  Env(
    timeout: 10,
    errorMessage: 'Call Programmer',
    successMessage: 'default',
    exception: true,
    beforeSend: ()=>print('hi this is me')
  ).saveConfiguration();
  
  await Post / Get {
    ...
    beforeSend: ()=> print('loading...'), // before send
    onTimeout: ()=> print('this is timeout'), // if timeout Exception
    onSocket: ()=> print('internet not connected'), // if socket Exception
    onException: (val)=> print(val), // if Exception with Code
    timeout: 20000, // default 10000 ms
    onComplete : (v) => print(v), // return Raw HTTP
    socketMessage : 'Error Connection', // return toast
    timeoutMessage : 'Error Connection', // return toast
    socketRedirect : true , // redirect to ...
    timeoutRedirect : true , // redirect to ...
  }.request(context)

  await Session.save('myname','hunter');
  String name = await Session.load('myname');

Installation #

Add this to your package's pubspec.yaml file:

dependencies:
  request_api_helper:

Usage #

import 'package:request_api_helper/request_api_helper.dart';

1 . Setting Env #

use static Env in main like this (Release):

// == 200 response dynamic
// != 200 response {'statusCode' : other int code}

void main(
  Env(
    confurl: 'http://rootUrl/api/',
    confnoapiurl: 'http://rootUrl/'
  ).save();
  runApp(MyApp());
}

or use Server Switcher (Team Developing)


ServerSwitcher(
 servers: [
   {'name' : 'Server 1' , 'id' : 'http://rootUrl/'},
   {'name' : 'Server 2' , 'id' : 'http://rootUrl2/'},
 ]
)

2 . Make Login And Saving Token #

First you must make login request.

login() async {
======================= GET DATA FROM SERVER

// == 200 response dynamic
// != 200 response {'statusCode' : other int code}
dynamic response = await Post( 
 name: 'login', // full url http://rootUrl/api/login
 exception : true, // error exception, false to hide
 successMessage : 'default',
 errorMessage : 'Try Again!',
 logResponse : false, // showing server response
 timeout : 20000, 
 timeoutRedirect : true // if true .request(context)
 socketRedirect : true // if true .request(context)
).request(context); 

// note : 

// successMessage : 'default' auto show message from server [200]
// { 'message' : 'Thanks For Buy' }

// errorMessage : 'default' auto show message from server [!=200]
// { 'message' : 'Error You Must Login' }

// timeout : CustomTimeout() // for routing to your custom view

======================= Process Data
// ex : response is 
// {'token' : 'mytoken' , 'user' : {'name' : 'MIAUW' , 'id' : 1}}

if(response != null){
  if(response['statusCode'] == null){

    // important
    await Session().save('access_token', response['token']);
    await Session().save('token_type', 'Bearer');

    // auto save different type data
    await Session().save('user_name',response['user']['name']);
    await Session().save('user_id',response['user']['id']);
  }
}

}

3 . Use Post / Get After Saving Token #

Post

Map<String,dynamic> mydata = await Post( 
 name: 'data/employee', // full url http://rootUrl/api/data/employee
 exception : true,
 logResponse : true,
 timeout : 20000, 
 timeoutRedirect : true,
 socketRedirect : true 
).request(context); 

if(mydata != null){
 if(mydata['statusCode'] == null){
  // success return
 }{
  // error return
 }
 // no response
}

Get

Map<String,dynamic> mydata = await Get( 
 name: 'data/employee', // full url http://rootUrl/api/data/employee
 exception : true,
 logResponse : true,
 timeout : 20000, 
 timeoutRedirect : true,
 socketRedirect : true 
 body : {
  'mydata' : 'yourdata'
 },
).request(context); 

if(mydata != null){
 if(mydata['statusCode'] == null){
  // success return
 }{
  // error return
 }
 // no response
}

4 . Use Post / Get other Url? #

Map<String,dynamic> mydata = await Post( 
 name: 'image/myimage', // full url http://google.com/api/image/myimage
 customUrl : 'http://google.com/api/',
 customHeader : {
   'your header' : 'this header',
 },
 body : {
   'mybody' : 'myvalue',
 }
 exception : true,
 logResponse : true,
 timeout : true, 
).request(context); 

File Upload #

  Post {
    ...
    ...
    ...
    file : file, // String path of file
    fileRequestName : 'images' // request name of file
  }.request()

5 . 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

6
likes
0
pub points
25%
popularity

Publisher

unverified uploader

http, shared_preferences helper for Lavarel and other.

Repository (GitHub)
View/report issues

License

unknown (LICENSE)

Dependencies

flutter, fluttertoast, http, http_parser, shared_preferences

More

Packages that depend on request_api_helper