Coder Social home page Coder Social logo

haiwei-lee / vexana Goto Github PK

View Code? Open in Web Editor NEW

This project forked from vb10/vexana

0.0 0.0 0.0 595 KB

Vexana is network manager project with dio.

License: MIT License

Ruby 3.17% Objective-C 0.09% Kotlin 0.29% Dart 92.00% Swift 0.94% HTML 3.51%

vexana's Introduction

vexana

Vexana is easy to use network proccess with dio. You can do dynamic model parsing, base error model, timeout and many utility functions.

Vexana-Game

Getting Started ๐Ÿ”ฅ

Let's talk about usage details.

Network Manager ๐Ÿ˜Ž

Have a lot of options: baseurl, logger, interceptors, base model etc.

INetworkManager  networkManager = NetworkManager(isEnableLogger: true, errorModel: UserErrorModel(),
 options: BaseOptions(baseUrl: "https://jsonplaceholder.typicode.com/"));

Model Parse โš”๏ธ

First, you have to provide the parse model, then the result model. (Result model could be a list, model or primitive)

final response =
await networkManager.send<Todo, List<Todo>>("/todos", parseModel: Todo(), method: RequestType.GET);

Base Headers ๐Ÿ“

You could be add key values to your every request directly.(add authhentication key)

networkManager.addBaseHeader(MapEntry(HttpHeaders.authorizationHeader, response.data?.first.title ?? ''));
// Clear singlhe header
networkManager.removeHeader('\${response.data?.first.id}');
// Clear all hader
networkManager.clearHeader();

Download File ๐Ÿ“

You can download any file format like pdf, png or etc with progress indicator.

final response = await networkManager.downloadFileSimple('http://www.africau.edu/images/default/sample.pdf', (count, total) {
      print('${count}');
});

//Example: Image.memory(response.data)

HTTP Post Request with Request Body ๐Ÿš€

The model found in the request body must extend the abstract class INetworkModel, as follows.

class TodoPostRequestData extends INetworkModel<TodoPostRequestData>

Then, since the model will have toJson and fromJson properties, you can create the object and pass it directly to the send method.

So, it is sufficient to send only the request body object into the send method. You don't need to use toJson.

final todoPostRequestBody = TodoPostRequestData();
final response =
await networkManager.send<Todo, List<Todo>>("/todosPost", parseModel: Todo(), method: RequestType.POST, data: todoPostRequestBody);

Cancel Request โŒ

You can implement cancel token when need to invoke your request during to complete.

  final cancelToken = CancelToken();
    networkManager
        .send<ReqResModel, ReqResModel>('/users?delay=5',
            parseModel: ReqResModel(), method: RequestType.GET, canceltoken: cancelToken)
        .catchError((err) {
      if (CancelToken.isCancel(err)) {
        print('Request canceled! ' + err.message);
      }
    });

    cancelToken.cancel('canceled');

    await Future.delayed(const Duration(seconds: 8));

Primitive Request ๐ŸŒผ

Sometimes we need to parse only primitive types for instance List, String, int etc. You can use this method.

//
[
  "en",
  "tr",
  "fr"
]
//
networkManager.sendPrimitive<List>("languages");

Network Model ๐Ÿ›’

You must wrap your model with INetworkModel so that, we understand model has toJson and fromJson methods.

class Todo extends INetworkModel<Todo>

Refresh Token โ™ป๏ธ

Many projects use authentication structure for mobile security (like a jwt). It could need to renew an older token when the token expires. This time provided a refresh token option, we can lock all requests until the token refresh process is complete.

Since i locked all requests, I am giving a new service instance.

INetworkManager  networkManager = NetworkManager(isEnableLogger: true, options: BaseOptions(baseUrl: "https://jsonplaceholder.typicode.com/"),
onRefreshFail: () {  //Navigate to login },
 onRefreshToken: (error, newService) async {
    <!-- Write your refresh token business -->
    <!-- Then update error.req.headers to new token -->
    return error;
});

Caching ๐Ÿงฒ

You need to write a response model in the mobile device cache sometimes. It's here now. You can say expiration date and lay back ๐Ÿ™

    await networkManager.send<Todo, List<Todo>>("/todos",
        parseModel: Todo(),
        expiration: Duration(seconds: 3),
        method: RequestType.GET);

You must declare a caching type. It has FileCache and SharedCache options now. NetworkManager(fileManager: LocalFile()); If you want more implementation details about the cache, you should read this article

Tasks


  • Example project
  • Unit Test with json place holder
  • Unit Test with custom api
  • Make a unit test all layers.
  • Cache Option
    • SQlite Support
    • Web Cache Support
  • Refresh Token Architecture
  • Usage Utility

License

License

2020 created for @VB10

Youtube Channel


Youtube

vexana's People

Contributors

vb10 avatar ahm3tcelik avatar alperen23230 avatar mfurkanyuceal avatar aydemiromer avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.