Skip to content

Commit 48bd426

Browse files
refactoring files to introduce minimal version
1 parent 1f4210b commit 48bd426

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

59 files changed

+668
-5784
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
.idea/
22
.vscode/
3+
reference/
34
/vendor/
45
node_modules/
56
npm-debug.log

README.md

Lines changed: 43 additions & 244 deletions
Original file line numberDiff line numberDiff line change
@@ -1,273 +1,72 @@
1-
<div align="center">
2-
<a href="https://openapi.com/">
3-
<img alt="Openapi SDK for PHP" src=".github/assets/repo-header-a3.png" >
4-
</a>
1+
# OpenAPI PHP SDK
52

6-
<h1>Openapi® client for Rust</h1>
7-
<h4>The perfect starting point to integrate <a href="https://openapi.com/">Openapi®</a> within your Rust project</h4>
3+
A minimal and agnostic PHP SDK for OpenAPI, inspired by the Rust implementation. This SDK provides only the core HTTP primitives needed to interact with any OpenAPI service.
84

9-
[![Build Status](https://img.shields.io/github/actions/workflow/status/<username>/<repo>/rust.yml?branch=main)](https://github.com/<username>/<repo>/actions)
10-
[![Crates.io](https://img.shields.io/crates/v/<crate_name>.svg)](https://crates.io/crates/<crate_name>)
11-
[![Docs.rs](https://img.shields.io/docsrs/<crate_name>)](https://docs.rs/<crate_name>)
12-
[![License](https://img.shields.io/github/license/<username>/<repo>)](LICENSE)
13-
[![Rust Version](https://img.shields.io/badge/rust-1.80+-orange.svg)](https://www.rust-lang.org/)
14-
</div>
5+
## Features
156

16-
# OpenAPI Library
7+
- **Agnostic Design**: No API-specific classes, works with any OpenAPI service
8+
- **Minimal Dependencies**: Only requires PHP 8.0+ and cURL
9+
- **OAuth Support**: Built-in OAuth client for token management
10+
- **HTTP Primitives**: GET, POST, PUT, DELETE, PATCH methods
11+
- **Clean Interface**: Similar to the Rust SDK design
1712

18-
<!-- vscode-markdown-toc -->
19-
* 1. [Installation](#Installation)
20-
* 2. [Usage](#Usage)
21-
* 2.1. [Instanza della classe](#Instanzadellaclasse)
22-
* 2.2. [Esempi](#Esempi)
23-
* 3. [Modulo comuni](#Modulocomuni)
24-
* 3.1. [Esempi](#Esempi-1)
25-
* 4. [Modulo imprese](#Moduloimprese)
26-
* 4.1. [Utilizzo](#Utilizzo)
27-
* 4.2. [Esempi](#Esempi-1)
28-
* 5. [Modulo Marche Temporali](#ModuloMarcheTemporali)
29-
* 5.1. [Esempi](#Esempi-1)
30-
* 6. [Modulo SMS](#ModuloSMS)
31-
* 6.1. [Inviare un SMS](#InviareunSMS)
32-
* 7. [Modulo Visengine](#ModuloVisengine)
13+
## Installation
3314

34-
<!-- vscode-markdown-toc-config
35-
numbering=true
36-
autoSave=true
37-
/vscode-markdown-toc-config -->
38-
<!-- /vscode-markdown-toc -->
39-
40-
41-
## 1. <a name='Installation'></a>Installation
42-
43-
```sh
44-
composer require altravia/openapi
45-
```
46-
47-
## 2. <a name='Usage'></a>Usage
48-
49-
### 2.1. <a name='Instanzadellaclasse'></a>Instanza della classe
50-
51-
```php
52-
require_once 'vendor/autoload.php';
53-
54-
$openapi = new \OpenApi\OpenApi($scopes, $user, $apikey, $environment);
55-
```
56-
57-
Dove `$scopes` è un array di stringhe o di oggetti in uno dei seguenti formati:
58-
59-
```php
60-
$scopes = [
61-
"GET:ws.ufficiopostale.com/comuni",
62-
[
63-
"domain"=>"ws.ufficiopostale.com",
64-
"method"=>"comuni",
65-
"mode" =>"GET"
66-
]
67-
];
15+
```bash
16+
composer require openapi/openapi-sdk
6817
```
6918

70-
...e `$environment` è l'ambiente sceltro tra `'test'` (default) e `'production'`
71-
72-
OpenApi si occuperá di reperire automaticamente, o generare, un nuovo token quando necessario.
19+
## Quick Start
7320

74-
A questo punto, in base agli scopes indicati vengono creati i seguenti oggetti:
75-
76-
77-
```php
78-
// Ogni oggetto verrá creato solo se disponibile nello scope.
79-
$openapi->ufficiopostale;
80-
$openapi->comuni;
81-
$openapi->imprese;
82-
$openapi->visengine;
83-
$openapi->marcheTemporali;
84-
$openapi->geocoding;
85-
$openapi->SMS;
86-
$openapi->firmaDigitale;
87-
$openapi->pecMassiva;
88-
```
89-
che possono essere usati al seguente modo:
21+
### Token Generation
9022

9123
```php
92-
$this->openapi->ufficioposale->getCitiesByCap('00132');
93-
```
94-
### 2.2. <a name='Esempi'></a>Esempi
24+
use OpenApi\OauthClient;
9525

96-
```php
97-
require_once 'vendor/autoload.php';
26+
$oauthClient = new OauthClient('username', 'apikey', true); // true for test environment
9827

99-
// Dichiaro gli scopes necessari
10028
$scopes = [
101-
'GET:comuni.openapi.it/cap',
102-
'GET:imprese.altravia.com/advance',
29+
'GET:test.imprese.openapi.it/advance',
30+
'POST:test.postontarget.com/fields/country'
10331
];
10432

105-
$openapi = new OpenApi\OpenApi($scopes, 'my_username','my_api_key', 'test');
106-
107-
// Comuni: prendi informazioni sul cap 00132
108-
$cap = $openapi->comuni->getCitiesByCap('00132');
109-
110-
// Imprese: prendi informazioni su una specifica impresa
111-
$impresa = $openapi->imprese->getByPartitaIva('12485671007');
112-
113-
// Ufficio Postale: ottieni informaizoni sul tracking
114-
$track = $this->openapi->ufficiopostale->track('123456789');
33+
$result = $oauthClient->createToken($scopes, 3600);
34+
$tokenData = json_decode($result, true);
35+
$token = $tokenData['token'];
11536
```
11637

117-
118-
## 3. <a name='Modulocomuni'></a>Modulo comuni
119-
Consente di prendere informazioni su comuni e provincie.
120-
121-
* `getCitiesByCap`
122-
* `getComuneByCatasto`
123-
* `getRegioni`
124-
* `getProvince`
125-
* `getComuni`
126-
127-
### 3.1. <a name='Esempi-1'></a>Esempi
38+
### Making API Calls
12839

12940
```php
130-
$provincia = 'RM';
131-
$comuni = $this->openapi->comuni->getComuni($provincia);
41+
use OpenApi\Client;
13242

133-
var_dump($comuni['comuni']);
134-
/*
43+
$client = new Client($token);
13544

136-
["nome_provincia"]=>
137-
string(4) "Roma"
138-
["sigla_provincia"]=>
139-
string(2) "RM"
140-
["regione"]=>
141-
string(5) "Lazio"
142-
["comuni"]=>
143-
array(121) {
144-
[0]=>
145-
string(6) "Affile"
146-
...
147-
*/
45+
// GET request
46+
$params = ['denominazione' => 'altravia', 'provincia' => 'RM'];
47+
$response = $client->get('https://test.imprese.openapi.it/advance', $params);
14848

49+
// POST request
50+
$payload = ['limit' => 10, 'query' => ['country_code' => 'IT']];
51+
$response = $client->post('https://test.postontarget.com/fields/country', $payload);
14952

53+
// Other HTTP methods
54+
$response = $client->put($url, $payload);
55+
$response = $client->delete($url);
56+
$response = $client->patch($url, $payload);
15057
```
15158

152-
## 4. <a name='Moduloimprese'></a>Modulo imprese
153-
### 4.1. <a name='Utilizzo'></a>Utilizzo
154-
Il modulo imprese espone i seguenti metodi:
155-
* `getByPartitaIva`
156-
* `getClosed`
157-
* `getVatGroup`
158-
* `getPec`
159-
* `getBySearch`
59+
## Architecture
16060

161-
Per `getBySearch` e `getByPartitaIva` è richiesto accesso allo scope `/advance`
61+
This SDK follows a minimal approach with only essential components:
16262

163-
### 4.2. <a name='Esempi-1'></a>Esempi
164-
Utilizziamo `getBySearch` per cercare un'azienda il cui nome inizia con `Altrav` a Roma
165-
166-
```php
167-
$autocomplete = $this->openapi->imprese->getBySearch('Altrav*', 'RM');
168-
169-
/*
170-
[0]=>
171-
object(stdClass)#41 (10) {
172-
["piva"]=>
173-
string(11) "12485671007"
174-
["cf"]=>
175-
string(11) "12485671007"
176-
["denominazione"]=>
177-
string(20) "ALTRAVIA SERVIZI SRL"
178-
[1]=>
179-
object(stdClass)#42 (10) {
180-
["id"]=>
181-
string(24) "4242424242"
182-
["denominazione"]=>
183-
string(18) "xxx Altravia Esempio 2"
184-
...
185-
*/
186-
```
63+
- `OauthClient`: Handles OAuth authentication and token management
64+
- `Client`: Agnostic HTTP client for API calls
65+
- `Exception`: Error handling
66+
- `Cache\CacheInterface`: Optional caching interface
18767

188-
## 5. <a name='ModuloMarcheTemporali'></a>Modulo Marche Temporali
189-
* `availability`
190-
* `checkLotto`
191-
* `purcahse`
68+
## Requirements
19269

193-
### 5.1. <a name='Esempi-1'></a>Esempi
194-
195-
```php
196-
// Controlliamo la disponibilitá di una marca di inforcert o aruba
197-
$disponibilita = $this->openapi->marcheTemporali->availability('infocert', 1);
198-
199-
// Se le marche sono disponibili, acquistiamone una
200-
if ($disponibilita->availability > 0) {
201-
try {
202-
$marca = $this->openapi->marcheTemporali->purcahse('infocert', 1);
203-
} catch (\OpenApi\classes\exception\OpenApiMarcheTemporaliException $e) {
204-
error_log(var_dump($e));
205-
}
206-
}
207-
```
208-
209-
## 6. <a name='ModuloSMS'></a>Modulo SMS
210-
* `getRecipients`
211-
* `getMessage`
212-
* `sendMore`
213-
* `sendOne`
214-
215-
### 6.1. <a name='InviareunSMS'></a>Inviare un SMS
216-
Per inviare un SMS, per prima cosa definiamo i destinatari:
217-
218-
```php
219-
$recipient = '+39-3939989741';
220-
// OR
221-
$recipients = [
222-
[
223-
'number' => '+39-3939989741',
224-
'fields' => ['nome' => 'NomeDestinatario']
225-
]
226-
];
227-
```
228-
229-
Possiamo ora procedere ad inviare un SMS:
230-
```php
231-
232-
try {
233-
$priority = 1;
234-
$options = null;
235-
$singleSms = $this->openapi->SMS->sendOne('Nome del mittente', $recipient, 'lorem ipsum', null, $priority, $options);
236-
} catch (\OpenApi\classes\exception\OpenApiConnectionsException $e) {
237-
throw 'Non è stato possibile recapitare il messaggio';
238-
}
239-
```
240-
241-
Possiamo anche speficiare i prefissi in modo indipendente:
242-
```php
243-
$this->openapi->SMS->sendOne('Nome del mittente', '3939989741', 'lorem ipsum', '+42', 1, null);
244-
```
245-
246-
O passare delle opzioni
247-
```php
248-
$options = ['timestamp_send' => '2021-04-20']
249-
$this->openapi->SMS->sendOne('Nome del mittente', '3939989741', 'lorem ipsum', '+42', 1, $options);
250-
```
251-
252-
## 7. <a name='ModuloVisengine'></a>Modulo Visengine
253-
Come prima cosa, settiamo l'hash della visura che vogliamo richiedere
254-
255-
```php
256-
// https://developers.openapi.it/services/visengine
257-
$this->openapi->visengine->setHash($visura->hash);
258-
```
259-
260-
A questo punto, possiamo lanciare `createRequest`, che ritornerà una istanza vuota della visura che andremo a creare della struttura richiesta
261-
262-
```php
263-
$request = $this->openapi->visengine->createRequest();
264-
```
265-
266-
Prodediamo a completare l'oggetto, che potremmo passare a sendRequest quando pronto
267-
268-
```php
269-
$request->setJson(['$0' => 'abcd', '$1' => '12485671007']);
270-
// url di callback, oggetto con dati aggiuntivi, metodo
271-
$request->setCallbackData('https://example.com', new stdClass(), 'POST');
272-
$visura = $this->openapi->visengine->sendRequest($request);
273-
```
70+
- PHP 8.0 or higher
71+
- cURL extension
72+
- JSON extension

composer.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "openapi/openapi-sdk",
3-
"description": "OpenApi PHP Libraries (https://openapi.com)",
3+
"description": "Minimal and agnostic PHP SDK for OpenAPI (https://openapi.com)",
44
"authors": [
55
{
66
"name": "Altravia",
@@ -9,7 +9,9 @@
99
],
1010
"minimum-stability": "stable",
1111
"require": {
12-
"php": ">=7.2.0"
12+
"php": ">=8.0.0",
13+
"ext-curl": "*",
14+
"ext-json": "*"
1315
},
1416
"autoload": {
1517
"psr-4": {

examples/api_calls.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
require_once __DIR__ . '/../vendor/autoload.php';
4+
5+
use OpenApi\Client;
6+
7+
try {
8+
$token = '<your_access_token>';
9+
$client = new Client($token);
10+
11+
// GET request with parameters
12+
$params = [
13+
'denominazione' => 'altravia',
14+
'provincia' => 'RM',
15+
'codice_ateco' => '6201'
16+
];
17+
18+
$result = $client->get('https://test.imprese.openapi.it/advance', $params);
19+
echo "GET API Response: " . $result . PHP_EOL;
20+
21+
// POST request with payload
22+
$payload = [
23+
'limit' => 10,
24+
'query' => [
25+
'country_code' => 'IT'
26+
]
27+
];
28+
29+
$result = $client->post('https://test.postontarget.com/fields/country', $payload);
30+
echo "POST API Response: " . $result . PHP_EOL;
31+
32+
} catch (Exception $e) {
33+
echo "Error: " . $e->getMessage() . PHP_EOL;
34+
}

0 commit comments

Comments
 (0)