---
name: "Swagger2::Client"
section: "3pm"
platform: "linux"
package: "libswagger2-perl"
version: "0.77-1"
release: "Debian 9 (Stretch)"
platforms: ["linux"]
url: "https://new.carta.tech/Swagger2::Client"
---

# Swagger2::Client(3pm)

A client for talking to a Swagger powered server

## 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 \<https://github.com/swagger-api/swagger-spec/blob/master/versions/2.0.md\> 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.

```
  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": "listPets"
    => $client->listPets()
    => $client->list_pets()
```

### 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(Swagger2->new($specification_url));
  $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"`
