English

ETICON API

Package for working with http requests.

Initialization

First you need to initialize:

void main() async {
  await 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}');
    }
  }

Raw Methods

All raw methods take the url to be requested. In case of GET and DELETE without parameters. They are specified separately as in the methods described above.

Future<void> request() async {
    try{
      Map<String, dynamic> response = await Api.rawGet(url: 'https://example.com/profile', query: {"id": 5}, headers: {"Content-type": 'application/json'});
    } on APIException catch(error){
      print('ERROR CODE: ${error.code}');
    }
  }
Future<void> request() async {
    try{
      Map<String, dynamic> response = await Api.rawPost(url: 'https://example.com/profile', body: {"id": 5}, headers: {"Content-type": 'application/json'});
    } on APIException catch(error){
      print('ERROR CODE: ${error.code}');
    }
  }
Future<void> request() async {
  try{
    Map<String, dynamic> response = await Api.rawDelete(url: 'https://example.com/profile', query: {"id": 5}, headers: {"Content-type": 'application/json'});
  } on APIException catch(error){
    print('ERROR CODE: ${error.code}');
  }
}
Future<void> request() async {
  try{
    Map<String, dynamic> response = await Api.rawPut(url: 'https://example.com/profile', body: {"id": 5}, headers: {"Content-type": 'application/json'});
  } 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.

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. When using Api.init (), the token will be automatically unloaded from the device memory. But you can disable loadTokenFromMemory:

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

Get a token:

  Api.token;

You can also check the token for emptiness or non-emptiness:

  Api.tokenIsEmpty;//return true or false
  Api.tokenIsNotEmpty;//return true or false

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

void main() async {
  await 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 {
  await 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 {
  await Api.init(
    baseUrl: 'https://example.com/', 
    globalTestMode: true, // Will be ignored
    disableAllTestMode: true,
  );
  runApp(MyApp());
}

Server storage url

It is often necessary to get various data from the server storage, for example, images. However, the base url and the url to the storage sometimes don't match or you have to write the url manually each time.

To set a storage url, it must be specified in the initialization:

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

Usage example:

Image.network(Api.dataFromStorage('image.png'));

Decoding UTF-8

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

void main() async {
  await 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

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