From 937ca9b941b935a6887767ba188471ec06319867 Mon Sep 17 00:00:00 2001 From: Victor Westerlund Date: Mon, 1 May 2023 17:55:47 +0200 Subject: [PATCH] feat(doc): add doc for Python lib --- README.md | 72 ++++++++++++++++--------------------------------------- 1 file changed, 20 insertions(+), 52 deletions(-) diff --git a/README.md b/README.md index 1d8e101..ccfd436 100644 --- a/README.md +++ b/README.md @@ -1,55 +1,55 @@ -# Reflect API client for PHP +# Reflect API client for Python -Make requests to an API built using the [Reflect API](https://github.com/VictorWesterlund/reflect) framework over HTTP or UNIX sockets. This program comes with both an extendable/instantiable class that you can integrate with existing PHP code, or as a stand-alone CLI which can be run by itself or UNIX piped into other programs. +Make requests to an API built using the [Reflect API](https://github.com/VictorWesterlund/reflect) framework over HTTP or UNIX sockets. --- -Make a request with `Client->call()`. It will always return the response as an array of length 2. +Make a request with `Client.call()`. It will always return the response as a tuple of length 2. - The first value is the HTTP-equivalent response code. - The second value is the response body -```php -$client = new Reflect\Client(""); +```python +client = Client("", ""); -$client->call("foo", Method::GET); // (array) [200, "bar"] -$client->call("foo", Method::POST, [ - "foo" => "bar" -]); // (array) [201, "Created"] +client.call("foo", Method.GET) # (tuple) (200, "bar") +client.call("foo", Method.POST, { + "hello": "world +}) # (tuple) (201, "Created") -// etc.. +# etc.. ``` ## How to use -Requires PHP 8.1 or newer, and of course a Reflect API endpoint. +Requires Python 3 or newer, and of course a Reflect API endpoint. -1. **Install with composer** +1. **Install with pip** ``` - composer require reflect/client + pip3 install reflect-client ``` -2. **Initialize the class** +2. **Initialize the module** - ```php - require_once "/vendor/autoload.php"; + ```python + from reflect-client import Client - $client = new Reflect\Client(""); + client = Client("", ""); ``` 3. **Make API request** Use the `call()` method to perform a request - ```php - $client->call("foo?bar=baz", Method::GET); + ```python + client.call("foo?bar=baz", Method.GET); ``` Argument index|Type|Required|Description --|--|--|-- 0|String|Yes|Fully qualified pathname and query params of the endpoint to call 1|Method|Yes|A supported [Reflect HTTP method](https://github.com/VictorWesterlund/reflect/wiki/Supported-technologies#http-request-methods) (eg. `Method::GET`) - 2|Array|No|An optional indexed, associative, or multidimensional array that will be sent as the request body as `Content-Type: application/json` + 2|List|No|An optional List of key and values to be sent as `Content-Type: application/json` to the endpoint The `call()` function will return an array of length 2 wil the following information @@ -57,35 +57,3 @@ Requires PHP 8.1 or newer, and of course a Reflect API endpoint. --|--|-- 0|Int|HTTP-equivalent response code (eg. `200` or `404`) 1|String/Array|Contains the response body as either a string, or array if the response `Content-Type` header is `application/json` - -## How to use (CLI) - -You can also run this from the command line with - -``` -php client [payload] -``` - -and it will return a serialized JSON array with the same structure as described in the `Client->call()` return. - -*Example* -```sh -php client "/run/reflect.sock" "foo?bar=biz" "POST" "[\"foo\" => \"bar\"]" # (string) [201, \"Created\"] -``` - ---- - -Requires PHP CLI 8.1 or greater, and of course a Reflect API endpoint. - -1. **Clone repo** - - ``` - git clone https://github.com/victorwesterlund/reflect-client-php - ``` - -2. **Run from command line** - - ``` - cd reflect-client-php - php client [payload] - ```