NAME
OpenAPI::Client - A client for talking to an Open API powered server
DESCRIPTION
OpenAPI::Client is a class for generating classes that can talk to an Open API server. This is done by generating a custom class, based on a Open API specification, with methods that transform parameters into a HTTP request.
The generated class will perform input validation, so invalid data won't be sent to the server.
Not that this implementation is currently EXPERIMENTAL! Feedback is appreciated.
SYNOPSIS
Open API specification
The specification given to "new" need to point to a valid OpenAPI document, in either JSON or YAML format. Example:
---
swagger: 2.0
host: api.example.com
basePath: /api
schemes: [ "http" ]
paths:
/foo:
get:
operationId: listPets
parameters:
- name: limit
in: query
type: integer
responses:
200: { ... }
host
, basePath
and the first item in schemes
will be used to construct "base_url". This can be altered at any time, if you need to send data to a custom endpoint.
Client
The OpenAPI API specification will be used to generate a sub-class of OpenAPI::Client where the "operationId", inside of each path definition, is used to generate methods:
use OpenAPI::Client;
$client = OpenAPI::Client->new("file:///path/to/api.json");
# Blocking
$tx = $client->listPets;
# Non-blocking
$client = $client->listPets(sub { my ($client, $tx) = @_; });
# With parameters
$tx = $client->listPets({limit => 10});
Customization
If you want to request a different server than what is specified in the Open API document:
$client->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. The default value comes from schemes
, basePath
and host
in the Open API specification.
ua
$ua = $self->ua;
Returns a Mojo::UserAgent object which is used to execute requests.
METHODS
local_app
$client = $client->local_app(Mojolicious->new);
This method will modify "ua" to run requests against the Mojolicious or Mojolicious::Lite application given as argument. (Useful for testing)
new
$client = OpenAPI::Client->new($specification, \%attrs);
$client = OpenAPI::Client->new($specification, %attrs);
$client = OpenAPI::Client->new($specification, app => Mojolicious->new);
Returns an object of a generated class, with methods generated from the Open API specification located at $specification
. See "schema" in JSON::Validator for valid versions of $specification
.
Note that the class is cached by perl, so loading a new specification from the same URL will not generate a new class.
Specifying an app
is useful when running against a local Mojolicious instance.
validator
$validator = $self->validator;
$validator = $class->validator;
Returns a JSON::Validator::OpenAPI::Mojolicious object for a generated class. Not that this is a global variable, so changing the object will affect all instances.
COPYRIGHT AND LICENSE
Copyright (C) 2017, 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