PHPackages                             allansun/openapi-runtime - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [API Development](/categories/api)
4. /
5. allansun/openapi-runtime

ActiveLibrary[API Development](/categories/api)

allansun/openapi-runtime
========================

Runtime library to be used with other SDK generated from OpenAPI docs

3.2.3(3y ago)15.6k120MITPHPPHP ^7.4|^8.0

Since Mar 9Pushed 3y ago4 watchersCompare

[ Source](https://github.com/allansun/openapi-runtime)[ Packagist](https://packagist.org/packages/allansun/openapi-runtime)[ RSS](/packages/allansun-openapi-runtime/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependencies (16)Versions (30)Used By (20)

OpenAPI Client Runtime
======================

[](#openapi-client-runtime)

[![Latest Stable Version](https://camo.githubusercontent.com/8120fc2df4eea69aba919cf39776f1a26858db8e61537a30cc17d16b1a92d747/68747470733a2f2f706f7365722e707567782e6f72672f616c6c616e73756e2f6f70656e6170692d72756e74696d652f76)](https://packagist.org/packages/allansun/openapi-runtime)[![Total Downloads](https://camo.githubusercontent.com/447f99277c959a1d6e9b18a26039e1c69bdaf66e98a391a00ae97cf0dbfa9f9f/68747470733a2f2f706f7365722e707567782e6f72672f616c6c616e73756e2f6f70656e6170692d72756e74696d652f646f776e6c6f616473)](https://packagist.org/packages/allansun/openapi-runtime)[![License](https://camo.githubusercontent.com/341815ca96d7b188845cb6e01c956af1cb28d5bcbe985c1436c84fede3cffe37/68747470733a2f2f706f7365722e707567782e6f72672f616c6c616e73756e2f6f70656e6170692d72756e74696d652f6c6963656e7365)](https://packagist.org/packages/allansun/openapi-runtime)[![codecov](https://camo.githubusercontent.com/1dd0d16bffae64a610a52cc157b4122f9be15a6ce6fee06bb5d3b9e257bb96d4/68747470733a2f2f636f6465636f762e696f2f67682f616c6c616e73756e2f6f70656e6170692d72756e74696d652f6272616e63682f6d61737465722f67726170682f62616467652e7376673f746f6b656e3d4f565952585053573259)](https://codecov.io/gh/allansun/openapi-runtime)

Runtime library to be used with other SDK generated from OpenAPI docs.

Installation
------------

[](#installation)

```
composer require allansun/openapi-runtime
```

You will also need a [PSR-18 compatible client](https://www.php-fig.org/psr/psr-18/) see

So either use Guzzle (or any other PSR-18 compatible clients)

```
composer require php-http/guzzle7-adapter
```

Basic concepts
--------------

[](#basic-concepts)

OpenAPI (formally Swagger) defines how API should be interacted with by giving

- API endpoint URI
- input payload and output response (normally in JSON format)

Code generators can generate valid code by parsing the OpenAPI doc.

We try to provide a guidance on how you should organize your code generation.

### ResponseHandlers

[](#responsehandlers)

One key function this lib provides is to transform response JSON into predefined PHP objects.

By calling Client::configure() you can customize your own [ResponseHanderStack](/src/ResponseHandlerStack/ResponseHandlerStack.php) , which basically is a stack of transformers to parse the response JSON.

You can create your own ResponseHandler by implementing the [ResponseHandlerInterface](/src/ResponseHandler/ResponseHandlerInterface.php). It is simply an invokable class either returns a [Model](/src/ModelInterface.php) or throws an [UndefinedResponseException](/src/ResponseHandler/Exception/UndefinedResponseException.php) so the ResponseHandlerStack can try next handler.

By default, we provide a simple JSON response handler( [JsonPsrResponseHandler](/src/ResponseHandler/JsonResponseHandler.php). It will try to parse the response into a model by looking into reference, which is defined in [ReponseTypes](/src/ResponseTypes.php). Be aware you should set up your response references by calling ResponseTypes::setTypes().

Usage
-----

[](#usage)

First your generated code should provide a way to parse response references into ResponseTypes, or you can create your own ResponseTypes class and inject into a Handler then into a HandlerStack

```
