jenschude / raml-php-generator

Generate a PHP API client from RAML
Other
11 stars 2 forks source link

use index.php #5

Open rac021 opened 7 years ago

rac021 commented 7 years ago

Hi,

How to use the index.php file which is generated ?

Thank's

jenschude commented 7 years ago

Hi,

the output comes with an composer.json. So you just have to add the output folder as a dependency to the project consuming the API.

jenschude commented 7 years ago

Btw i'm working on an update of the generator which inlines to the recent changes by the raml-javascript-generator. As the javascript generator is now using typescript as templating language, this project will produce a different class API which may be easier to handle.

rac021 commented 7 years ago

Thank you for your quick response,

I have the following api.raml :

#%RAML 0.8
title: Sample API
version: v1
mediaType:  application/json

schemas:
  - artist: !include ./schemas/artist-schema.json

/artists/{name}:
  get:
    responses:
      200:
        body:
          schema: artist
          example: |
            {
              "firstName": "John Doe",
              "birthday": "2014-12-23T15:05:00.000Z",
              "aliasS": "jaydoe",
              "age": 35
            }

and here is the generated php file :


<?php

/**
* @param  string $string
* @param  mixed $interpolate
* @param  mixed $defaults
* @return string
*/
function template($string, $interpolate = [], $defaults = []) {
    $defaults = !is_null($defaults) ? $defaults : [];
    $interpolate = !is_null($interpolate) ? $interpolate : [];

  return preg_replace_callback(TEMPLATE_REGEXP, function ($matches) use (
$defaults, $interpolate) {
        $key = $matches[1];
        if (isset($interpolate[$key]) && $interpolate[$key] != null) {
            return urlencode($interpolate[$key]);
        }

        if (isset($defaults[$key]) && $defaults[$key] != null) {
            return urlencode($defaults[$key]);
        }

        return '';

    }, $string);

}

class Parameter
{
    protected $key;
    protected $value;

    public function __construct($key, $value = null)
    {
        if (empty($key)) {
            throw new \InvalidArgumentException('no key given');
        }
        $this->key = $key;
        $this->value = $value;
    }

    public function getId()
    {
        return $this->key;
    }

    public function getValue()
    {
        return $this->value;
    }

    public function __toString()
    {
        $value = $this->getValue();
        if (is_null($value)) {
            $paramStr = $this->key;
        } elseif (is_bool($value)) {
            $paramStr = $this->key . '=' . ($value ? 'true' : 'false');
        } else {
            $paramStr = $this->key . '=' . urlencode((string)$value);
        }

        return $paramStr;
    }
}

class Resource
{
    protected $client;
    protected $uri;

    public function __construct($uri, Client $client)
    {
        $this->uri = $uri;
        $this->client = $client;
    }
}

class Resource021 extends Resource
{
    public $artists;

    public function __construct($uri, $client)
    {
        parent::__construct($uri, $client);

        $this->artists = new Resource1($this->uri . '/artists', $client);
    }

}
class Resource1 extends Resource
{

    public function __construct($uri, $client)
    {
        parent::__construct($uri, $client);

    }

    public function name($name)
    {
        $uri = $this->uri . template('/{0}', [$name]);

        return new Resource2($uri, $this->client);
    }

}
class Resource2 extends Resource
{

    public function __construct($uri, $client)
    {
        parent::__construct($uri, $client);

    }

    public function get($body = null, $options = [])
    {
        return $this->client->request($this->uri, 'GET', $body, $options);
    }
}

class CustomResource extends Resource
{
    public function checkout($body = null, $options = [])
    {
        return $this->client->request($this->uri, 'CHECKOUT', $body, $options);
    }
    public function connect($body = null, $options = [])
    {
        return $this->client->request($this->uri, 'CONNECT', $body, $options);
    }
    public function copy($body = null, $options = [])
    {
        return $this->client->request($this->uri, 'COPY', $body, $options);
    }
    public function delete($body = null, $options = [])
    {
        return $this->client->request($this->uri, 'DELETE', $body, $options);
    }
    public function get($body = null, $options = [])
    {
        return $this->client->request($this->uri, 'GET', $body, $options);
    }
    public function head($body = null, $options = [])
    {
        return $this->client->request($this->uri, 'HEAD', $body, $options);
    }
    public function lock($body = null, $options = [])
    {
        return $this->client->request($this->uri, 'LOCK', $body, $options);
    }
    public function mSearch($body = null, $options = [])
    {
        return $this->client->request($this->uri, 'M-SEARCH', $body, $options);
    }
    public function merge($body = null, $options = [])
    {
        return $this->client->request($this->uri, 'MERGE', $body, $options);
    }
    public function mkactivity($body = null, $options = [])
    {
        return $this->client->request($this->uri, 'MKACTIVITY', $body, $options);
    }
    public function mkcalendar($body = null, $options = [])
    {
        return $this->client->request($this->uri, 'MKCALENDAR', $body, $options);
    }
    public function mkcol($body = null, $options = [])
    {
        return $this->client->request($this->uri, 'MKCOL', $body, $options);
    }
    public function move($body = null, $options = [])
    {
        return $this->client->request($this->uri, 'MOVE', $body, $options);
    }
    public function notify($body = null, $options = [])
    {
        return $this->client->request($this->uri, 'NOTIFY', $body, $options);
    }
    public function options($body = null, $options = [])
    {
        return $this->client->request($this->uri, 'OPTIONS', $body, $options);
    }
    public function patch($body = null, $options = [])
    {
        return $this->client->request($this->uri, 'PATCH', $body, $options);
    }
    public function post($body = null, $options = [])
    {
        return $this->client->request($this->uri, 'POST', $body, $options);
    }
    public function propfind($body = null, $options = [])
    {
        return $this->client->request($this->uri, 'PROPFIND', $body, $options);
    }
    public function proppatch($body = null, $options = [])
    {
        return $this->client->request($this->uri, 'PROPPATCH', $body, $options);
    }
    public function purge($body = null, $options = [])
    {
        return $this->client->request($this->uri, 'PURGE', $body, $options);
    }
    public function put($body = null, $options = [])
    {
        return $this->client->request($this->uri, 'PUT', $body, $options);
    }
    public function report($body = null, $options = [])
    {
        return $this->client->request($this->uri, 'REPORT', $body, $options);
    }
    public function search($body = null, $options = [])
    {
        return $this->client->request($this->uri, 'SEARCH', $body, $options);
    }
    public function subscribe($body = null, $options = [])
    {
        return $this->client->request($this->uri, 'SUBSCRIBE', $body, $options);
    }
    public function trace($body = null, $options = [])
    {
        return $this->client->request($this->uri, 'TRACE', $body, $options);
    }
    public function unlock($body = null, $options = [])
    {
        return $this->client->request($this->uri, 'UNLOCK', $body, $options);
    }
    public function unsubscribe($body = null, $options = [])
    {
        return $this->client->request($this->uri, 'UNSUBSCRIBE', $body, $options);
    }
}

class Client
{
    private $httpClient;

    private $options;

    public $resources;

    public $resource;

    public function __construct($options = [])
    {     
        $baseUriParameters = [];
        if (isset($options['baseUriParameters'])) {
            $baseUriParameters = $options['baseUriParameters'];
            unset($options['baseUriParameters']);
        }
        if (isset($options['baseUri'])) {
            $options['base_uri'] = template($options['baseUri'], $baseUriParameters);
            unset($options['baseUri']);
        } else {
            $options['base_uri'] = template('', []);
        }
        $this->options = $options;

        $this->httpClient = new HttpClient($this->options);
        $this->resources = new Resource021('.', $this);
    }

    protected function buildRequest($path, $method, $body = null, $options = [])
    {
        $options = array_merge($this->options, $options);
        $hasBody = $method !== 'GET' && $method !== 'HEAD';

        $reqQuery = [];
        if (isset($options['query'])) {
            $reqQuery = $options['query'];
            unset($options['query']);
        }
        $headers = isset($options['headers']) ? $options['headers'] : [];

        $request = new Request($method, $path, $headers);

        if ($hasBody && !is_null($body)) {
            $request = $request->withBody($body);
        }
        if (!$hasBody && !is_null($body)) {
            $reqQuery = array_merge($reqQuery, $body);
        }
        if (count($reqQuery) > 0) {
            $params = [];
            foreach ($reqQuery as $key => $value) {
                if (!is_array($value)) {
                    $param = new Parameter($key, $value);
                    $params[$param->getId()] = $param;
                } else {
                    foreach ($value as $data) {
                        $param = new MultiParameter($key, $data);
                        $params[$param->getId()] = $param;
                    }
                }
            }
            $params = array_map(
                function ($param) {
                    return (string)$param;
                },
                $params
            );
            sort($params);
            $query = implode('&', $params);
            $uri = $request->getUri()->withQuery($query);
            $request = $request->withUri($uri);
        }

        return $request;
    }

    public function request($path, $method, $body = null, $options = [])
    {
        return $this->send($this->buildRequest($path, $method, $body, $options));
    }

    public function resource($route, $parameters)
    {
        $path = '/' . ltrim(template($route, $parameters),'/');

        return new CustomResource($path, $this);
    }

    protected function send(Request $request)
    {
        return $this->getHttpClient()->send($request);
    }

    public function getHttpClient()
    {
        return $this->httpClient;
    }
}

?>

My problem is that I do not know where to start knowing that I am no expert in php.

I'm trying to get artist that have id 10, but I'm stuck.

below, my piece of code

  $options = [            
            'baseUri' => 'http://localhost:8081/artists'
   ] ;

  $client = new Client($options) ;

 $res = $client->resources->artists-> .... ??

Thank's