パッケージ、リポジトリ、アプリカテゴリを検索

パッケージ詳細v0.1.36

api_manager

flutter dio api の助けを借りて、APIリクエストを簡単に管理するためのシンプルなパッケージです。

AndroidiOSWebmacOSWindowsLinux
30日間ダウンロード
301
いいね数
25
Pub ポイント
140

パッケージをインストール

flutter pub add api_manager

パッケージ概要

公開情報に基づく選定ガイドです。参考としてご利用ください。

Dio HTTPクライアントを使用してAPIリクエスト管理を簡素化するように設計された軽量なFlutterパッケージです。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 件

ソースリポジトリ

このパッケージを使用しているプロジェクト

データはまだありません。