-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPutTrait.php
More file actions
91 lines (79 loc) · 2.46 KB
/
Copy pathPutTrait.php
File metadata and controls
91 lines (79 loc) · 2.46 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
<?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://tinyurl.com/vmbbh6kd for the canonical source repository
* @license https://tinyurl.com/3pc9am89
* @copyright © 2023 Text Control GmbH
*/
namespace TextControl\ReportingCloud;
use Ctw\Http\HttpMethod;
use Ctw\Http\HttpStatus;
use GuzzleHttp\RequestOptions;
use JsonException;
use Psr\Http\Message\ResponseInterface;
use TextControl\ReportingCloud\PropertyMap\AbstractPropertyMap as PropertyMap;
/**
* Trait PutTrait
*/
trait PutTrait
{
/**
* Create an API key
*/
public function createApiKey(): string
{
return $this->put('/account/apikey', [], '', HttpStatus::STATUS_CREATED);
}
/**
* Construct URI with version number
*
* @param string $uri URI
*/
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
*/
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
*/
abstract protected function buildPropertyMapArray(array $array, PropertyMap $propertyMap): array;
/**
* 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
*/
private function put(string $uri, array $query = [], mixed $json = '', int $statusCode = 0): string
{
$ret = '';
$response = $this->request(HttpMethod::METHOD_PUT, $this->uri($uri), [
RequestOptions::QUERY => $query,
RequestOptions::JSON => $json,
]);
if ($statusCode === $response->getStatusCode()) {
try {
$body = $response->getBody();
$content = $body->getContents();
$ret = json_decode($content, true, 512, JSON_THROW_ON_ERROR);
} catch (JsonException) {
}
}
assert(is_string($ret));
return $ret;
}
}