Skip to content

Commit 404e704

Browse files
francescobiancoSeraphim200001claude
committed
feat: integrate transport layer, DotEnv support and Openapi namespace from upkeep
- Rename namespace from OpenApi\ to Openapi\ across all classes - Replace Client with OpenapiClient supporting injectable HTTP transport - Add OpenapiCurlTransport as default cURL-based transport - Add OpenapiHttpTransportInterface for custom client injection (Guzzle, PSR-18) - Add OpenapiDotEnv for lightweight .env loading without framework dependencies - Add OpenapiBootstrap for automatic .env loading via Composer files autoload - Rename OauthClient → OpenapiOauthClient with env var fallback for credentials - Rename Exception → OpenapiException, CacheInterface → OpenapiCacheInterface - Rename ArrayCache → OpenapiArrayCache - Add phpunit.xml.dist with env fixtures for unit tests - Add OpenapiClientTest with fake transport for isolated unit testing - Add psr/http-client dependency Co-authored-by: Mario Ugurcu <mario.ugurcu@wum-studios.de> Co-authored-by: Claude Sonnet 4.6 <noreply@anthropic.com>
1 parent dd94955 commit 404e704

25 files changed

Lines changed: 600 additions & 446 deletions

composer.json

Lines changed: 42 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,42 @@
1-
{
2-
"name": "openapi/openapi-sdk",
3-
"description": "Minimal and agnostic PHP SDK for Openapi® (https://openapi.com)",
4-
"license": "MIT",
5-
"authors": [
6-
{
7-
"name": "Altravia",
8-
"email": "info@altravia.com"
9-
}
10-
],
11-
"minimum-stability": "stable",
12-
"require": {
13-
"php": ">=8.0.0",
14-
"ext-curl": "*",
15-
"ext-json": "*"
16-
},
17-
"require-dev": {
18-
"symfony/dotenv": "^5.3",
19-
"phpunit/phpunit": "^9.5"
20-
},
21-
"autoload": {
22-
"psr-4": {
23-
"OpenApi\\": "src"
24-
}
25-
},
26-
"scripts": {
27-
"test": "phpunit tests/",
28-
"test:unit": "phpunit tests/ --testsuite=unit",
29-
"example:token": "php examples/token_generation.php",
30-
"example:api": "php examples/api_calls.php",
31-
"example:complete": "php examples/complete_workflow.php"
32-
}
33-
}
1+
{
2+
"name": "openapi/openapi-sdk",
3+
"description": "Minimal and agnostic PHP SDK for Openapi® (https://openapi.com)",
4+
"license": "MIT",
5+
"authors": [
6+
{
7+
"name": "Altravia",
8+
"email": "info@altravia.com"
9+
}
10+
],
11+
"minimum-stability": "stable",
12+
"require": {
13+
"php": ">=8.0.0",
14+
"ext-curl": "*",
15+
"ext-json": "*",
16+
"psr/http-client": "^1.0"
17+
},
18+
"require-dev": {
19+
"symfony/dotenv": "^5.3",
20+
"phpunit/phpunit": "^9.5"
21+
},
22+
"autoload": {
23+
"psr-4": {
24+
"Openapi\\": "src"
25+
},
26+
"files": [
27+
"src/OpenapiBootstrap.php"
28+
]
29+
},
30+
"autoload-dev": {
31+
"psr-4": {
32+
"Tests\\": "tests"
33+
}
34+
},
35+
"scripts": {
36+
"test": "phpunit tests/",
37+
"test:unit": "phpunit tests/ --testsuite=unit",
38+
"example:token": "php examples/token_generation.php",
39+
"example:api": "php examples/api_calls.php",
40+
"example:complete": "php examples/complete_workflow.php"
41+
}
42+
}

examples/api_calls.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,16 @@
22

33
require_once __DIR__ . '/../vendor/autoload.php';
44

5-
use OpenApi\Client;
5+
use Openapi\OpenapiClient;
66

77
try {
88
$token = '<your_access_token>';
9-
$client = new Client($token);
9+
$client = new OpenapiClient($token);
1010

1111
// GET request with parameters
1212
$params = [
1313
'denominazione' => 'altravia',
14-
'provincia' => 'RM',
14+
'provincia' => 'RM',
1515
'codice_ateco' => '6201'
1616
];
1717

@@ -31,4 +31,4 @@
3131

3232
} catch (Exception $e) {
3333
echo "Error: " . $e->getMessage() . PHP_EOL;
34-
}
34+
}

examples/complete_workflow.php

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,16 @@
22

33
require_once __DIR__ . '/../vendor/autoload.php';
44

5-
use OpenApi\OauthClient;
6-
use OpenApi\Client;
7-
use OpenApi\Exception;
5+
use Openapi\OpenapiOauthClient;
6+
use Openapi\OpenapiClient;
7+
use Openapi\OpenapiException;
88

99
try {
1010
echo "=== OpenAPI PHP SDK Complete Workflow Example ===" . PHP_EOL . PHP_EOL;
1111

1212
// Step 1: Create OAuth client
1313
echo "Step 1: Creating OAuth client..." . PHP_EOL;
14-
$oauthClient = new OauthClient('<your_username>', '<your_apikey>', true);
14+
$oauthClient = new OpenapiOauthClient('<your_username>', '<your_apikey>', true);
1515
echo "✓ OAuth client created" . PHP_EOL . PHP_EOL;
1616

1717
// Step 2: Generate token
@@ -25,15 +25,15 @@
2525
$tokenData = json_decode($tokenResult, true);
2626

2727
if (!isset($tokenData['token'])) {
28-
throw new Exception('Failed to generate token: ' . $tokenResult);
28+
throw new OpenapiException('Failed to generate token: ' . $tokenResult);
2929
}
3030

3131
$token = $tokenData['token'];
3232
echo "✓ Token generated: " . substr($token, 0, 20) . "..." . PHP_EOL . PHP_EOL;
3333

3434
// Step 3: Create API client
3535
echo "Step 3: Creating API client..." . PHP_EOL;
36-
$apiClient = new Client($token);
36+
$apiClient = new OpenapiClient($token);
3737
echo "✓ API client created" . PHP_EOL . PHP_EOL;
3838

3939
// Step 4: Make API calls
@@ -49,7 +49,7 @@
4949
$getResponse = $apiClient->get('https://test.imprese.openapi.it/advance', $getParams);
5050
echo " ✓ GET response received (" . strlen($getResponse) . " bytes)" . PHP_EOL;
5151

52-
// POST request
52+
// POST request
5353
echo " → Making POST request..." . PHP_EOL;
5454
$postPayload = [
5555
'limit' => 10,
@@ -62,14 +62,14 @@
6262

6363
echo "=== Workflow completed successfully! ===" . PHP_EOL;
6464

65-
} catch (Exception $e) {
65+
} catch (OpenapiException $e) {
6666
echo "✗ Error: " . $e->getMessage() . PHP_EOL;
67-
67+
6868
if ($e->getHttpCode()) {
6969
echo " HTTP Code: " . $e->getHttpCode() . PHP_EOL;
7070
}
71-
71+
7272
if ($e->getServerResponse()) {
7373
echo " Server Response: " . json_encode($e->getServerResponse()) . PHP_EOL;
7474
}
75-
}
75+
}

examples/token_generation.php

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

33
require_once __DIR__ . '/../vendor/autoload.php';
44

5-
use OpenApi\OauthClient;
5+
use Openapi\OpenapiOauthClient;
66

77
try {
8-
$oauthClient = new OauthClient('<your_username>', '<your_apikey>', true);
8+
$oauthClient = new OpenapiOauthClient('<your_username>', '<your_apikey>', true);
99

1010
$scopes = [
1111
'GET:test.imprese.openapi.it/advance',
1212
'POST:test.postontarget.com/fields/country'
1313
];
14-
14+
1515
$ttl = 3600;
1616
$result = $oauthClient->createToken($scopes, $ttl);
1717

1818
$tokenData = json_decode($result, true);
19-
19+
2020
if (isset($tokenData['token'])) {
2121
echo "Generated token: " . $tokenData['token'] . PHP_EOL;
2222
echo "Token created successfully!" . PHP_EOL;
@@ -26,4 +26,4 @@
2626

2727
} catch (Exception $e) {
2828
echo "Error: " . $e->getMessage() . PHP_EOL;
29-
}
29+
}

phpunit.xml.dist

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.5/phpunit.xsd"
4+
bootstrap="vendor/autoload.php"
5+
colors="true">
6+
7+
<testsuites>
8+
<testsuite name="unit">
9+
<directory>tests</directory>
10+
</testsuite>
11+
</testsuites>
12+
13+
<php>
14+
<env name="OPENAPI_BASE_URL" value="https://example.com" force="true"/>
15+
<env name="OPENAPI_USERNAME" value="test_user" force="true"/>
16+
<env name="OPENAPI_SANDBOX_KEY" value="test_key" force="true"/>
17+
<env name="OPENAPI_OAUTH_SANDBOX_URL" value="https://api.com" force="true"/>
18+
<env name="OPENAPI_OAUTH_URL" value="https://api.com" force="true"/>
19+
</php>
20+
</phpunit>

src/Cache/CacheInterface.php

Lines changed: 0 additions & 33 deletions
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,18 @@
11
<?php
22

3-
namespace OpenApi\Cache;
3+
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-
*/
9-
class ArrayCache implements CacheInterface
5+
class OpenapiArrayCache implements OpenapiCacheInterface
106
{
117
private array $cache = [];
128
private array $expiry = [];
139

14-
/**
15-
* Retrieve value from cache
16-
* Returns null if key doesn't exist or has expired
17-
*/
1810
public function get(string $key): mixed
1911
{
2012
if (!isset($this->cache[$key])) {
2113
return null;
2214
}
2315

24-
// Check if expired
2516
if (isset($this->expiry[$key]) && time() > $this->expiry[$key]) {
2617
$this->delete($key);
2718
return null;
@@ -30,9 +21,6 @@ public function get(string $key): mixed
3021
return $this->cache[$key];
3122
}
3223

33-
/**
34-
* Save value to cache with expiration
35-
*/
3624
public function save(string $key, mixed $value, int $ttl = 3600): bool
3725
{
3826
$this->cache[$key] = $value;
@@ -41,24 +29,18 @@ public function save(string $key, mixed $value, int $ttl = 3600): bool
4129
return true;
4230
}
4331

44-
/**
45-
* Delete value from cache
46-
*/
4732
public function delete(string $key): bool
4833
{
4934
unset($this->cache[$key], $this->expiry[$key]);
5035

5136
return true;
5237
}
5338

54-
/**
55-
* Clear all cached values
56-
*/
5739
public function clear(): bool
5840
{
5941
$this->cache = [];
6042
$this->expiry = [];
6143

6244
return true;
6345
}
64-
}
46+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
namespace Openapi\Cache;
4+
5+
interface OpenapiCacheInterface
6+
{
7+
public function get(string $key): mixed;
8+
9+
public function save(string $key, mixed $value, int $ttl = 3600): bool;
10+
11+
public function delete(string $key): bool;
12+
13+
public function clear(): bool;
14+
}

0 commit comments

Comments
 (0)