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

ParametersRequest typeValue
isAuthALLIf the value is true, the request will be authorized
methodALLAccepts a string appended to baseURL
bodyPOST, PUTAccepts request body in Map
queryGET, DELETEAccepts query in Map
testModeALLif 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.

Libraries

eticon_api
Eticon API is a handy and simple package for handling http requests.