Swagger2::Client - A client for talking to a Swagger powered server
See Swagger2.
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.
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: { ... }
The swagger specification will the be turned into a sub class of Swagger2::Client, where the "parameters" rules are used to do input validation.
use Swagger2::Client; $ua = Swagger2::Client->generate("file:///path/to/api.json"); # blocking (will croak() on error) $pets = $ua->listPets; # blocking (will not croak() on error) $ua->return_on_error(1); $pets = $ua->listPets; # non-blocking $ua = $ua->listPets(sub { my ($ua, $err, $pets) = @_; }); # with arguments, where the key map to the "parameters" name $pets = $ua->listPets({limit => 10});
The method name added will both be the original operationId, but a "snake case" version will also be added. Example:
operationId
"operationId": "listPets" => $client->listPets() => $client->list_pets()
If you want to request a different server than what is specified in the swagger document:
$ua->base_url->host("other.server.com");
$base_url = $self->base_url;
Returns a Mojo::URL object with the base URL to the API.
$ua = $self->ua;
Returns a Mojo::UserAgent object which is used to execute requests.
$client = Swagger2::Client->generate(Swagger2->new($specification_url)); $client = Swagger2::Client->generate($specification_url);
Returns an object of a generated class, with the rules from the $specification_url.
$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 (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.
Jan Henning Thorsen - jhthorsen@cpan.org
jhthorsen@cpan.org
To install Swagger2, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Swagger2
CPAN shell
perl -MCPAN -e shell install Swagger2
For more information on module installation, please visit the detailed CPAN module installation guide.