Red Clan Network

Learn More

About

red_clan_network is a Dart package designed to simplify API requests by abstracting common functionalities like GET, POST, PUT, PATCH, and DELETE methods. It provides a flexible and customizable way to handle different types of HTTP requests and parse responses into Dart models.

Features

  • API Type: Just provide API type(GET, POST, PATCH, PUT, DELETE).
  • Flexible Data Handling: If you also need something in return you can provide model or function will return response.statusCode and resonponse.body automatically.
  • Simplified Code: Get auto mapped models in simpliest form of success or failure.

Warrning

red_clan_network does not help you with Socket APIs, this feature is under development and coming soon. You can contribute with us by doing work on github repo.

Red_Clan_Network Example

final ApiService apiService = ApiService();
Create Model
class TodoModel {
  int? userId;
  int? id;
  String? title;
  bool? completed;

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

  TodoModel.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;
  }
}

GET API Example

Make an instance of api before use
 //final ApiService apiService = ApiService();

List<TodoModel> dataList = [];
String message = 'Press button any of above';
 Future<void> fetchData() async {
    var response = await apiService.request<TodoModel>(
      url: 'https://jsonplaceholder.typicode.com/todos', 
      method: 'GET',
      modelFromJson: (json) => TodoModel.fromJson(json),
      successStatusCodes: [200, 201], 
    );
    setState(() {
      if (response.isSuccess) {
        message = 'Data Fetched Success';
      } else {
        message = 'Some thing went wrong with ${response.code}';
      }
    });
  }
  // you can call any API of get using this function

Post Example

Future<void> postData() async {
  var bodyData = {
    "email": "codered@youtube.com",
    "secure_pin": "this is my pin"
  };
  var response = await apiService.request(
    url: 'https://api.yoururl.com//login', // Add your URL
    method: 'POST',
    successStatusCodes: [200, 201], 
    headers: {
      'Content-Type': 'application/json',
    },
    body: bodyData,
  );

  setState(() {
    if (response.isSuccess) {
     
      message = 'Data post Success';
    } else {
      message = 'Something went wrong with ${response.code}';
    }
  });
}

Delete Example

Future<void> delete() async {
  var bodyData = {
    "email": "coderedclan@youtube.com",
    "secure_pin": "this is my pin"
  };
  var response = await apiService.request(
    url: 'https://dummy.restapiexample.com/api/v1/delete/3', // Add your URL
    method: 'DELETE',
    successStatusCodes: [200, 201], 
    headers: {
      'Content-Type': 'application/json',
      'Authorization':""
    },
    body: bodyData,
  );

  setState(() {
    dataList = [];
    if (response.isSuccess) {
      message = 'Delete Success';
    } else {
      message = 'Something went wrong with ${response.code}';
    }
  });
}

Put Example

Future<void> putData()async{
var bodyData = {
  "name": "CompanyX",
  "metric_limit": 50,
  "allow_negative_credits": false
};
  var response = await apiService.request(
    url: 'https://dummy.restapiexample.com/api/v1/update/21', // Add your URL
    method: 'PUT',
    successStatusCodes: [200, 201], 
    headers:{
  'Authorization': ' ',
  'Content-Type': 'application/json'
},
    body: bodyData,
  );

  setState(() {
    dataList = [];
    if (response.isSuccess) {
      message = 'Data Put Success';
    } else {
      message = 'Something went wrong with ${response.code}';
    }
  });
  
}

Patch Example

Future<void> patchData()async{
var bodyData = {
  "name": "CompanyX",
  "metric_limit": 50,
  "allow_negative_credits": false
};
  var response = await apiService.request(
    url: 'https://your_patch_api_link', // Add your URL
    method: 'PATCH',
    successStatusCodes: [200, 201], 
    headers:{
  'Authorization': ' ',
  'Content-Type': 'application/json'
},
    body: bodyData,
  );

  setState(() {
    dataList = [];
    if (response.isSuccess) {
      message = 'Patch Success';
    } else {
      message = 'Something went wrong with ${response.code}';
    }
  });
  
}

Pass model as function parameters


  // Fetch data from the API
  Future<void> fetchData<T>(T Function(dynamic) modelFromJson) async {
    var response = await apiService.request<T>(
      url: 'https://jsonplaceholder.typicode.com/todos',
      method: 'GET',
      modelFromJson: modelFromJson,
      successStatusCodes: [200, 201],
    );

    setState(() {
      if (response.isSuccess) {
        message = 'Data Fetched Successfully';
        dataList = response.response as List<T>;
        statusCode= response.code;

      } else {
        message = 'Something went wrong with ${response.code}';
        statusCode= response.code;

      }
    });
  }

  // Post data to the API
  Future<void> postData<T>(T Function(dynamic) modelFromJson)async {
    var bodyData = {
    "title": 'foo',
    "body": 'bar',
    "userId": 1,
  };
    var response = await apiService.request<T>(
      url: 'https://jsonplaceholder.typicode.com/posts',
      method: 'POST',
      successStatusCodes: [200, 201],
      headers: {
        'Content-Type': 'application/json',
      },
      body: bodyData,
      modelFromJson: modelFromJson,
    );

    setState(() {
      if (response.isSuccess) {
        message = 'Data Post Success';
        statusCode= response.code;

      } else {
        message = 'Something went wrong with ${response.code}';
        statusCode= response.code;
      }
    });
  }

  // Put data to the API
Future<void> putData<T>(T Function(dynamic) modelFromJson) async {
  var bodyData = {
    "id": 1,
    "title": 'foo',
    "body": 'bar',
    "userId": 1,
  };
  var response = await apiService.request<T>(
    url: 'https://jsonplaceholder.typicode.com/posts/1',
    method: 'PUT',
    successStatusCodes: [200, 201],
    headers: {'Authorization': ' ', 'Content-Type': 'application/json'},
    body: bodyData,
    modelFromJson: modelFromJson,
  );

  setState(() {
    dataList = [];
    if (response.isSuccess) {
      message = 'Data Put Success';
        statusCode= response.code;

    } else {
      message = 'Something went wrong with ${response.code}';
        statusCode= response.code;

    }
  });
}

  // Delete data from the API
  Future<void> deleteData<T>(T Function(dynamic) modelFromJson) async {
  
    var response = await apiService.request<T>(
      url: 'https://dummy.restapiexample.com/api/v1/delete/3',
      method: 'DELETE',
      successStatusCodes: [200, 201],
      headers: {'Content-Type': 'application/json', 'Authorization': ""},
      modelFromJson: modelFromJson,
    );

    setState(() {
      dataList = [];
      if (response.isSuccess) {
        message = 'Delete Success';
        statusCode= response.code;

      } else {
        message = 'Something went wrong with ${response.code}';
        statusCode= response.code;

      }
    });
  }

  // Patch data to the API
  Future<void> patchData<T>(T Function(dynamic) modelFromJson) async {
    var bodyData = {
          "title": 'foo',

    };
    var response = await apiService.request<T>(
      url: 'https://jsonplaceholder.typicode.com/posts/1',
      method: 'PATCH',
      successStatusCodes: [200, 201],
      headers: {'Authorization': ' ', 'Content-Type': 'application/json'},
      body: bodyData,
      modelFromJson: modelFromJson,
    );

    setState(() {
      dataList = [];
      if (response.isSuccess) {
        statusCode= response.code;

        message = 'Patch Success';
      } else {
        statusCode= response.code;

        message = 'Something went wrong with ${response.code}';
      }
    });
  }

  // Post data with a file to the API
  Future<void> postDataWithFile() async {
    var request = http.MultipartRequest(
      'POST',
      Uri.parse('https://your_file_upload_api_link'),
    );

    request.headers.addAll({
      'Content-Type': 'multipart/form-data',
      'Authorization': 'Bearer your_token_here',
    });

    request.fields['description'] = 'File upload example';

    var file = await http.MultipartFile.fromPath('file', 'path_to_your_file');
    request.files.add(file);

    var response = await request.send();
    final responseBody = await response.stream.bytesToString();

    setState(() {
      if (response.statusCode == 200 || response.statusCode == 201) {
        message = 'File Upload Success';
        statusCode= response.statusCode;
      } else {
        statusCode= response.statusCode;

        message = 'Something went wrong with ${response.statusCode}';
      }
    });
  }

Learn More

Contributors

Learn More Learn More Learn More

  • Rana Sharjeel Ali Flutter Developer - Muhammad Abuzar Backend Developer

Libraries

red_clan_network