This repository was archived by the owner on Sep 10, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathPutTrait.php
More file actions
116 lines (99 loc) · 2.9 KB
/
Copy pathPutTrait.php
File metadata and controls
116 lines (99 loc) · 2.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
<?php
declare(strict_types=1);
/**
* ReportingCloud PHP SDK
*
* PHP SDK for ReportingCloud Web API. Authored and supported by Text Control GmbH.
*
* @link https://www.reporting.cloud to learn more about ReportingCloud
* @link https://git.io/Jejj2 for the canonical source repository
* @license https://git.io/Jejjr
* @copyright © 2022 Text Control GmbH
*/
namespace TxTextControl\ReportingCloud;
use Ctw\Http\HttpMethod;
use Ctw\Http\HttpStatus;
use GuzzleHttp\RequestOptions;
use Psr\Http\Message\ResponseInterface;
use TxTextControl\ReportingCloud\Exception\InvalidArgumentException;
use TxTextControl\ReportingCloud\Exception\RuntimeException;
use TxTextControl\ReportingCloud\PropertyMap\AbstractPropertyMap as PropertyMap;
/**
* Trait PutTrait
*
* @package TxTextControl\ReportingCloud
* @author Jonathan Maron (@JonathanMaron)
*/
trait PutTrait
{
// <editor-fold desc="Abstract methods">
/**
* Construct URI with version number
*
* @param string $uri URI
*
* @return string
*/
abstract protected function uri(string $uri): string;
/**
* Request the URI with options
*
* @param string $method HTTP method
* @param string $uri URI
* @param array $options Options
*
* @return ResponseInterface
* @throws RuntimeException
*/
abstract protected function request(string $method, string $uri, array $options): ResponseInterface;
/**
* Using the passed propertyMap, recursively build array
*
* @param array $array Array
* @param PropertyMap $propertyMap PropertyMap
*
* @return array
*/
abstract protected function buildPropertyMapArray(array $array, PropertyMap $propertyMap): array;
// </editor-fold>
// <editor-fold desc="Methods">
/**
* Create an API key
*
* @return string
* @throws InvalidArgumentException
*/
public function createApiKey(): string
{
return $this->put('/account/apikey', [], '', HttpStatus::STATUS_CREATED);
}
/**
* Execute a PUT request via REST client
*
* @param string $uri URI
* @param array[] $query Query
* @param mixed $json JSON
* @param int $statusCode Required HTTP status code for response
*
* @return string
*/
private function put(
string $uri,
array $query = [],
$json = '',
int $statusCode = 0
): string {
$ret = '';
$options = [
RequestOptions::QUERY => $query,
RequestOptions::JSON => $json,
];
$response = $this->request(HttpMethod::METHOD_PUT, $this->uri($uri), $options);
if ($statusCode === $response->getStatusCode()) {
$ret = json_decode($response->getBody()->getContents(), true);
assert(is_string($ret));
}
return $ret;
}
// </editor-fold>
}