The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

Swagger2::Client - A client for talking to a Swagger powered server

DESCRIPTION

Swagger2::Client is a base class for autogenerated classes that can talk to a server using a swagger specification.

Note that this is a DRAFT, so there will probably be bugs and changes.

SYNOPSIS

Swagger specification

The input "url" given to "generate" need to point to a valid swagger document.

  ---
  swagger: 2.0
  basePath: /api
  paths:
    /foo:
      get:
        operationId: listPets
        parameters:
        - name: limit
          in: query
          type: integer
        responses:
          200: { ... }

Client

The swagger specification will the be turned into a sub class of Swagger2::Client, where the "parameters" rules are used to do input validation.

The method name added is a decamelized version of the operationId, which creates a more perl-ish feeling to the API.

  use Swagger2::Client;
  my $ua = Swagger2::Client->generate("file:///path/to/api.json");

  # blocking
  my $pets = $ua->list_pets; # instead of listPets()

  # non-blocking
  $ua = $ua->list_pets(sub { my ($ua, $err, $pets) = @_; });

  # with arguments, where the key map to the "parameters" name
  my $pets = $ua->list_pets({limit => 10});

Customization

If you want to request a different server than what is specified in the swagger document:

  $ua->base_url->host("other.server.com");

ATTRIBUTES

base_url

  $base_url = $self->base_url;

Returns a Mojo::URL object with the base URL to the API.

ua

  $ua = $self->ua;

Returns a Mojo::UserAgent object which is used to execute requests.

METHODS

generate

  $client = Swagger2::Client->generate($specification_url);

Returns an object of a generated class, with the rules from the $specification_url.

Note that the class is cached by perl, so loading a new specification from the same URL will not generate a new class.

COPYRIGHT AND LICENSE

Copyright (C) 2014-2015, Jan Henning Thorsen

This program is free software, you can redistribute it and/or modify it under the terms of the Artistic License version 2.0.

AUTHOR

Jan Henning Thorsen - jhthorsen@cpan.org