fireball 0.0.1 copy "fireball: ^0.0.1" to clipboard
fireball: ^0.0.1 copied to clipboard

discontinued
outdated

http power.

fireball #

Fireball 🔥 is an HTTP wrapper and helper library. You can easy to use HTTP requests and responses, and you have global configuration options.

Sample Using #

What kind of you model all you have todo set model in responseType and dynamic type.

 final response = await NetworkManager().get<Pet>("pet/1", responseType: Pet());
    expect(response is Pet, true);

How to use #

Config ⚒ #

We always need a base configuration for HTTP requests. You can initiation a project before starting.

    final config = NetworkConfig(baseUrl: "https://petstore.swagger.io/v2/");
    config.addHeader(KeyValue("SAMPLE", "ANY-VALUE-IN-ALL REQUEST"));
    config.setTimeout(Duration(seconds: 10));

Learning ⚔️ #

Service response if you need a custom scenario, you can do a learning class. Learning class has two method check error and check success, so it works HTTP request then complete.

class BaseError {
  final String data;
  BaseError(this.data);
}

class PetIOLearning extends NetworkLearning {
  @override
  checkCustomError(ErrorModel error) {
    switch (error.statusCode) {
      case HttpStatus.unauthorized:
        return BaseError("Unauthorized");
      case HttpStatus.notFound:
        return BaseError("Not Found");
      default:
    }
    return error.data;
  }

  @override
  checkSuccess<T>(ResponseModel response) {
    print(response.result);
    return response.data;
  }
}

Model 🧲 #

We need to be required "toJson" and "fromJson" on the model. Your model must be extended "Serializable" model, so your model overrides the "toJson" function. You call the Consturctuor.fromJson method on income method.


class Todo extends Serializable<Todo> {
  int userId;
  int id;
  String title;
  bool completed;

  Todo({this.userId, this.id, this.title, this.completed});

  Todo.fromJson(Map<String, dynamic> json) {
    userId = json['userId'];
    id = json['id'];
    title = json['title'];
    completed = json['completed'];
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['userId'] = this.userId;
    data['id'] = this.id;
    data['title'] = this.title;
    data['completed'] = this.completed;
    return data;
  }

  @override
  Todo fromJson(Map<String, Object> json) => Todo.fromJson(json);
}

Caching #

You have cache options in any request. You will declare duration in your any request then cache using now. You have two options for "Cache" If you want to cache on mobile phone cache(like shared preferences or nsuserdata ), write "LocalPreferences" on your config consturctor; also, you want to cache on file (any JSON), write "LocalFile" on your config. (Note if you didn't write any Cache type, we default set "LocalFile")

 var config =
        NetworkConfig(baseUrl: "https://jsonplaceholder.typicode.com/",fileManager: LocalFile());

This user request is caching for one minute. (More than information look test folder.)

 final response = await manager.get<User>("users",
        responseType: User(), duration: Duration(minutes: 1));

Project Usage #

Finally, you have learning and config options on your project. Let's write to your manager's class.

  class PlaceHolderNetworkManager {
NetworkManager manager;
static PlaceHolderNetworkManager _instance;
static PlaceHolderNetworkManager get instance {
  if (_instance == null) {
    _instance = PlaceHolderNetworkManager._init();
  }
  return _instance;
}

PlaceHolderNetworkManager._init() {
  manager = NetworkManager(
      config: PlaceHolderConfig(), learning: PlaceHolderLearning());
}
}

Everything is okay now. You can use it in your project. 🎊 #