API-Client

 view release on metacpan or  search on metacpan

README.md  view on Meta::CPAN


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

    # is equivalent to

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

    [$tx1, $tx2]

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

## fetching

    # given: synopsis

    my $tx1 = $client->resource('get')->fetch(
      query => {active => 1}
    );

    # is equivalent to

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

    [$tx1, $tx2]

This example illustrates how you might fetch an API resource.

## subclassing

    package Hookbin;

    use Data::Object::Class;

    extends 'API::Client';

    sub auth {
      ['admin', 'secret']
    }

    sub headers {
      [['Accept', '*/*']]
    }

    sub base {
      ['https://httpbin.org/get']
    }

    package main;

    my $hookbin = Hookbin->new;

This package was designed to be subclassed and provides hooks into the client
building and request dispatching processes. Specifically, there are three
useful hooks (i.e. methods, which if present are used to build up the client
object and requests), which are, the `auth` hook, which should return a
`Tuple[Str, Str]` which is used to configure the basic auth header, the
`base` hook which should return a `Tuple[Str]` which is used to configure the
base URL, and the `headers` hook, which should return a
`ArrayRef[Tuple[Str, Str]]` which are used to configure the HTTP request
headers.

## transacting

    # given: synopsis

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

    # is equivalent to

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

    [$tx1, $tx2]

An HTTP request is only issued when the ["dispatch"](#dispatch) method is called, directly
or indirectly. Those calls return a [Mojo::Transaction](https://metacpan.org/pod/Mojo::Transaction) object which provides
access to the `request` and `response` objects.

## updating

    # given: synopsis

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

    # is equivalent to

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

    [$tx1, $tx2]

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

# ATTRIBUTES

This package has the following attributes:

## debug

    debug(Bool)

This attribute is read-only, accepts `(Bool)` values, and is optional.

## fatal

README.md  view on Meta::CPAN


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

- dispatch example #8

        # given: synopsis

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

## fetch

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

The fetch method issues a `GET` request to the API resource represented by the
object.

- fetch example #1

        # given: synopsis

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

## patch

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

The patch method issues a `PATCH` request to the API resource represented by
the object.

- patch example #1

        # given: synopsis

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

## prepare

    prepare(Object $ua, Object $tx, Any %args) : Object

The prepare method acts as a `before` hook triggered before each request where
you can modify the transactor objects.

- prepare example #1

        # given: synopsis

        require Mojo::UserAgent;
        require Mojo::Transaction::HTTP;

        $client->prepare(
          Mojo::UserAgent->new,
          Mojo::Transaction::HTTP->new
        );

## process

    process(Object $ua, Object $tx, Any %args) : Object

The process method acts as an `after` hook triggered after each response where
you can modify the transactor objects.

- process example #1

        # given: synopsis

        require Mojo::UserAgent;
        require Mojo::Transaction::HTTP;

        $client->process(
          Mojo::UserAgent->new,
          Mojo::Transaction::HTTP->new
        );

## resource

    resource(Str @segments) : Object

The resource method returns a new instance of the object for the API resource
endpoint specified.

- resource example #1

        # given: synopsis

        $client->resource('status', 200);

## serialize

    serialize() : HashRef

The serialize method serializes and returns the object as a `hashref`.

- serialize example #1

        # given: synopsis

        $client->serialize;

## update

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

The update method issues a `PUT` request to the API resource represented by
the object.

- update example #1

        # given: synopsis

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

# AUTHOR

Al Newkirk, `awncorp@cpan.org`

# LICENSE

Copyright (C) 2011-2019, Al Newkirk, et al.

This is free software; you can redistribute it and/or modify it under the terms
of the The Apache License, Version 2.0, as elucidated in the ["license
file"](https://github.com/iamalnewkirk/api-client/blob/master/LICENSE).

# PROJECT

[Wiki](https://github.com/iamalnewkirk/api-client/wiki)



( run in 1.502 second using v1.01-cache-2.11-cpan-140bd7fdf52 )