API-Client

 view release on metacpan or  search on metacpan

t/API_Client.t  view on Meta::CPAN

=method resource

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

=signature resource

resource(Str @segments) : Object

=example-1 resource

  # given: synopsis

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

=cut

=method serialize

The serialize method serializes and returns the object as a C<hashref>.

=signature serialize

serialize() : HashRef

=example-1 serialize

  # given: synopsis

  $client->serialize;

=cut

=method update

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

=signature update

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

=example-1 update

  # given: synopsis

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

=cut

package main;

use Mojo::UserAgent;

SKIP: {
  my $skip_tests = do {
    my $tx = Mojo::UserAgent->new->get('https://httpbin.org/anything');

    !eval{$tx->result->is_success};
  };

  unless ($skip_tests) {
    my $test = testauto(__FILE__);

    my $subs = $test->standard;

    $subs->synopsis(fun($tryable) {
      ok my $result = $tryable->result;

      $result
    });

    $subs->scenario('building', fun($tryable) {
      require Scalar::Util;
      ok my $result = $tryable->result;

      my $get = $result->[0];
      my $head = $result->[1];
      my $patch = $result->[2];

      isnt Scalar::Util::refaddr($get), Scalar::Util::refaddr($head);
      isnt Scalar::Util::refaddr($get), Scalar::Util::refaddr($patch);
      isnt Scalar::Util::refaddr($head), Scalar::Util::refaddr($patch);

      is $get->req->method, 'get';
      is $head->req->method, 'head';
      is $patch->req->method, 'patch';
    });

    $subs->scenario('chaining', fun($tryable) {
      require Scalar::Util;
      ok my $result = $tryable->result;

      my $users = $result->[0];
      my $user = $result->[1];
      my $new_user = $result->[2];

      isnt Scalar::Util::refaddr($users), Scalar::Util::refaddr($user);
      isnt Scalar::Util::refaddr($users), Scalar::Util::refaddr($new_user);
      isnt Scalar::Util::refaddr($user), Scalar::Util::refaddr($new_user);

      is $users->url->to_string, 'https://httpbin.org/users';
      is $user->url->to_string, 'https://httpbin.org/users/c09e91a';
      is $new_user->url->to_string, 'https://httpbin.org/users/c09e91a';
    });

    $subs->scenario('fetching', fun($tryable) {
      ok my $result = $tryable->result;

      ;
    });

    $subs->scenario('creating', fun($tryable) {
      ok my $result = $tryable->result;

      ;
    });

    $subs->scenario('updating', fun($tryable) {



( run in 0.842 second using v1.01-cache-2.11-cpan-98e64b0badf )