teja_http_json 0.1.5

  • README.md
  • CHANGELOG.md
  • Example
  • Installing
  • Versions
  • 0

teja_http_json

Write concise JSON API clients

Usage

Get request

final JsonResponse resp =
    await client.get('http://localhost:8080/api/list');
print(resp.body);

Post request

final JsonResponse resp = await client
    .post('http://localhost:8080/api/map', body: {'posting': 'hello'});
print(resp.body);

Put request

final JsonResponse resp = await client
    .put('http://localhost:8080/api/map', body: {'putting': 'hello'});
print(resp.body);

Delete request

final JsonResponse resp =
    await client.delete('http://localhost:8080/api/map/123?query=why');
print(resp.body);

Changelog

0.1.2

  • Serialize/Deserialize using jaguar_serializer

0.0.1

  • Initial version

example/teja_http_json_example.dart

// Copyright (c) 2017, teja. All rights reserved. Use of this source code
// is governed by a BSD-style license that can be found in the LICENSE file.

import 'dart:io';
import 'dart:async';
import 'package:http/http.dart' as http;
import 'package:jaguar/jaguar.dart';
import 'package:jaguar_reflect/jaguar_reflect.dart';
import 'package:teja_http_json/teja_http_json.dart';

@Api(path: '/api')
class ExampleApi {
  @Get(path: '/map')
  Response<String> getMap(Context ctx) => Response.json({
        'jaguar': 'awesome',
      });

  @Get(path: '/list')
  Response<String> getList(Context ctx) => Response.json(['Hello', 'World']);

  @Get(path: '/string')
  Response<String> getString(Context ctx) => Response.json("Jaguar");

  @Get(path: '/header')
  Response<String> getHeader(Context ctx) =>
      Response.json({'testing': ctx.req.headers.value('jaguar-testing')});

  @Post(path: '/map')
  Future<Response<String>> postMap(Context ctx) async =>
      Response.json(await ctx.req.bodyAsJsonMap());

  @Put(path: '/map')
  Future<Response<String>> putMap(Context ctx) async =>
      Response.json(await ctx.req.bodyAsJsonMap());

  @Delete(path: '/map/:id')
  Response<String> deleteMap(Context ctx) =>
      Response.json({'id': ctx.pathParams.id, 'query': ctx.queryParams.query});
}

Future serve() async {
  Jaguar server = new Jaguar();
  server.addApi(reflectJaguar(new ExampleApi()));
  await server.serve();
}

Future client() async {
  final http.Client baseClient = new http.Client();
  final JsonClient client = new JsonClient(baseClient);

  {
    final JsonResponse resp = await client.get('http://localhost:8080/api/map');
    print(resp.body);
  }

  {
    final JsonResponse resp =
        await client.get('http://localhost:8080/api/list');
    print(resp.body);
  }

  {
    final JsonResponse resp =
        await client.get('http://localhost:8080/api/string');
    print(resp.body);
  }

  {
    final JsonResponse resp = await client.get(
        'http://localhost:8080/api/header',
        headers: {'jaguar-testing': 'testing 1 2 3'});
    print(resp.body);
  }

  {
    final JsonResponse resp = await client
        .post('http://localhost:8080/api/map', body: {'posting': 'hello'});
    print(resp.body);
  }

  {
    final JsonResponse resp = await client
        .put('http://localhost:8080/api/map', body: {'putting': 'hello'});
    print(resp.body);
  }

  {
    final JsonResponse resp =
        await client.delete('http://localhost:8080/api/map/123?query=why');
    print(resp.body);
  }
}

main() async {
  await serve();
  await client();

  exit(0);
}

Use this package as a library

1. Depend on it

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


dependencies:
  teja_http_json: ^0.1.5

2. Install it

You can install packages from the command line:

with pub:


$ pub get

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

3. Import it

Now in your Dart code, you can use:


import 'package:teja_http_json/teja_http_json.dart';
  
Version Uploaded Documentation Archive
0.1.5 Jul 9, 2017 Go to the documentation of teja_http_json 0.1.5 Download teja_http_json 0.1.5 archive
0.1.4 Jul 8, 2017 Go to the documentation of teja_http_json 0.1.4 Download teja_http_json 0.1.4 archive
0.1.3 Jul 7, 2017 Go to the documentation of teja_http_json 0.1.3 Download teja_http_json 0.1.3 archive
0.1.2 Jul 7, 2017 Go to the documentation of teja_http_json 0.1.2 Download teja_http_json 0.1.2 archive
0.1.1 Jul 3, 2017 Go to the documentation of teja_http_json 0.1.1 Download teja_http_json 0.1.1 archive
0.1.0 Jun 18, 2017 Go to the documentation of teja_http_json 0.1.0 Download teja_http_json 0.1.0 archive
0.0.1 Feb 21, 2017 Go to the documentation of teja_http_json 0.0.1 Download teja_http_json 0.0.1 archive
0.1.6-dev Jul 13, 2017 Go to the documentation of teja_http_json 0.1.6-dev Download teja_http_json 0.1.6-dev archive
Popularity:
Describes how popular the package is relative to other packages. [more]
0
Health:
Code health derived from static analysis. [more]
--
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
--
Overall:
Weighted score of the above. [more]
0
Learn more about scoring.

The package version is not analyzed, because it does not support Dart 2. Until this is resolved, the package will receive a health and maintenance score of 0.

Analysis issues and suggestions

Support Dart 2 in pubspec.yaml.

The SDK constraint in pubspec.yaml doesn't allow the Dart 2.0.0 release. For information about upgrading it to be Dart 2 compatible, please see https://www.dartlang.org/dart-2#migration.

Maintenance issues and suggestions

Running dartdoc failed. (-10 points)

Make sure dartdoc runs without any issues.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=1.19.0 <2.0.0