iw_rest_client 1.1.1 copy "iw_rest_client: ^1.1.1" to clipboard
iw_rest_client: ^1.1.1 copied to clipboard

This project is an implementation that allow developers to encapsulate their own usage of a http client.

example/example.dart

import 'dart:convert';
import 'dart:io';

import 'package:iw_rest_client/iw_rest_client.dart';

Future<void> main() async {
  try {
    final RestClientService service = HttpClientService();

    for (var method in RestMethod.values) {
      final RestResponse response = switch (method) {
        // GET
        RestMethod.get => await service.fetch(
            method: RestMethod.get,
            uri: RestUri(
              scheme: RestScheme.https,
              host: 'jsonplaceholder.typicode.com',
              path: '/todos/1',
            ),
            headers: {
              'Content-Type': 'application/json',
            },
          ),
        // POST
        RestMethod.post => await service.fetch(
            method: RestMethod.post,
            uri: RestUri(
              scheme: RestScheme.https,
              host: 'jsonplaceholder.typicode.com',
              path: '/posts',
            ),
            headers: {
              'Content-Type': 'application/json',
            },
            body: jsonEncode({
              'title': 'foo',
              'body': 'bar',
              'userId': 1,
            }),
            encoding: Encoding.getByName('UTF-8'),
          ),
        // PUT
        RestMethod.put => await service.fetch(
            method: RestMethod.put,
            uri: RestUri(
              scheme: RestScheme.https,
              host: 'jsonplaceholder.typicode.com',
              path: '/posts/1',
            ),
            headers: {
              'Content-Type': 'application/json',
            },
            body: jsonEncode({
              'id': 1,
              'title': 'foo',
              'body': 'bar',
              'userId': 1,
            }),
            encoding: Encoding.getByName('UTF-8'),
          ),
        // PATCH
        RestMethod.patch => await service.fetch(
            method: RestMethod.patch,
            uri: RestUri(
              scheme: RestScheme.https,
              host: 'jsonplaceholder.typicode.com',
              path: '/posts/1',
            ),
            headers: {
              'Content-Type': 'application/json',
            },
            body: jsonEncode({
              'title': 'foo',
            }),
            encoding: Encoding.getByName('UTF-8'),
          ),
        // DELETE
        RestMethod.delete => await service.fetch(
            method: RestMethod.delete,
            uri: RestUri(
              scheme: RestScheme.https,
              host: 'jsonplaceholder.typicode.com',
              path: '/posts/1',
            ),
            headers: {
              'Content-Type': 'application/json',
            },
          ),
      };

      print('$method: $response');
    }

    exit(0);
  } on RestException catch (error) {
    print('Error [$error]');
    exit(1);
  } catch (error) {
    print('Unhandled error [$error]');
    exit(2);
  }
}
2
likes
160
points
47
downloads

Publisher

verified publisherivanwilhelm.dev

Weekly Downloads

This project is an implementation that allow developers to encapsulate their own usage of a http client.

Repository (GitHub)
View/report issues

Topics

#http #rest

Documentation

API reference

License

MIT (license)

Dependencies

collection, http

More

Packages that depend on iw_rest_client