eticon_api 1.2.2
eticon_api: ^1.2.2 copied to clipboard

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() 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}');
    }
  }

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.

  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 {
  await Api.init(baseUrl: 'https://example.com/');
  bool tokenLoaded = 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 {
  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.

5
likes
120
pub points
62%
popularity

Publisher

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

Documentation

API reference

License

MIT (LICENSE)

Dependencies

flutter, get_storage, http

More

Packages that depend on eticon_api