From aed00ec5ed3b59c8fa2e83c2b9a8122259bd2f86 Mon Sep 17 00:00:00 2001 From: Sylvain Date: Thu, 8 Jul 2021 12:19:19 +0200 Subject: [PATCH] Updated documentation * Updated readme (added configuration variables + default values) * Updated document block in Connection * Fix composer.json (minimum php version 7.0, required for \Throwable) --- README.md | 23 +++++++++++++++-------- composer.json | 2 +- src/Connection.php | 9 ++++----- 3 files changed, 20 insertions(+), 14 deletions(-) diff --git a/README.md b/README.md index 0b35963..6e97e32 100644 --- a/README.md +++ b/README.md @@ -49,14 +49,21 @@ To use this extension, configure restclient component in your application config ], ``` -| Parameter | Description | -| ------------------ | ---------------------------------------------------------------------------------------------------------------- | -| `baseUrl` | The location of the api. E.g. for http://api.site.com/v1/users the `baseUrl` would be http://api.site.com/v1/ | -| `auth` | Either a Closure which returns a `string` or a `string`. The rest connection will be passed as parameter. | -| `usePluralisation` | Whether to use plural version for lists (index action) or not (e.g. http://api.site.com/users instead of `user`) | -| `useFilterKeyword` | Whether to use "filter" key word in url parameters when filtering (e.g. ?filter[name]=user instead of ?name=user | -| `enableExceptions` | Whether the connection should throw an exception if response is not 200 or not | -| `itemsProperty` | If your items are wrapped inside a property (e.g. `items`), set it's name here | +| Parameter | Default | Description | +| ------------------- | -----------| ------------------------------------------------------------------------------------------------------------- | +| `baseUrl` | `''` | The location of the api. E.g. for http://api.site.com/v1/users the `baseUrl` would be http://api.site.com/v1/ (required) | +| `auth` | | Either a Closure which returns a `string` or a `string`. The rest connection will be passed as parameter. | +| `usePluralisation` | `true` | Whether to use plural version for lists (index action) or not (e.g. http://api.site.com/users instead of `user`) | +| `useFilterKeyword` | `true` | Whether to use "filter" key word in url parameters when filtering (e.g. ?filter[name]=user instead of ?name=user | +| `enableExceptions` | `false` | Whether the connection should throw an exception if response is not 200 or not | +| `itemsProperty` | | If your items are wrapped inside a property (e.g. `items`), set it's name here | +| `requestConfig` | `[]` | Client request configuration | +| `responseConfig` | `[]` | Client response configuration | +| `updateMethod` | `'put'` | The method to use for update operations. | +| `isTestMode` | `false` | Whether we are in test mode or not (prevent execution) | +| `enableQueryCache` | `false` | Whether to enable query caching | +| `queryCacheDuration`| `3600` | The default number of seconds that query results can remain valid in cache | +| `queryCache` | `'cache'` | The cache object or the ID of the cache application component | ## Usage Define your Model diff --git a/composer.json b/composer.json index b9c60ba..46469e5 100644 --- a/composer.json +++ b/composer.json @@ -26,7 +26,7 @@ }, "minimum-stability": "beta", "require": { - "php": ">=5.6", + "php": ">=7.0", "yiisoft/yii2": "^2.0.14", "yiisoft/yii2-httpclient": "^2.0.0" }, diff --git a/src/Connection.php b/src/Connection.php index 994ce14..f4bdddc 100644 --- a/src/Connection.php +++ b/src/Connection.php @@ -16,6 +16,7 @@ use yii\httpclient\Client; use yii\httpclient\Response; use yii\web\HeaderCollection; +use simialbi\yii2\rest\Exception as RestException; /** * Class Connection @@ -25,9 +26,7 @@ * 'components' => [ * 'restclient' => [ * 'class' => 'simialbi\yii2\rest\Connection', - * 'config' => [ - * 'base_uri' => 'https://api.site.com/', - * ], + * 'baseUrl' => 'https://api.site.com/', * ], * ], * ``` @@ -496,13 +495,13 @@ protected function request($method, $url, $data = []) try { $this->_response = $this->isTestMode ? [] : $request->send(); } catch (\yii\httpclient\Exception $e) { - throw new Exception('Request failed', [], 1, $e); + throw new RestException('Request failed', [], 1, $e); } Yii::endProfile($profile, __METHOD__); if (!$this->isTestMode && !$this->_response->isOk) { if ($this->enableExceptions) { - throw new Exception($this->_response->content, $this->_response->headers->toArray()); + throw new RestException($this->_response->content, $this->_response->headers->toArray()); } return false; }