An open API service indexing awesome lists of open source software.

https://github.com/remarkablemark/janephp-template

🌱 JanePHP template
https://github.com/remarkablemark/janephp-template

composer janephp openapi petstore petstore-api php php-library php-sdk swagger template

Last synced: 12 months ago
JSON representation

🌱 JanePHP template

Awesome Lists containing this project

README

          

# janephp-template

[![packagist](https://img.shields.io/packagist/v/petstore/sdk)](https://packagist.org/packages/petstore/sdk)
[![build](https://github.com/remarkablemark/janephp-template/actions/workflows/build.yml/badge.svg)](https://github.com/remarkablemark/janephp-template/actions/workflows/build.yml)
[![test](https://github.com/remarkablemark/janephp-template/actions/workflows/test.yml/badge.svg)](https://github.com/remarkablemark/janephp-template/actions/workflows/test.yml)

PHP library for [Petstore](https://petstore3.swagger.io/). Code is generated using the [OpenAPI spec](https://petstore3.swagger.io/api/v3/openapi.json).

## Requirements

### Version

PHP >=8.0

## Install

Install with [Composer](http://getcomposer.org/):

```sh
composer require petstore/sdk
```

Use Composer's [autoload](https://getcomposer.org/doc/01-basic-usage.md#autoloading):

```php
require_once 'vendor/autoload.php';
```

## Usage

Instantiate the client:

```php
use Petstore\Petstore;

$petstore = new Petstore('YOUR_API_KEY', 'YOUR_API_URL');

$client = $petstore->client;
```

Or create a custom client:

```php
use Petstore\Client;

$httpClient = \Http\Discovery\Psr18ClientDiscovery::find();
$uri = \Http\Discovery\Psr17FactoryDiscovery::findUriFactory()->createUri('YOUR_API_URL');
$bearer = new \Http\Message\Authentication\Bearer('YOUR_API_KEY');
$plugins = [
new \Http\Client\Common\Plugin\AddHostPlugin($uri),
new \Http\Client\Common\Plugin\AuthenticationPlugin($bearer),
];
$httpClient = new \Http\Client\Common\PluginClient($httpClient, $plugins);

$client = Client::create($httpClient);
```

Add a pet:

```php
use Petstore\Petstore;
use Petstore\Model\Pet;

$petstore = new Petstore('YOUR_API_KEY');

$pet = new Pet();
$pet->setName('Neo');
$pet->setPhotoUrls(['https://placecats.com/neo/300/200']);
$response = $petstore->client->addPet($pet);
```

Handle an API error:

```php
use Petstore\Petstore;

$petstore = new Petstore('YOUR_API_KEY');

try {
$petstore->client->getPetById(0);
} catch (Throwable $exception) {
echo $exception->getMessage();
echo $exception->getCode();
}
```

## Scripts

### `composer build`

Generate the code:

```sh
composer build
```

### `composer clean`

Delete the `lib/` directory:

```sh
composer clean
```

### `composer test`

Run the tests:

```sh
composer test
```

## About This Package

This package is automatically generated by [Jane](https://github.com/janephp/janephp).