general_repository 0.0.4 copy "general_repository: ^0.0.4" to clipboard
general_repository: ^0.0.4 copied to clipboard

A versatile API repository for handling API responses efficiently, designed for Flutter apps needing robust data management.

General Repository Package #

Overview #

The general_repository package provides a robust and flexible repository layer for handling HTTP requests in Dart and Flutter applications. It includes built-in authentication, automatic token refresh, detailed logging, error handling, and support for various HTTP methods.

Features #

  • Authentication with token refresh mechanism
  • Supports GET, POST, PUT, PATCH, DELETE, and Multipart requests
  • Automatic header management (Authorization and Content-Type)
  • Configurable request timeouts
  • Enhanced logging for debugging purposes
  • Exception handling for network errors, timeouts, and API failures
  • Customizable token storage and session management

Installation #

Add the following dependency to your pubspec.yaml file:

dependencies:
  general_repository: ^0.0.4

Then, run:

flutter pub get

Usage #

Initialization #

Create an instance of GeneralRepository by providing authentication parameters:

import 'package:general_repository/general_repository.dart';

final repository = GeneralRepository(
  currentUserToken: 'your_access_token',
  refreshToken: 'your_refresh_token',
  updateTokens: (newAccessToken, newRefreshToken) {
    // Store updated tokens securely
  },
  clearUser: () {
    // Handle user logout
  },
);

Making API Calls #

GET Request

final response = await repository.get(handle: '/endpoint');
print(response);

POST Request

final response = await repository.post(
  handle: '/endpoint',
  body: {'key': 'value'},
);
print(response);

PUT Request

final response = await repository.put(
  handle: '/endpoint',
  body: {'key': 'updated_value'},
);
print(response);

PATCH Request

final response = await repository.patch(
  handle: '/endpoint',
  body: {'key': 'partial_update'},
);
print(response);

DELETE Request

final response = await repository.delete(handle: '/endpoint');
print(response);

Handling Multipart Requests #

final response = await repository.multipartPost(
  handle: '/upload',
  fields: {'description': 'file upload'},
  files: [
    MultipartFile(
      fieldName: 'file',
      filePath: '/path/to/file.jpg',
      fileName: 'file.jpg',
    ),
  ],
);
print(response);

Token Refresh Mechanism #

If an API call returns a 401 Unauthorized error, the repository automatically tries to refresh the token and retries the request. If refreshing fails, the clearUser callback is triggered to log out the user.

Future<String?> fetchNewToken() async {
  final refreshHeader = {"Authorization": "Bearer \$refreshToken"};
  final refreshUri = Uri.parse("https://api.example.com/refresh-token");

  try {
    final refreshResponse = await http.get(refreshUri, headers: refreshHeader);
    if (refreshResponse.statusCode == 200) {
      final responseJson = jsonDecode(refreshResponse.body);
      updateTokens(responseJson["accessToken"], responseJson["refresh_token"]);
      return responseJson["accessToken"];
    } else {
      clearUser();
      return null;
    }
  } catch (e) {
    throw FetchDataException("Network error while refreshing token.");
  }
}

Exception Handling #

The repository handles exceptions for network errors, timeouts, and failed responses.

try {
  final response = await repository.get(handle: '/secure-endpoint');
  print(response);
} catch (e) {
  print('Error: \$e');
}

Logging #

The repository provides detailed logs for debugging:

final response = await repository.get(handle: '/endpoint', enableLogs: true);

Error Handling #

  • FetchDataException: Thrown for network errors.
  • TimeOutExceptionC: Thrown for request timeouts.
  • API-specific errors are thrown based on response status codes.

Conclusion #

The general_repository package simplifies API handling in Flutter applications by providing built-in authentication, token refresh, logging, and error handling. It is highly customizable and can be seamlessly integrated into any project.

9
likes
160
points
15
downloads

Publisher

unverified uploader

Weekly Downloads

A versatile API repository for handling API responses efficiently, designed for Flutter apps needing robust data management.

Homepage
Repository (GitHub)
View/report issues

Documentation

API reference

License

MIT (license)

Dependencies

http

More

Packages that depend on general_repository