Skip to content

Commit 747d297

Browse files
author
Josh Freeman
committed
Generalise query options
1 parent eff45e5 commit 747d297

File tree

5 files changed

+30
-104
lines changed

5 files changed

+30
-104
lines changed

README.md

Lines changed: 9 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -27,44 +27,17 @@ You can set the environment variable: `XIVAPI_KEY` Or via:
2727
$api->environment->key('my_api_key');
2828
```
2929

30-
**Using global queries**
30+
**Using Queries (excludes Search)**
3131

32-
The following global queries are supported:
32+
The API has a whole host of queries to allow you to customise the response, these are all passed to the API before you request data. Search has some extra queries that are hard coded as these interact with Elastic Search.
3333

34-
#### `columns`
35-
36-
```php
37-
$columns = [
38-
'ID',
39-
'Name',
40-
'Icon
41-
];
42-
43-
$api->columns($columns)->content->Item()->list();
44-
```
45-
46-
#### `language`
47-
48-
```php
49-
$api->language('en')->content->Item()->list();
50-
```
51-
52-
#### `snake_case`
53-
54-
```php
55-
$api->snakeCase()->content->Item()->list();
56-
```
57-
58-
#### `tags`
59-
60-
```php
61-
$tags = [
62-
'one',
63-
'two',
64-
'three'
65-
];
66-
$api->tags($tags)->content->Item()->list();
67-
```
34+
- `limit=250` - https://xivapi.com/docs/Content#limit
35+
- `ids=1,4,8,20` - https://xivapi.com/docs/Content#ids
36+
- `minify=1` - https://xivapi.com/docs/Content#minify
37+
- `language=en` - https://xivapi.com/docs/Welcome#language
38+
- `snake_case` - https://xivapi.com/docs/Welcome#snake_case
39+
- `columns` - https://xivapi.com/docs/Welcome#columns
40+
- `tags` - https://xivapi.com/docs/Welcome#tags
6841

6942
### Content
7043

cli

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,15 @@ $api = new \XIVAPI\XIVAPI();
1212
// set env key
1313
$api->environment->key('my_api_key');
1414

15-
$columns = [
16-
'ID',
17-
'Name'
15+
$queries = [
16+
'limit' => 500,
17+
'columns' => [
18+
'ID',
19+
'Name'
20+
]
1821
];
1922

2023
// do commands
21-
$results = $api->columns($columns)->content->Item()->list();
24+
$results = $api->queries($queries)->content->Item()->list();
2225

2326
print_r($results);

src/XIVAPI/Api/Content.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -459,7 +459,7 @@ public function __call($name, $args): ContentHandler
459459
return (new ContentHandler())->setContentName($name);
460460
}
461461

462-
public function list(): array
462+
public function list($options = []): array
463463
{
464464
return Guzzle::get('/content');
465465
}

src/XIVAPI/Guzzle/Guzzle.php

Lines changed: 9 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
namespace XIVAPI\Guzzle;
44

55
use GuzzleHttp\Client;
6-
use GuzzleHttp\Exception\ClientException;
76
use GuzzleHttp\RequestOptions;
87
use XIVAPI\Common\Environment;
98

@@ -14,31 +13,17 @@ class Guzzle
1413
const TIMEOUT = 10.0;
1514
const VERIFY = false;
1615

17-
private static $columns = [];
18-
private static $language = null;
19-
private static $snakeCase = false;
20-
private static $tags = [];
16+
private static $options = [];
2117

2218
public static function query($method, $apiEndpoint, $options = [])
2319
{
2420
if ($key = getenv(Environment::XIVAPI_KEY)) {
2521
$options[RequestOptions::QUERY]['key'] = $key;
2622
}
2723

28-
if (self::$columns) {
29-
$options[RequestOptions::QUERY]['columns'] = implode(',', self::$columns);
30-
}
31-
32-
if (self::$language) {
33-
$options[RequestOptions::QUERY]['language'] = self::$language;
34-
}
35-
36-
if (self::$snakeCase) {
37-
$options[RequestOptions::QUERY]['snake_case'] = 1;
38-
}
39-
40-
if (self::$tags) {
41-
$options[RequestOptions::QUERY]['tags'] = implode(',', self::$tags);
24+
foreach (self::$options as $query => $value) {
25+
$value = is_array($value) ? implode(',', $value) : $value;
26+
$options[RequestOptions::QUERY][$query] = $value;
4227
}
4328

4429
$client = new Client([
@@ -47,34 +32,14 @@ public static function query($method, $apiEndpoint, $options = [])
4732
'verify' => self::VERIFY,
4833
]);
4934

50-
try {
51-
return \GuzzleHttp\json_decode(
52-
$client->request($method, $apiEndpoint, $options)->getBody()
53-
);
54-
} catch (ClientException $ex) {
55-
// todo - handle exception
56-
$response = $ex->getResponse();
57-
}
58-
}
59-
60-
public static function setColumns($columns)
61-
{
62-
self::$columns = $columns;
63-
}
64-
65-
public static function setLanguage($language)
66-
{
67-
self::$language = $language;
68-
}
69-
70-
public static function setSnakeCase()
71-
{
72-
self::$snakeCase = true;
35+
return \GuzzleHttp\json_decode(
36+
$client->request($method, $apiEndpoint, $options)->getBody()
37+
);
7338
}
7439

75-
public static function setTags($tags)
40+
public static function setQuery(string $query, $value)
7641
{
77-
self::$tags = $tags;
42+
self::$options[$query] = $value;
7843
}
7944

8045
public static function get($apiEndpoint, $options = [])

src/XIVAPI/XIVAPI.php

Lines changed: 4 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -51,27 +51,12 @@ public function __construct()
5151
$this->patchlist = new PatchList();
5252
}
5353

54-
public function columns($columns): XIVAPI
54+
public function queries($queries): XIVAPI
5555
{
56-
Guzzle::setColumns($columns);
57-
return $this;
58-
}
59-
60-
public function language($language): XIVAPI
61-
{
62-
Guzzle::setLanguage($language);
63-
return $this;
64-
}
56+
foreach ($queries as $query => $value) {
57+
Guzzle::setQuery($query, $value);
58+
}
6559

66-
public function snakeCase(): XIVAPI
67-
{
68-
Guzzle::setSnakeCase();
69-
return $this;
70-
}
71-
72-
public function tags($tags): XIVAPI
73-
{
74-
Guzzle::setTags($tags);
7560
return $this;
7661
}
7762
}

0 commit comments

Comments
 (0)