restquester 0.6.0

  • Readme
  • Changelog
  • Example
  • Installing
  • 62

restquester #

Package simplifying rest requests

Usage #

Example for sending post request

  import 'package:restquester/requester.dart';

  //create scope for localhost api
  RequestScope scope = RequestScope
        .newScope(baseUrl: 'https://localhost:8080/');
  //Instantiating request body for request
  final LoginBody body = LoginBody(
    login: "admin",
    password: "admin",
    clientId:
    "d524c1a0811dt35692f841085cc0063eb62b902735a94542795d1ca9824a941",
  );
  //Instantiating RequestBuilder for specified method and post
  final Response response = await scope
      .newRequestBuilder()
      .post('authorize')
      //setting mapper which will convert json map into specific data model
      .withMapper((map) => Response.fromJson(map))
      //set request body to be sent in request
      //body should have `toJson` method which returns map of data
      .withBody(body)
      //returns Future with converted data model
      .execute();

Example request body model

class LoginBody {
  String login;
  String password;
  String clientId;

  LoginBody({this.login, this.password, this.clientId});

  LoginBody.fromJson(Map<String, dynamic> json) {
    login = json['login'];
    password = json['password'];
    clientId = json['clientId'];
  }

  //needed to be converted to json inside RequestBuilder
  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['login'] = this.login;
    data['password'] = this.password;
    data['clientId'] = this.clientId;
    return data;
  }
}

[0.5.0] - 16.12.2019 #

  • initial RequestBuilder release

[0.5.1] - 17.12.2019 #

  • Added RequestScope to set some default values for request from specified scope
  • Rearranged RequestBuilder
  • Added example usage

[0.5.2] - 21.01.2020 #

  • Change Mapper parameter type to dynamic - allow mapping lists

[0.5.3] - 21.01.2020 #

  • Fix list mapping

[0.5.4] - 22.01.2020 #

  • Fix list mapping

[0.5.5] - 10.02.2020 #

  • Add logging possibility

[0.6.0] - 12.02.2020 #

  • Add error handling

example/lib/main.dart

library example;

import 'package:flutter/material.dart';
import 'package:restquester/scope.dart';

void main() {
  runApp(App());
}

class App extends StatelessWidget {

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Content(),
    );
  }
}

class Content extends StatefulWidget {

  @override
  State createState() => ContentState();
}

class ContentState extends State<Content> {
  final RequestScope _scope = RequestScope.newScope(
      baseUrl: 'https://api.github.com/');
  String _text = "";

  @override
  void initState() {
    super.initState();
    getCompanyName()
        .then((name) => setText(name))
        .catchError((error) => setText("$error"));
  }

  void setText(String newText) {
    setState(() {
      _text = newText;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
        child: Text(_text),
      ),
    );
  }

  Future<String> getCompanyName() async {
    //Instantiating request body for request
    final Response response = await _scope
        .newRequestBuilder()
        .get('orgs/octokit')
        .withMapper((map) => Response.fromJson(map))
        .execute();
    return response.login;
  }
}

class LoginBody {
  String login;
  String password;
  String clientId;

  LoginBody({this.login, this.password, this.clientId});

  LoginBody.fromJson(Map<String, dynamic> json) {
    login = json['login'];
    password = json['password'];
    clientId = json['clientId'];
  }

  //needed to be converted to json inside RequestBuilder
  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['login'] = this.login;
    data['password'] = this.password;
    data['clientId'] = this.clientId;
    return data;
  }
}

class Response {
  final String login;
  final int id;
  final String nodeId;
  final String url;
  final String reposUrl;
  final String eventsUrl;
  final String hooksUrl;
  final String issuesUrl;
  final String membersUrl;
  final String publicMembersUrl;
  final String avatarUrl;
  final String description;
  final Null name;
  final Null company;
  final String blog;
  final Null location;
  final Null email;
  final bool isVerified;
  final bool hasOrganizationProjects;
  final bool hasRepositoryProjects;
  final int publicRepos;
  final int publicGists;
  final int followers;
  final int following;
  final String htmlUrl;
  final String createdAt;
  final String updatedAt;
  final String type;

  Response({this.login,
    this.id,
    this.nodeId,
    this.url,
    this.reposUrl,
    this.eventsUrl,
    this.hooksUrl,
    this.issuesUrl,
    this.membersUrl,
    this.publicMembersUrl,
    this.avatarUrl,
    this.description,
    this.name,
    this.company,
    this.blog,
    this.location,
    this.email,
    this.isVerified,
    this.hasOrganizationProjects,
    this.hasRepositoryProjects,
    this.publicRepos,
    this.publicGists,
    this.followers,
    this.following,
    this.htmlUrl,
    this.createdAt,
    this.updatedAt,
    this.type});

  Response.fromJson(Map<String, dynamic> json)
      :
        login = json['login'],
        id = json['id'],
        nodeId = json['node_id'],
        url = json['url'],
        reposUrl = json['repos_url'],
        eventsUrl = json['events_url'],
        hooksUrl = json['hooks_url'],
        issuesUrl = json['issues_url'],
        membersUrl = json['members_url'],
        publicMembersUrl = json['public_members_url'],
        avatarUrl = json['avatar_url'],
        description = json['description'],
        name = json['name'],
        company = json['company'],
        blog = json['blog'],
        location = json['location'],
        email = json['email'],
        isVerified = json['is_verified'],
        hasOrganizationProjects = json['has_organization_projects'],
        hasRepositoryProjects = json['has_repository_projects'],
        publicRepos = json['public_repos'],
        publicGists = json['public_gists'],
        followers = json['followers'],
        following = json['following'],
        htmlUrl = json['html_url'],
        createdAt = json['created_at'],
        updatedAt = json['updated_at'],
        type = json['type'];

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['login'] = this.login;
    data['id'] = this.id;
    data['node_id'] = this.nodeId;
    data['url'] = this.url;
    data['repos_url'] = this.reposUrl;
    data['events_url'] = this.eventsUrl;
    data['hooks_url'] = this.hooksUrl;
    data['issues_url'] = this.issuesUrl;
    data['members_url'] = this.membersUrl;
    data['public_members_url'] = this.publicMembersUrl;
    data['avatar_url'] = this.avatarUrl;
    data['description'] = this.description;
    data['name'] = this.name;
    data['company'] = this.company;
    data['blog'] = this.blog;
    data['location'] = this.location;
    data['email'] = this.email;
    data['is_verified'] = this.isVerified;
    data['has_organization_projects'] = this.hasOrganizationProjects;
    data['has_repository_projects'] = this.hasRepositoryProjects;
    data['public_repos'] = this.publicRepos;
    data['public_gists'] = this.publicGists;
    data['followers'] = this.followers;
    data['following'] = this.following;
    data['html_url'] = this.htmlUrl;
    data['created_at'] = this.createdAt;
    data['updated_at'] = this.updatedAt;
    data['type'] = this.type;
    return data;
  }
}

Use this package as a library

1. Depend on it

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


dependencies:
  restquester: ^0.6.0

2. Install it

You can install packages from the command line:

with Flutter:


$ flutter pub get

Alternatively, your editor might support 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:restquester/headers.dart';
import 'package:restquester/requester.dart';
import 'package:restquester/scope.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
31
Health:
Code health derived from static analysis. [more]
100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
80
Overall:
Weighted score of the above. [more]
62
Learn more about scoring.

We analyzed this package on Mar 31, 2020, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.7.1
  • pana: 0.13.6
  • Flutter: 1.12.13+hotfix.8

Health suggestions

Format lib/requester.dart.

Run flutter format to format lib/requester.dart.

Format lib/scope.dart.

Run flutter format to format lib/scope.dart.

Maintenance issues and suggestions

Homepage URL doesn't exist. (-20 points)

At the time of the analysis the homepage field https://stonks.pl was unreachable.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.1.0 <3.0.0
flutter 0.0.0
http ^0.12.0+2 0.12.0+4
Transitive dependencies
async 2.4.1
charcode 1.1.3
collection 1.14.11 1.14.12
http_parser 3.1.4
meta 1.1.8
path 1.6.4
pedantic 1.9.0
sky_engine 0.0.99
source_span 1.7.0
string_scanner 1.0.5
term_glyph 1.1.0
typed_data 1.1.6
vector_math 2.0.8
Dev dependencies
flutter_test