搜尋套件、儲存庫與 APP 分類

套件詳情v0.1.36

api_manager

一個簡單的套件,藉由 flutter dio api 輕鬆管理 API 請求。

AndroidiOSWebmacOSWindowsLinux
近 30 天下載
301
喜歡數
25
Pub 分數
140

安裝套件

flutter pub add api_manager

套件介紹

根據套件公開資料產生的選型評估,僅供參考。

一個輕量級的 Flutter 套件,旨在使用 Dio HTTP 客戶端簡化 API 請求管理。它提供了一個易於使用的介面,用於發起 HTTP 呼叫、處理回應以及在專案中一致地管理錯誤。專為開發者體驗而設計,可簡化常見的任務,如驗證標頭、請求攔截和回應解析。非常適合需要可靠且可維護網路通訊的應用程式。

適用情境

  • 處理 REST API 呼叫
  • 管理身份驗證令牌
  • 集中化 API 端點
  • 錯誤處理和重試邏輯
  • 請求/響應日誌
  • 簡化網路層程式碼

主要特色

  • 基於 Dio 建構
  • 輕鬆設定與設定
  • 支援攔截器
  • 自動錯誤處理
  • 模組化和可重用的設計
  • 關注點的清晰分離

適合對象

  • Flutter 初學者學習網路程式設計
  • 需要一致 API 模式的項目
  • 具有多個 API 端點的應用程式
  • 注重可維護代碼的團隊
  • 希望程式碼量最少的開發者

使用建議

  • 需要了解 Dio 的基本知識
  • 僅限 HTTP 請求(不支援 WebSocket)
  • 可能需要額外設定以處理複雜的認證流程
  • 未經客製化,不適合大型企業應用

原始 README

以下為關聯原始碼儲存庫的 README 原文快照,最新內容請造訪原始碼儲存庫。

api_manager pub package

A simple flutter API to manage rest api request easily with the help of flutter dio.

Get started

Install

Add dependency

pub package

dependencies:
  api_manager: $latest_version
Super simple to use
import 'package:api_manager/api_manager.dart';

void main() async {
 
  ApiResponse response = await ApiManager().request(
    requestType: RequestType.GET,
    route: "your route",
  );
  print(response);
}
Config in a base manager

class ApiRepository {
  
  static final ApiRepository _instance = ApiRepository._internal(); /// singleton api repository
  ApiManager _apiManager;

  factory ApiRepository() {
    return _instance;
  }

  /// base configuration for api manager
  ApiRepository._internal() {
    _apiManager = ApiManager();
    _apiManager.options.baseUrl = BASE_URL; /// EX: BASE_URL = https://google.com/api/v1 
    _apiManager.options.connectTimeout = 100000;
    _apiManager.options.receiveTimeout = 100000;
    _apiManager.responseBodyWrapper('data'); /// This is used to parse the response without data attribute, some use case will shown below
    _apiManager.enableLogging(responseBody: true, requestBody: false); /// enable api logging EX: response, request, headers etc
    _apiManager.enableAuthTokenCheck(() => "access_token"); /// EX: JWT/PASSPORT auth token store in cache
  }
}

Examples

Suppose we have a response model like this:

class SampleResponse{
  String name;
  int id;

  SampleResponse.fromJson(jsonMap): 
        this.name = jsonMap['name'],
        this.id = jsonMap['id'];
}

and actual api response json structure is:

{
    "data": {
        "name": "md afratul kaoser taohid",
        "id": "id"
    }
}

#Now we Performing a GET request :

 Future<ApiResponse<SampleResponse>> getRequestSample() async =>
      await _apiManager.request<SampleResponse>(
        requestType: RequestType.GET,
        route: 'api_route',
        requestParams: {"userId": 12}, /// add params if required
        isAuthRequired: true, /// by set it to true, this request add a header authorization from this method enableAuthTokenCheck();
        responseBodySerializer: (jsonMap) {
          return SampleResponse.fromJson(jsonMap); /// parse the json response into dart model class
        },
      );

#Now we Performing a POST request :

 Future<ApiResponse<SampleResponse>> postRequestSample() async =>
      await _apiManager.request<SampleResponse>(
        requestType: RequestType.POST,
        route: 'api_route',
        requestBody: {"userId": 12}, /// add POST request body
        isAuthRequired: true, /// by set it to true, this request add a header authorization from this method enableAuthTokenCheck();
        responseBodySerializer: (jsonMap) {
          return SampleResponse.fromJson(jsonMap); /// parse the json response into dart model class
        },
      );

#Now er performing a multipart file upload request :

  Future<ApiResponse<void>> updateProfilePicture(
    String filePath,
  ) async {
    MultipartFile multipartFile =
        await _apiManager.getMultipartFileData(filePath);
    FormData formData = FormData.fromMap({'picture': multipartFile});

    return await _apiManager.request(
      requestType: RequestType.POST,
      isAuthRequired: true,
      requestBody: formData,
      route: 'api_route',
    );
  }

所屬分類

依賴

依賴清單 2 項

原始碼儲存庫

使用此套件的專案

暫無相關資料。