API-Client

 view release on metacpan or  search on metacpan

LICENSE  view on Meta::CPAN


2. You may apply bug fixes, portability fixes and other modifications derived
from the Public Domain or from the Copyright Holder. A Package modified in such
a way shall still be considered the Standard Version.

3. You may otherwise modify your copy of this Package in any way, provided that
you insert a prominent notice in each changed file stating how and when you
changed that file, and provided that you do at least ONE of the following:

  a) place your modifications in the Public Domain or otherwise make them
     Freely Available, such as by posting said modifications to Usenet or an
     equivalent medium, or placing the modifications on a major archive site
     such as ftp.uu.net, or by allowing the Copyright Holder to include your
     modifications in the Standard Version of the Package.

  b) use the modified Package only within your corporation or organization.

  c) rename any non-standard executables so the names do not conflict with
     standard executables, which must also be provided, and provide a separate
     manual page for each non-standard executable that clearly documents how it
     differs from the Standard Version.

README  view on Meta::CPAN

    HTTP API Thin-Client Abstraction

SYNOPSIS

      package main;
    
      use API::Client;
    
      my $client = API::Client->new(url => 'https://httpbin.org');
    
      # $client->resource('post');
    
      # $client->update(json => {...});

DESCRIPTION

    This package provides an abstraction and method for rapidly developing
    HTTP API clients. While this module can be used to interact with APIs
    directly, API::Client was designed to be consumed (subclassed) by
    higher-level purpose-specific API clients.

README  view on Meta::CPAN


    Because each call to "resource" returns a new object instance
    configured with a path (resource locator) based on the supplied
    parameters, reuse and request isolation are made simple, i.e., you will
    only need to configure the client once in your application.

 creating

      # given: synopsis
    
      my $tx1 = $client->resource('post')->create(
        json => {active => 1}
      );
    
      # is equivalent to
    
      my $tx2 = $client->resource('post')->dispatch(
        method => 'post',
        json => {active => 1}
      );
    
      [$tx1, $tx2]

    This example illustrates how you might create a new API resource.

 deleting

      # given: synopsis

README  view on Meta::CPAN


      create(Any %args) : InstanceOf["Mojo::Transaction"]

    The create method issues a POST request to the API resource represented
    by the object.

    create example #1

        # given: synopsis
      
        $client->resource('post')->create(
          json => {active => 1}
        );

 delete

      delete(Any %args) : InstanceOf["Mojo::Transaction"]

    The delete method issues a DELETE request to the API resource
    represented by the object.

README  view on Meta::CPAN

    dispatch example #1

        # given: synopsis
      
        $client->resource('get')->dispatch;

    dispatch example #2

        # given: synopsis
      
        $client->resource('post')->dispatch(
          method => 'post', body => 'active=1'
        );

    dispatch example #3

        # given: synopsis
      
        $client->resource('get')->dispatch(
          method => 'get', query => {active => 1}
        );

    dispatch example #4

        # given: synopsis
      
        $client->resource('post')->dispatch(
          method => 'post', json => {active => 1}
        );

    dispatch example #5

        # given: synopsis
      
        $client->resource('post')->dispatch(
          method => 'post', form => {active => 1}
        );

    dispatch example #6

        # given: synopsis
      
        $client->resource('put')->dispatch(
          method => 'put', json => {active => 1}
        );

README.md  view on Meta::CPAN

HTTP API Thin-Client Abstraction

# SYNOPSIS

    package main;

    use API::Client;

    my $client = API::Client->new(url => 'https://httpbin.org');

    # $client->resource('post');

    # $client->update(json => {...});

# DESCRIPTION

This package provides an abstraction and method for rapidly developing HTTP API
clients. While this module can be used to interact with APIs directly,
API::Client was designed to be consumed (subclassed) by higher-level
purpose-specific API clients.

README.md  view on Meta::CPAN


Because each call to ["resource"](#resource) returns a new object instance configured with
a path (resource locator) based on the supplied parameters, reuse and request
isolation are made simple, i.e., you will only need to configure the client
once in your application.

## creating

    # given: synopsis

    my $tx1 = $client->resource('post')->create(
      json => {active => 1}
    );

    # is equivalent to

    my $tx2 = $client->resource('post')->dispatch(
      method => 'post',
      json => {active => 1}
    );

    [$tx1, $tx2]

This example illustrates how you might create a new API resource.

## deleting

    # given: synopsis

README.md  view on Meta::CPAN


    create(Any %args) : InstanceOf["Mojo::Transaction"]

The create method issues a `POST` request to the API resource represented by
the object.

- create example #1

        # given: synopsis

        $client->resource('post')->create(
          json => {active => 1}
        );

## delete

    delete(Any %args) : InstanceOf["Mojo::Transaction"]

The delete method issues a `DELETE` request to the API resource represented by
the object.

README.md  view on Meta::CPAN

- dispatch example #1

        # given: synopsis

        $client->resource('get')->dispatch;

- dispatch example #2

        # given: synopsis

        $client->resource('post')->dispatch(
          method => 'post', body => 'active=1'
        );

- dispatch example #3

        # given: synopsis

        $client->resource('get')->dispatch(
          method => 'get', query => {active => 1}
        );

- dispatch example #4

        # given: synopsis

        $client->resource('post')->dispatch(
          method => 'post', json => {active => 1}
        );

- dispatch example #5

        # given: synopsis

        $client->resource('post')->dispatch(
          method => 'post', form => {active => 1}
        );

- dispatch example #6

        # given: synopsis

        $client->resource('put')->dispatch(
          method => 'put', json => {active => 1}
        );

lib/API/Client.pm  view on Meta::CPAN

    $self->{url} = Mojo::URL->new(join('/', @{$self->base($args)}));
  }

  return $self;
}

# METHODS

method create(Any %args) {

  return $self->dispatch(%args, method => 'post');
}

method delete(Any %args) {

  return $self->dispatch(%args, method => 'delete');
}

method dispatch(Str :$method = 'get', Any %args) {
  my $log = $self->logger->info("@{[uc($method)]} @{[$self->url->to_string]}");

lib/API/Client.pm  view on Meta::CPAN

=cut

=head1 SYNOPSIS

  package main;

  use API::Client;

  my $client = API::Client->new(url => 'https://httpbin.org');

  # $client->resource('post');

  # $client->update(json => {...});

=cut

=head1 DESCRIPTION

This package provides an abstraction and method for rapidly developing HTTP API
clients. While this module can be used to interact with APIs directly,
API::Client was designed to be consumed (subclassed) by higher-level

lib/API/Client.pm  view on Meta::CPAN

a path (resource locator) based on the supplied parameters, reuse and request
isolation are made simple, i.e., you will only need to configure the client
once in your application.

=cut

=head2 creating

  # given: synopsis

  my $tx1 = $client->resource('post')->create(
    json => {active => 1}
  );

  # is equivalent to

  my $tx2 = $client->resource('post')->dispatch(
    method => 'post',
    json => {active => 1}
  );

  [$tx1, $tx2]

This example illustrates how you might create a new API resource.

=cut

=head2 deleting

lib/API/Client.pm  view on Meta::CPAN


The create method issues a C<POST> request to the API resource represented by
the object.

=over 4

=item create example #1

  # given: synopsis

  $client->resource('post')->create(
    json => {active => 1}
  );

=back

=cut

=head2 delete

  delete(Any %args) : InstanceOf["Mojo::Transaction"]

lib/API/Client.pm  view on Meta::CPAN

  $client->resource('get')->dispatch;

=back

=over 4

=item dispatch example #2

  # given: synopsis

  $client->resource('post')->dispatch(
    method => 'post', body => 'active=1'
  );

=back

=over 4

=item dispatch example #3

  # given: synopsis

lib/API/Client.pm  view on Meta::CPAN

  );

=back

=over 4

=item dispatch example #4

  # given: synopsis

  $client->resource('post')->dispatch(
    method => 'post', json => {active => 1}
  );

=back

=over 4

=item dispatch example #5

  # given: synopsis

  $client->resource('post')->dispatch(
    method => 'post', form => {active => 1}
  );

=back

=over 4

=item dispatch example #6

  # given: synopsis

t/API_Client.t  view on Meta::CPAN

=cut

=synopsis

  package main;

  use API::Client;

  my $client = API::Client->new(url => 'https://httpbin.org');

  # $client->resource('post');

  # $client->update(json => {...});

=cut

=libraries

Types::Standard

=cut

t/API_Client.t  view on Meta::CPAN

=cut

=scenario creating

This example illustrates how you might create a new API resource.

=example creating

  # given: synopsis

  my $tx1 = $client->resource('post')->create(
    json => {active => 1}
  );

  # is equivalent to

  my $tx2 = $client->resource('post')->dispatch(
    method => 'post',
    json => {active => 1}
  );

  [$tx1, $tx2]

=cut

=scenario updating

This example illustrates how you might update a new API resource.

t/API_Client.t  view on Meta::CPAN

=example-1 dispatch

  # given: synopsis

  $client->resource('get')->dispatch;

=example-2 dispatch

  # given: synopsis

  $client->resource('post')->dispatch(
    method => 'post', body => 'active=1'
  );

=example-3 dispatch

  # given: synopsis

  $client->resource('get')->dispatch(
    method => 'get', query => {active => 1}
  );

=example-4 dispatch

  # given: synopsis

  $client->resource('post')->dispatch(
    method => 'post', json => {active => 1}
  );

=example-5 dispatch

  # given: synopsis

  $client->resource('post')->dispatch(
    method => 'post', form => {active => 1}
  );

=example-6 dispatch

  # given: synopsis

  $client->resource('put')->dispatch(
    method => 'put', json => {active => 1}
  );

t/API_Client.t  view on Meta::CPAN

the object.

=signature create

create(Any %args) : InstanceOf["Mojo::Transaction"]

=example-1 create

  # given: synopsis

  $client->resource('post')->create(
    json => {active => 1}
  );

=cut

=method delete

The delete method issues a C<DELETE> request to the API resource represented by
the object.

t/API_Client.t  view on Meta::CPAN

      is_deeply $result->headers, [['Accept', '*/*']];
      is_deeply $result->base, ['https://httpbin.org/get'];
      is $result->url->to_string, 'https://httpbin.org/get';
      is $result->name, 'Hookbin (0.01)';
    });

    $subs->example(-1, 'create', 'method', fun($tryable) {
      ok my $result = $tryable->result;

      my $req = $result->req;
      is lc($req->method), 'post';

      my $res = $result->res;
      is $res->code, 200;

      my $json = $res->json;
      is $json->{headers}{'Host'}, 'httpbin.org';
      is $json->{headers}{'Content-Type'}, 'application/json';
      is_deeply $json->{json}, {active => 1};

      $result

t/API_Client.t  view on Meta::CPAN

      is $json->{headers}{'Content-Type'}, 'application/json';
      is_deeply $json->{args}, {};

      $result
    });

    $subs->example(-2, 'dispatch', 'method', fun($tryable) {
      ok my $result = $tryable->result;

      my $req = $result->req;
      is lc($req->method), 'post';

      my $res = $result->res;
      is $res->code, 200;

      my $json = $res->json;
      is $json->{headers}{'Host'}, 'httpbin.org';
      is $json->{headers}{'Content-Type'}, 'application/json';
      is $json->{data}, "active=1";

      $result

t/API_Client.t  view on Meta::CPAN

      is $json->{headers}{'Content-Type'}, 'application/json';
      is_deeply $json->{args}, {active => 1};

      $result
    });

    $subs->example(-4, 'dispatch', 'method', fun($tryable) {
      ok my $result = $tryable->result;

      my $req = $result->req;
      is lc($req->method), 'post';

      my $res = $result->res;
      is $res->code, 200;

      my $json = $res->json;
      is $json->{headers}{'Host'}, 'httpbin.org';
      is $json->{headers}{'Content-Type'}, 'application/json';
      is_deeply $json->{json}, {active => 1};

      $result
    });

    $subs->example(-5, 'dispatch', 'method', fun($tryable) {
      ok my $result = $tryable->result;

      my $req = $result->req;
      is lc($req->method), 'post';

      my $res = $result->res;
      is $res->code, 200;

      my $json = $res->json;
      is $json->{headers}{'Host'}, 'httpbin.org';
      is $json->{headers}{'Content-Type'}, 'application/json';
      is $json->{data}, "active=1";

      $result



( run in 0.499 second using v1.01-cache-2.11-cpan-acf6aa7dc9e )