SDKgen is a powerful code generator to automatically build client SDKs for your REST API.

Sign in   Sign up

Code

At the following page you can take a look at the generated code, the code is based on this TypeAPI specification. This is only a demo to give you a first insight, for live examples feel free to login at the backend and use the live generator.


Client.php

<?php
/**
 * Client automatically generated by SDKgen please do not edit this file manually
 * @see https://sdkgen.app
 */

namespace SDK;

use GuzzleHttp\Exception\BadResponseException;
use Sdkgen\Client\ClientAbstract;
use Sdkgen\Client\Credentials;
use Sdkgen\Client\CredentialsInterface;
use Sdkgen\Client\Exception\ClientException;
use Sdkgen\Client\Exception\UnknownStatusCodeException;
use Sdkgen\Client\TokenStoreInterface;

class Client extends ClientAbstract
{
    public function test(): TestTag
    {
        return new TestTag(
            $this->httpClient,
            $this->parser
        );
    }



    public static function build(CredentialsInterface $credentials): self
    {
        return new self('https://api.sdkgen.app/', $credentials);
    }
}

Error.php

<?php
/**
 * Error automatically generated by SDKgen please do not edit this file manually
 * @see https://sdkgen.app
 */

namespace SDK;


class Error implements \JsonSerializable, \PSX\Record\RecordableInterface
{
    protected ?bool $success = null;
    protected ?string $message = null;
    public function setSuccess(?bool $success) : void
    {
        $this->success = $success;
    }
    public function getSuccess() : ?bool
    {
        return $this->success;
    }
    public function setMessage(?string $message) : void
    {
        $this->message = $message;
    }
    public function getMessage() : ?string
    {
        return $this->message;
    }
    public function toRecord() : \PSX\Record\RecordInterface
    {
        /** @var \PSX\Record\Record<mixed> $record */
        $record = new \PSX\Record\Record();
        $record->put('success', $this->success);
        $record->put('message', $this->message);
        return $record;
    }
    public function jsonSerialize() : object
    {
        return (object) $this->toRecord()->getAll();
    }
}

ErrorException.php

<?php
/**
 * ErrorException automatically generated by SDKgen please do not edit this file manually
 * @see https://sdkgen.app
 */

namespace SDK;

use Sdkgen\Client\Exception\KnownStatusCodeException;

class ErrorException extends KnownStatusCodeException
{
    private Error $payload;

    public function __construct(Error $payload)
    {
        parent::__construct('The server returned an error');

        $this->payload = $payload;
    }

    /**
     * @return Error
     */
    public function getPayload(): Error
    {
        return $this->payload;
    }
}

HelloWorld.php

<?php
/**
 * HelloWorld automatically generated by SDKgen please do not edit this file manually
 * @see https://sdkgen.app
 */

namespace SDK;


class HelloWorld implements \JsonSerializable, \PSX\Record\RecordableInterface
{
    protected ?string $message = null;
    public function setMessage(?string $message) : void
    {
        $this->message = $message;
    }
    public function getMessage() : ?string
    {
        return $this->message;
    }
    public function toRecord() : \PSX\Record\RecordInterface
    {
        /** @var \PSX\Record\Record<mixed> $record */
        $record = new \PSX\Record\Record();
        $record->put('message', $this->message);
        return $record;
    }
    public function jsonSerialize() : object
    {
        return (object) $this->toRecord()->getAll();
    }
}

Response.php

<?php
/**
 * Response automatically generated by SDKgen please do not edit this file manually
 * @see https://sdkgen.app
 */

namespace SDK;


class Response implements \JsonSerializable, \PSX\Record\RecordableInterface
{
    protected ?bool $success = null;
    protected ?string $message = null;
    public function setSuccess(?bool $success) : void
    {
        $this->success = $success;
    }
    public function getSuccess() : ?bool
    {
        return $this->success;
    }
    public function setMessage(?string $message) : void
    {
        $this->message = $message;
    }
    public function getMessage() : ?string
    {
        return $this->message;
    }
    public function toRecord() : \PSX\Record\RecordInterface
    {
        /** @var \PSX\Record\Record<mixed> $record */
        $record = new \PSX\Record\Record();
        $record->put('success', $this->success);
        $record->put('message', $this->message);
        return $record;
    }
    public function jsonSerialize() : object
    {
        return (object) $this->toRecord()->getAll();
    }
}

TestTag.php

<?php
/**
 * TestTag automatically generated by SDKgen please do not edit this file manually
 * @see https://sdkgen.app
 */

namespace SDK;

use GuzzleHttp\Exception\BadResponseException;
use Sdkgen\Client\Exception\ClientException;
use Sdkgen\Client\Exception\UnknownStatusCodeException;
use Sdkgen\Client\TagAbstract;

class TestTag extends TagAbstract
{
    /**
     * Returns a hello world message
     *
     * @return HelloWorld
     * @throws ClientException
     */
    public function getHello(): HelloWorld
    {
        $url = $this->parser->url('/hello/world', [
        ]);

        $options = [
            'query' => $this->parser->query([
            ], [
            ]),
        ];

        try {
            $response = $this->httpClient->request('GET', $url, $options);
            $data = (string) $response->getBody();

            return $this->parser->parse($data, HelloWorld::class);
        } catch (ClientException $e) {
            throw $e;
        } catch (BadResponseException $e) {
            $data = (string) $e->getResponse()->getBody();

            switch ($e->getResponse()->getStatusCode()) {
                default:
                    throw new UnknownStatusCodeException('The server returned an unknown status code');
            }
        } catch (\Throwable $e) {
            throw new ClientException('An unknown error occurred: ' . $e->getMessage());
        }
    }

    /**
     * Returns available todo entries
     *
     * @param int|null $startIndex
     * @param int|null $count
     * @return Todos
     * @throws ClientException
     */
    public function getEntries(?int $startIndex = null, ?int $count = null): Todos
    {
        $url = $this->parser->url('/todo', [
        ]);

        $options = [
            'query' => $this->parser->query([
                'startIndex' => $startIndex,
                'count' => $count,
            ], [
            ]),
        ];

        try {
            $response = $this->httpClient->request('GET', $url, $options);
            $data = (string) $response->getBody();

            return $this->parser->parse($data, Todos::class);
        } catch (ClientException $e) {
            throw $e;
        } catch (BadResponseException $e) {
            $data = (string) $e->getResponse()->getBody();

            switch ($e->getResponse()->getStatusCode()) {
                default:
                    throw new UnknownStatusCodeException('The server returned an unknown status code');
            }
        } catch (\Throwable $e) {
            throw new ClientException('An unknown error occurred: ' . $e->getMessage());
        }
    }

    /**
     * Inserts a new todo entry
     *
     * @param Todo $payload
     * @return Response
     * @throws ClientException
     */
    public function insert(Todo $payload): Response
    {
        $url = $this->parser->url('/todo', [
        ]);

        $options = [
            'query' => $this->parser->query([
            ], [
            ]),
            'json' => $payload
        ];

        try {
            $response = $this->httpClient->request('POST', $url, $options);
            $data = (string) $response->getBody();

            return $this->parser->parse($data, Response::class);
        } catch (ClientException $e) {
            throw $e;
        } catch (BadResponseException $e) {
            $data = (string) $e->getResponse()->getBody();

            switch ($e->getResponse()->getStatusCode()) {
                default:
                    throw new UnknownStatusCodeException('The server returned an unknown status code');
            }
        } catch (\Throwable $e) {
            throw new ClientException('An unknown error occurred: ' . $e->getMessage());
        }
    }

    /**
     * Returns a hello world message
     *
     * @return HelloWorld
     * @throws ErrorException
     * @throws ClientException
     */
    public function throwException(): HelloWorld
    {
        $url = $this->parser->url('/exception', [
        ]);

        $options = [
            'query' => $this->parser->query([
            ], [
            ]),
        ];

        try {
            $response = $this->httpClient->request('GET', $url, $options);
            $data = (string) $response->getBody();

            return $this->parser->parse($data, HelloWorld::class);
        } catch (ClientException $e) {
            throw $e;
        } catch (BadResponseException $e) {
            $data = (string) $e->getResponse()->getBody();

            switch ($e->getResponse()->getStatusCode()) {
                case 500:
                    throw new ErrorException($this->parser->parse($data, Error::class));
                default:
                    throw new UnknownStatusCodeException('The server returned an unknown status code');
            }
        } catch (\Throwable $e) {
            throw new ClientException('An unknown error occurred: ' . $e->getMessage());
        }
    }


}

Todo.php

<?php
/**
 * Todo automatically generated by SDKgen please do not edit this file manually
 * @see https://sdkgen.app
 */

namespace SDK;


class Todo implements \JsonSerializable, \PSX\Record\RecordableInterface
{
    protected ?string $title = null;
    public function setTitle(?string $title) : void
    {
        $this->title = $title;
    }
    public function getTitle() : ?string
    {
        return $this->title;
    }
    public function toRecord() : \PSX\Record\RecordInterface
    {
        /** @var \PSX\Record\Record<mixed> $record */
        $record = new \PSX\Record\Record();
        $record->put('title', $this->title);
        return $record;
    }
    public function jsonSerialize() : object
    {
        return (object) $this->toRecord()->getAll();
    }
}

Todos.php

<?php
/**
 * Todos automatically generated by SDKgen please do not edit this file manually
 * @see https://sdkgen.app
 */

namespace SDK;


class Todos implements \JsonSerializable, \PSX\Record\RecordableInterface
{
    protected ?int $totalResults = null;
    protected ?int $startIndex = null;
    protected ?int $itemsPerPage = null;
    /**
     * @var array<Todo>|null
     */
    protected ?array $entry = null;
    public function setTotalResults(?int $totalResults) : void
    {
        $this->totalResults = $totalResults;
    }
    public function getTotalResults() : ?int
    {
        return $this->totalResults;
    }
    public function setStartIndex(?int $startIndex) : void
    {
        $this->startIndex = $startIndex;
    }
    public function getStartIndex() : ?int
    {
        return $this->startIndex;
    }
    public function setItemsPerPage(?int $itemsPerPage) : void
    {
        $this->itemsPerPage = $itemsPerPage;
    }
    public function getItemsPerPage() : ?int
    {
        return $this->itemsPerPage;
    }
    /**
     * @param array<Todo>|null $entry
     */
    public function setEntry(?array $entry) : void
    {
        $this->entry = $entry;
    }
    /**
     * @return array<Todo>|null
     */
    public function getEntry() : ?array
    {
        return $this->entry;
    }
    public function toRecord() : \PSX\Record\RecordInterface
    {
        /** @var \PSX\Record\Record<mixed> $record */
        $record = new \PSX\Record\Record();
        $record->put('totalResults', $this->totalResults);
        $record->put('startIndex', $this->startIndex);
        $record->put('itemsPerPage', $this->itemsPerPage);
        $record->put('entry', $this->entry);
        return $record;
    }
    public function jsonSerialize() : object
    {
        return (object) $this->toRecord()->getAll();
    }
}