Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 15 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
Expand Down
9 changes: 4 additions & 5 deletions src/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -25,9 +26,7 @@
* 'components' => [
* 'restclient' => [
* 'class' => 'simialbi\yii2\rest\Connection',
* 'config' => [
* 'base_uri' => 'https://api.site.com/',
* ],
* 'baseUrl' => 'https://api.site.com/',
* ],
* ],
* ```
Expand Down Expand Up @@ -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;
}
Expand Down