eticon_api 1.2.0-beta copy "eticon_api: ^1.2.0-beta" to clipboard
eticon_api: ^1.2.0-beta copied to clipboard

outdated

This package will help you quickly and easily work with HTTP REQUESTS. All you need to do is select one of the 4 required requests: GET, POST, DELETE, PUT

English

ETICON API #

Package for working with http requests.

Initialization #

First you need to initialize:

void main(){
  Api.init(baseUrl: 'https://example.com/');
  runApp(MyApp());
}

Methods #

Parameters Request type Value
isAuth ALL If the value is true, the request will be authorized
method ALL Accepts a string appended to baseURL
body POST, PUT Accepts request body in Map
query GET, DELETE Accepts query in Map
testMode ALL if true shows detailed information about the request

GET #

Future<void> getRequest() async {
    try{
      Map<String, dynamic> response = await Api.get(method: 'product', query: {"id": 5});
    } on APIException catch(error){
      print('ERROR CODE: ${error.code}');
    }
  }

POST #

Future<void> postRequest() async {
    try{
      Map<String, dynamic> response = await Api.post(method: 'product', body: {"id": 5});
    } on APIException catch(error){
      print('ERROR CODE: ${error.code}');
    }
  }

DELETE #

Future<void> deleteRequest() async {
    try{
      Map<String, dynamic> response = await Api.delete(method: 'product', query: {"id": 5}, isAuth: true);
    } on APIException catch(error){
      print('ERROR CODE: ${error.code}');
    }
  }

PUT #

Future<void> putRequest() async {
    try{
      Map<String, dynamic> response = await Api.put(method: 'product', body: {"id": 5}, isAuth: true);
    } on APIException catch(error){
      print('ERROR CODE: ${error.code}');
    }
  }

HTTP status codes #

If the result of the status code in the response is not 200, then APIException will be thrown. It contains the status code as well as the response body. In case there are problems with the Internet connection, APIException will return code 0, if there is no Internet connection. Code 1, for other connection errors.

Headers #

To declare headers, you must use the method:

  Api.setHeaders({"Content-type": 'application/json'});

If no headers are set then the default is Content-type: application / json

Note!!! that the Authorization header is added automatically on an authorized request.

Authorization #

For authorized requests, you need to set the token value. The set value will be written to the device memory.

  await Api.setToken('{your_token}');

Get a token:

  Api.token;

When the application starts, you can unload the token recorded in the device's memory:

void main() async {
  bool tokenLoaded = await Api.loadTokenFromMemory();
  if(tokenLoaded){
    print(Api.token);
  }
  runApp(MyApp());
}

If you do not use the Bearer type in the token, then disable it:

void main() async {
  Api.init(baseUrl: 'https://example.com/', bearerToken: false);
  runApp(MyApp());
}

Test Mode #

Test Mode is a handy tool for developing an application that shows complete information about the request (parameters, full URL, response body, etc.). In addition, this feature disables all error handlers. Test mode can be set globally for all requests in the project:

void main() async {
  Api.init(baseUrl: 'https://example.com/', globalTestMode: true);
  runApp(MyApp());
}

And on a separate request:

Future<void> getRequest() async {
    try{
      Map<String, dynamic> response = await Api.get(method: 'product', isAuth: true, testMode: true);
    } on APIException catch(error){
      print('ERROR CODE: ${error.code}');
    }
  }

To disable test mode in the whole project, you can use disableAllTestMode:

void main() async {
  Api.init(
    baseUrl: 'https://example.com/', 
    globalTestMode: true, // Will be ignored
    disableAllTestMode: true,
  );
  runApp(MyApp());
}

Decoding UTF-8 #

There is built-in support for decoding in response to utf-8

void main() async {
  Api.init(
    baseUrl: 'https://example.com/', 
    ebableUtf8Decoding: true,
  );
  runApp(MyApp());
}

Getting Started #

This project is a starting point for a Dart package, a library module containing code that can be shared easily across multiple Flutter or Dart projects.

For help getting started with Flutter, view our online documentation, which offers tutorials, samples, guidance on mobile development, and a full API reference.

11
likes
0
pub points
62%
popularity

Publisher

verified publishereticon.ru

This package will help you quickly and easily work with HTTP REQUESTS. All you need to do is select one of the 4 required requests: GET, POST, DELETE, PUT

Repository (GitHub)
View/report issues

License

unknown (LICENSE)

Dependencies

flutter, get_storage, http, shared_preferences

More

Packages that depend on eticon_api