network 0.7.1

  • Readme
  • Changelog
  • Example
  • Installing
  • 80
Logo

Package including hooks for easy works with http package in dart.

Pub

Key FeaturesGetting StartedTodoCredits

Key Features #

  • Simple hooks for get / post
  • Specify response type (blob\json api)

Getting Started #

Get request to API:

main() async {
  final getResponse = await network.get<network.JsonApiResponse>(
      'https://jsonplaceholder.typicode.com/todos/1');
  print(getResponse.toMap['title']);
}

Get request Blob:

main() async {
  final blobResponse = await network.get(
      'https://via.placeholder.com/300');
  print(blobResponse.bytes);
}

Post request to API:

final postResponse = await network.post<network.JsonApiResponse>(
      'https://jsonplaceholder.typicode.com/todos',
      body: {'title': 'test'});
  print(postResponse.toMap['id']);

Handle exceptions:

try {
  await network.get('https://jsonplaceholder.typicode.com/todos/202');
} on network.NetworkException catch (error) {
  print('Network exception called, status code: ${error.code}');
}

Provide their exceptions:

network.NetworkSettings().exceptionDelegate = (network.NetworkException error) {
  // You can check type of respose in error by:
  // if (error is network.JsonApiResponse)

  switch (error.code) {
    case 400:
      throw BadRequestException(error.response);
    case 404:
      throw NotFoundException(error.response);
    default:
      throw error;
  }
};

And... api reference available here

TODO #

  • [x] Get
  • [x] Post
  • [x] Delete
  • [x] Put
  • [ ] Decorators (#1)

Credits #

This software uses the following open source packages:

0.7.1 #

  • Fix defaul headers

0.7.0 #

  • Added property to use an external http-client

0.6.0 #

  • Added userAgent (available in Settings)
  • Added defaultHeaders (available in Settings)
  • Added default http exceptions (like a unautchorized, not found etc)

0.5.0 #

  • Refactoring

0.4.0 #

  • Added NetworkUnavailableException
  • Fixed exceptionDelegate

0.3.0 #

  • Added NetworkSettings
  • Added exceptionDelegate in NetworkSettings

0.2.0 #

  • Added toString() method for NetworkException and BinaryResponse
  • Added toJsonApiResponse() method for BinaryResponse

0.1.0 #

  • Added parameter queryParameters for all methods
  • Removed parameter jsonBody from post and put methods

0.0.2 #

  • Removed debug print
  • Added hooks for delete & put http methods

0.0.1 #

  • Initial release

example/main.dart

import 'package:network/network.dart' as network;

/// HTTP 400
class BadRequestException<T extends network.BinaryResponse>
    extends network.NetworkException<T> {
  BadRequestException(T response) : super(response);
}

/// HTTP 404
class NotFoundException<T extends network.BinaryResponse>
    extends network.NetworkException<T> {
  NotFoundException(T response) : super(response);
}

/// No connection to internet
class NoInternetConnection {}

main() async {
  network.NetworkSettings().exceptionDelegate = (error) {
    // You can check type of respose in error by:
    // if (error is network.JsonApiResponse)

    if (error is network.NetworkException) {
      switch (error.code) {
        case 400:
          throw BadRequestException(error.response);
        case 404:
          throw NotFoundException(error.response);
        default:
          throw error;
      }
    } else if (error is network.NetworkUnavailableException) {
      throw NoInternetConnection();
    }

    throw error;
  };

  try {
    final getResponse = await network.get<network.JsonApiResponse>(
        'https://jsonplaceholder.typicode.com/comments',
        queryParameters: {'postId': 1});
    print(getResponse.toList[1]['body']);

    // Post request to api
    final postResponse = await network.post<network.JsonApiResponse>(
        'https://jsonplaceholder.typicode.com/todos',
        body: {'title': 'test'});
    print(postResponse.toMap['id']);
  } on NoInternetConnection {
    print('No intrernet connection');
  }

  // Or post binary
  await network.post<network.JsonApiResponse>(
      'https://jsonplaceholder.typicode.com/todos',
      body: [0, 0, 0, 0, 0]);

  // Handle exceptions
  try {
    await network.get('https://jsonplaceholder.typicode.com/todos/202');
  } on network.NetworkException catch (error) {
    print('Network exception handled: ${error}');
  }
}

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:


dependencies:
  network: ^0.7.1

2. Install it

You can install packages from the command line:

with pub:


$ pub get

with Flutter:


$ flutter pub get

Alternatively, your editor might support pub get or flutter pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:


import 'package:network/network.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
61
Health:
Code health derived from static analysis. [more]
100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
100
Overall:
Weighted score of the above. [more]
80
Learn more about scoring.

We analyzed this package on Oct 21, 2019, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.5.1
  • pana: 0.12.21

Platforms

Detected platforms: Flutter, other

Primary library: package:network/network.dart with components: io.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.2.2 <3.0.0
http ^0.12.0+2 0.12.0+2
Transitive dependencies
async 2.4.0
charcode 1.1.2
collection 1.14.12
http_parser 3.1.3
meta 1.1.7
path 1.6.4
pedantic 1.8.0+1
source_span 1.5.5
string_scanner 1.0.5
term_glyph 1.1.0
typed_data 1.1.6
Dev dependencies
test ^1.5.3