http_api_service 1.0.0 copy "http_api_service: ^1.0.0" to clipboard
http_api_service: ^1.0.0 copied to clipboard

A simple and reusable HTTP service for Flutter applications.

api_service #

api_service is a Dart package that provides an easy-to-use, reusable HTTP service for Flutter applications. With this package, you can perform various HTTP requests such as GET, POST, PUT, and DELETE in a simple and structured way.

Key Features #

  • Supports multiple HTTP methods: GET, POST, PUT, DELETE
  • Robust Response Handling: Easily manage responses and exceptions
  • Reusable and Easy to Use: Can be reused across different parts of the application
  • Logging: Logs each request and response for easier debugging
  • Supports query parameters and custom headers: Customize each request to fit your application's needs

Installation #

To use this package, add api_service to your pubspec.yaml file.

dependencies:
  api_service:
    git:
      url: https://github.com/naneps/api_service.git
      ref: main

Then run the following command to install the dependencies:

dart pub get

Usage #

Initialize ApiService #

To use ApiService, you need to initialize an instance of the ApiService class with the base URL of the API you want to interact with.

import 'package:api_service/api_service.dart';

void main() {
  final apiService = ApiService(baseUrl: 'https://api.example.com');
}

Using HTTP Methods #

Here’s how to use the different HTTP methods supported by api_service.

GET Request

final response = await apiService.get('/users');
if (response.statusCode == 200) {
  // Successful response
  print('Response: ${response.body}');
} else {
  // Handle error
  print('Error: ${response.statusCode}');
}

POST Request

final response = await apiService.post(
  '/users',
  body: {
    'name': 'John Doe',
    'email': 'john@example.com',
  },
);
if (response.statusCode == 201) {
  // Data successfully created
  print('Created: ${response.body}');
} else {
  // Handle error
  print('Error: ${response.statusCode}');
}

PUT Request

final response = await apiService.put(
  '/users/1',
  body: {
    'name': 'John Doe Updated',
    'email': 'john_updated@example.com',
  },
);
if (response.statusCode == 200) {
  // Data successfully updated
  print('Updated: ${response.body}');
} else {
  // Handle error
  print('Error: ${response.statusCode}');
}

DELETE Request

final response = await apiService.delete('/users/1');
if (response.statusCode == 200) {
  // Data successfully deleted
  print('Deleted: ${response.body}');
} else {
  // Handle error
  print('Error: ${response.statusCode}');
}

Error Handling #

api_service automatically handles common HTTP status codes and throws exceptions based on the error type.

Example error handling:

try {
  final response = await apiService.get('/users');
  // Handle successful response
} catch (e) {
  print('Error occurred: $e');
}

Handled Exceptions: #

  • BadRequestException
  • UnauthorizedException
  • ForbiddenException
  • NotFoundException
  • InternalServerErrorException
  • ApiException

Contribution #

If you want to contribute to this project, feel free to fork this repository and submit a pull request. Before doing so, make sure to follow the code guidelines and run tests.

License #

This package is licensed under the MIT License.


For further questions or support, visit the GitHub repository.


### Explanation:
- The **Introduction** section briefly describes what the package does.
- The **Installation** section explains how to add this package to your project.
- The **Usage** section provides practical examples of how to use each HTTP method.
- **Error Handling** explains how exceptions are managed and how to handle them.
- The **Contribution** section encourages others to contribute to the development of the package.
- The **License** section informs users of the licensing terms.

This `README.md` is structured to be clear and informative, making it easy for other developers to start using and contributing to your project.
1
likes
0
points
12
downloads

Publisher

unverified uploader

Weekly Downloads

A simple and reusable HTTP service for Flutter applications.

Repository (GitHub)
View/report issues

License

unknown (license)

Dependencies

http

More

Packages that depend on http_api_service