Skip to content

Commit 7dc86b8

Browse files
Merge pull request #3 from openapi/claude/add-comments-and-todos-011CUxWxPPiWJnxDgkqZH9xi
Add code comments and improve error messages
2 parents b2be918 + f0e3828 commit 7dc86b8

5 files changed

Lines changed: 160 additions & 11 deletions

File tree

src/Cache/ArrayCache.php

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,26 @@
22

33
namespace OpenApi\Cache;
44

5+
/**
6+
* In-memory cache implementation
7+
* Data is stored in PHP arrays and cleared at end of script execution
8+
*/
59
class ArrayCache implements CacheInterface
610
{
711
private array $cache = [];
812
private array $expiry = [];
913

14+
/**
15+
* Retrieve value from cache
16+
* Returns null if key doesn't exist or has expired
17+
*/
1018
public function get(string $key): mixed
1119
{
1220
if (!isset($this->cache[$key])) {
1321
return null;
1422
}
1523

24+
// Check if expired
1625
if (isset($this->expiry[$key]) && time() > $this->expiry[$key]) {
1726
$this->delete($key);
1827
return null;
@@ -21,26 +30,35 @@ public function get(string $key): mixed
2130
return $this->cache[$key];
2231
}
2332

33+
/**
34+
* Save value to cache with expiration
35+
*/
2436
public function save(string $key, mixed $value, int $ttl = 3600): bool
2537
{
2638
$this->cache[$key] = $value;
2739
$this->expiry[$key] = time() + $ttl;
28-
40+
2941
return true;
3042
}
3143

44+
/**
45+
* Delete value from cache
46+
*/
3247
public function delete(string $key): bool
3348
{
3449
unset($this->cache[$key], $this->expiry[$key]);
35-
50+
3651
return true;
3752
}
3853

54+
/**
55+
* Clear all cached values
56+
*/
3957
public function clear(): bool
4058
{
4159
$this->cache = [];
4260
$this->expiry = [];
43-
61+
4462
return true;
4563
}
4664
}

src/Cache/CacheInterface.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,32 @@
22

33
namespace OpenApi\Cache;
44

5+
/**
6+
* Cache interface for SDK implementations
7+
*/
58
interface CacheInterface
69
{
10+
/**
11+
* Retrieve value from cache
12+
*/
713
public function get(string $key): mixed;
14+
15+
/**
16+
* Save value to cache with TTL
17+
*
18+
* @param string $key Cache key
19+
* @param mixed $value Value to store
20+
* @param int $ttl Time-to-live in seconds (default: 3600)
21+
*/
822
public function save(string $key, mixed $value, int $ttl = 3600): bool;
23+
24+
/**
25+
* Delete value from cache
26+
*/
927
public function delete(string $key): bool;
28+
29+
/**
30+
* Clear all cached values
31+
*/
1032
public function clear(): bool;
1133
}

src/Client.php

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,40 @@
22

33
namespace OpenApi;
44

5-
class Client
5+
/**
6+
* Generic HTTP client for OpenAPI services
7+
* Handles REST operations with Bearer token authentication
8+
*/
9+
class Client
610
{
711
private string $token;
812

13+
/**
14+
* Initialize client with Bearer token
15+
*/
916
public function __construct(string $token)
1017
{
1118
$this->token = $token;
1219
}
1320

21+
/**
22+
* Execute HTTP request
23+
*
24+
* @param string $method HTTP method (GET, POST, PUT, DELETE, PATCH)
25+
* @param string $url Target URL
26+
* @param mixed $payload Request body (for POST/PUT/PATCH)
27+
* @param array|null $params Query parameters (for GET) or form data (for other methods)
28+
* @return string Response body
29+
*/
1430
public function request(string $method, string $url, mixed $payload = null, ?array $params = null): string
1531
{
32+
// Append query parameters for GET requests
1633
if ($params && $method === 'GET') {
1734
$url .= '?' . http_build_query($params);
1835
}
1936

2037
$ch = curl_init();
21-
38+
2239
curl_setopt_array($ch, [
2340
CURLOPT_URL => $url,
2441
CURLOPT_RETURNTRANSFER => true,
@@ -30,12 +47,14 @@ public function request(string $method, string $url, mixed $payload = null, ?arr
3047
]
3148
]);
3249

50+
// Add JSON payload for POST/PUT/PATCH requests
3351
if ($payload && in_array($method, ['POST', 'PUT', 'PATCH'])) {
3452
curl_setopt($ch, CURLOPT_POSTFIELDS, is_string($payload) ? $payload : json_encode($payload));
3553
}
3654

55+
// Add form data for non-GET requests
3756
if ($params && $method !== 'GET') {
38-
curl_setopt($ch, CURLOPT_POSTFIELDS,
57+
curl_setopt($ch, CURLOPT_POSTFIELDS,
3958
is_string($params) ? $params : http_build_query($params));
4059
}
4160

@@ -44,37 +63,54 @@ public function request(string $method, string $url, mixed $payload = null, ?arr
4463
$error = curl_error($ch);
4564
curl_close($ch);
4665

66+
// TODO: Provide more graceful error message with connection context (timeout, DNS, SSL, etc.)
4767
if ($response === false) {
4868
throw new Exception("cURL Error: " . $error);
4969
}
5070

71+
// TODO: Parse response body and provide structured error details (error code, message, request ID)
5172
if ($httpCode >= 400) {
5273
throw new Exception("HTTP Error {$httpCode}: " . $response);
5374
}
5475

5576
return $response;
5677
}
5778

79+
/**
80+
* Perform GET request
81+
*/
5882
public function get(string $url, ?array $params = null): string
5983
{
6084
return $this->request('GET', $url, null, $params);
6185
}
6286

87+
/**
88+
* Perform POST request
89+
*/
6390
public function post(string $url, mixed $payload = null): string
6491
{
6592
return $this->request('POST', $url, $payload);
6693
}
6794

95+
/**
96+
* Perform PUT request
97+
*/
6898
public function put(string $url, mixed $payload = null): string
6999
{
70100
return $this->request('PUT', $url, $payload);
71101
}
72102

103+
/**
104+
* Perform DELETE request
105+
*/
73106
public function delete(string $url): string
74107
{
75108
return $this->request('DELETE', $url);
76109
}
77110

111+
/**
112+
* Perform PATCH request
113+
*/
78114
public function patch(string $url, mixed $payload = null): string
79115
{
80116
return $this->request('PATCH', $url, $payload);

src/Exception.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,26 @@
22

33
namespace OpenApi;
44

5+
/**
6+
* Custom exception for OpenAPI SDK
7+
* Stores HTTP response details for better error handling
8+
*/
59
class Exception extends \Exception
610
{
711
private mixed $serverResponse = null;
812
private mixed $headers = null;
913
private mixed $rawResponse = null;
1014
private ?int $httpCode = null;
1115

16+
/**
17+
* Store server response details
18+
* TODO: Utilize this method in Client and OauthClient to provide structured error context
19+
*
20+
* @param mixed $response Parsed server response
21+
* @param mixed $headers Response headers
22+
* @param mixed $rawResponse Raw response body
23+
* @param int|null $httpCode HTTP status code
24+
*/
1225
public function setServerResponse(mixed $response, mixed $headers = null, mixed $rawResponse = null, ?int $httpCode = null): void
1326
{
1427
$this->serverResponse = $response;
@@ -17,21 +30,33 @@ public function setServerResponse(mixed $response, mixed $headers = null, mixed
1730
$this->httpCode = $httpCode;
1831
}
1932

33+
/**
34+
* Get parsed server response
35+
*/
2036
public function getServerResponse(): mixed
2137
{
2238
return $this->serverResponse;
2339
}
2440

41+
/**
42+
* Get response headers
43+
*/
2544
public function getHeaders(): mixed
2645
{
2746
return $this->headers;
2847
}
2948

49+
/**
50+
* Get raw response body
51+
*/
3052
public function getRawResponse(): mixed
3153
{
3254
return $this->rawResponse;
3355
}
3456

57+
/**
58+
* Get HTTP status code
59+
*/
3560
public function getHttpCode(): ?int
3661
{
3762
return $this->httpCode;

0 commit comments

Comments
 (0)