Skip to content

Commit c90a59e

Browse files
authored
Merge pull request #2 from Ticketpark/v2
2 parents 8f99090 + e13f765 commit c90a59e

14 files changed

+720
-370
lines changed
File renamed without changes.

.travis.yml

Lines changed: 0 additions & 11 deletions
This file was deleted.

README.md

Lines changed: 73 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,86 @@
11
# Ticketpark PHP API Client
22

3-
A basic api client to consume the Ticketpark REST API.
3+
[![Build Status](https://github.com/Ticketpark/php-api-client/actions/workflows/ci.yml/badge.svg)](https://github.com/sprain/php-swiss-qr-bill/actions)
4+
5+
6+
A PHP client to consume the Ticketpark REST API.
47

58
## Installation
69

7-
Simply add this library to your composer.json:
10+
Add this library to your composer.json:
811

912
```
1013
composer require ticketpark/php-api-client
1114
```
1215

1316
## Usage
14-
See example.php
17+
18+
Also see `example.php`.
19+
20+
### Getting data (GET)
21+
22+
```php
23+
<?php
24+
25+
include('vendor/autoload.php');
26+
27+
$client = new \Ticketpark\ApiClient\TicketparkApiClient('yourApiKey', 'yourApiSecret');
28+
$client->setUserCredentials('[email protected]', 'yourPassword');
29+
30+
$response = $client->get('/events/', ['maxResults' => 2]);
31+
32+
if ($response->isSuccessful()) {
33+
$data = $response->getContent();
34+
}
35+
```
36+
37+
### Creating data (POST)
38+
39+
```php
40+
<?php
41+
42+
include('vendor/autoload.php');
43+
44+
$client = new \Ticketpark\ApiClient\TicketparkApiClient('yourApiKey', 'yourApiSecret');
45+
$client->setUserCredentials('[email protected]', 'yourPassword');
46+
47+
$response = $client->post('/events/', [
48+
'host' => 'yourHostPid',
49+
'name' => 'Some great event',
50+
'currency' => 'CHF'
51+
]);
52+
53+
if ($response->isSuccessful()) {
54+
$pidOfNewEvent = $response->getGeneratedPid();
55+
56+
// if you created a collection of records, the response will contain a link instead
57+
// that can be used to fetch the data of the newly generated records.
58+
//
59+
// $path = $response->getGeneratedListLink();
60+
// $newResponse = $client->get($path);
61+
}
62+
```
63+
64+
### Updating data (PATCH)
65+
66+
```php
67+
<?php
68+
69+
include('vendor/autoload.php');
70+
71+
$client = new \Ticketpark\ApiClient\TicketparkApiClient('yourApiKey', 'yourApiSecret');
72+
$client->setUserCredentials('[email protected]', 'yourPassword');
73+
74+
$response = $client->patch('/events/yourEventPid', [
75+
'name' => 'Some changed event name'
76+
]
77+
78+
if ($response->isSuccessful()) {
79+
// Data was successfully updated
80+
}
81+
```
82+
1583

1684
## User credentials
17-
Get in touch with us to get your user credentials:
18-
[tech@ticketpark.ch](mailto:[email protected]), [www.ticketpark.ch](http://www.ticketpark.ch)
85+
Get in touch with us to get your user credentials:<br>
86+
[support@ticketpark.ch](mailto:support@ticketpark.ch)

composer.json

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
"description": "A PHP client to use the Ticketpark API",
44
"type": "library",
55
"require": {
6-
"php": ">=8.1",
7-
"kriswallsmith/buzz": "^0.15.0"
6+
"php": "^8.1|^8.2",
7+
"guzzlehttp/guzzle": "^6.5|^7.5"
88
},
99
"license": "MIT",
1010
"authors": [
@@ -23,6 +23,7 @@
2323
"require-dev": {
2424
"phpunit/phpunit": "^9.6",
2525
"rector/rector": "^0.18.3",
26-
"friendsofphp/php-cs-fixer": "^3.27"
26+
"friendsofphp/php-cs-fixer": "^3.27",
27+
"phpspec/prophecy": "^1.17"
2728
}
2829
}

example.php

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,24 +12,21 @@
1212
// With frequent requests, re-using tokens results in less api requests than using user credentials only.
1313
//
1414
// $client->setAccessToken('someAccessTokenString');
15-
// or $client->setAccessTokenInstance(new AccessToken($string, $expiration));
1615
// $client->setRefreshToken('someRefreshToken');
17-
// or $client->setRefreshTokenInstance($string, $expiration);
1816

1917
// 3. Execute the desired command
20-
$response = $client->get('/events/', array('maxResults' => 2));
18+
$response = $client->get('/events/', ['maxResults' => 2]);
2119

2220
// 4. Handle the response
23-
// It is an instance of Buzz\Message\Response
2421
if ($response->isSuccessful()) {
25-
print "<strong>Request successful!</strong><br>";
26-
$events = json_decode($response->getContent(), true);
22+
print "Request successful!\n\n";
2723

24+
$events = $response->getContent();
2825
foreach($events as $event) {
29-
print $event['name']."<br>";
26+
print $event['name']."\n";
3027
}
3128
}
3229

33-
// 5. Get the tokens and store them to use them again later on
30+
// 5. Recommended: Get the tokens and store them to use them again later on
3431
$myAccessToken = $client->getAccessToken();
3532
$myRefreshToken = $client->getRefreshToken();
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Ticketpark\ApiClient\Exception;
6+
7+
class HttpRequestException extends \Exception
8+
{
9+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Ticketpark\ApiClient\Exception;
6+
7+
class HttpTimeOutException extends \Exception
8+
{
9+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Ticketpark\ApiClient\Exception;
6+
7+
class UnexpectedResponseException extends \Exception
8+
{
9+
}

lib/ApiClient/Http/Client.php

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Ticketpark\ApiClient\Http;
6+
7+
use GuzzleHttp\Client as GuzzleClient;
8+
use GuzzleHttp\Exception\ClientException;
9+
use GuzzleHttp\Exception\ConnectException;
10+
use GuzzleHttp\Psr7\Response as GuzzleResponse;
11+
use Ticketpark\ApiClient\Exception\HttpRequestException;
12+
use Ticketpark\ApiClient\Exception\HttpTimeOutException;
13+
14+
final class Client implements ClientInterface
15+
{
16+
private readonly GuzzleClient $guzzle;
17+
18+
public function __construct()
19+
{
20+
$this->guzzle = new GuzzleClient();
21+
}
22+
23+
public function head(string $url, array $headers): Response
24+
{
25+
return $this->execute('head', $url, $headers);
26+
}
27+
28+
public function get(string $url, array $headers): Response
29+
{
30+
return $this->execute('get', $url, $headers);
31+
}
32+
33+
public function post(string $url, string $content, array $headers): Response
34+
{
35+
return $this->execute('post', $url, $headers, $content);
36+
}
37+
38+
public function postForm(string $url, array $formData, array $headers): Response
39+
{
40+
return $this->execute('post', $url, $headers, null, $formData);
41+
}
42+
43+
public function patch(string $url, string $content, array $headers): Response
44+
{
45+
return $this->execute('patch', $url, $headers, $content);
46+
}
47+
48+
public function delete(string $url, array $headers): Response
49+
{
50+
return $this->execute('delete', $url, $headers);
51+
}
52+
53+
private function execute(
54+
string $method,
55+
string $url,
56+
array $headers = [],
57+
string $content = null,
58+
array $formData = []
59+
): Response {
60+
try {
61+
$guzzleResponse = $this->doExecute(
62+
$method,
63+
$url,
64+
$headers,
65+
$content,
66+
$formData
67+
);
68+
69+
} catch (ConnectException $e) {
70+
if (str_contains($e->getMessage(), 'cURL error 28')) {
71+
throw new HttpTimeOutException();
72+
}
73+
74+
} catch (ClientException $e) {
75+
/** @var GuzzleResponse $response */
76+
$guzzleResponse = $e->getResponse();
77+
78+
} catch (\Exception $e) {
79+
throw new HttpRequestException($e->getMessage());
80+
}
81+
82+
return new Response(
83+
$guzzleResponse->getStatusCode(),
84+
(string) $guzzleResponse->getBody(),
85+
$guzzleResponse->getHeaders()
86+
);
87+
}
88+
89+
private function doExecute(
90+
string $method,
91+
string $url,
92+
array $headers,
93+
?string $content,
94+
array $formData
95+
): GuzzleResponse {
96+
$requestData = [
97+
'headers' => $headers,
98+
'timeout' => 30
99+
];
100+
101+
if ($formData) {
102+
$requestData['form_params'] = $formData;
103+
} else {
104+
$requestData['body'] = $content;
105+
}
106+
107+
/** @var GuzzleResponse $response */
108+
$guzzleResponse = $this->guzzle->request(
109+
$method,
110+
$url,
111+
$requestData
112+
);
113+
114+
return $guzzleResponse;
115+
}
116+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Ticketpark\ApiClient\Http;
6+
7+
interface ClientInterface
8+
{
9+
public function head(string $url, array $headers): Response;
10+
11+
public function get(string $url, array $headers): Response;
12+
13+
public function post(string $url, string $content, array $headers): Response;
14+
15+
public function postForm(string $url, array $formData, array $headers): Response;
16+
17+
public function patch(string $url, string $content, array $headers): Response;
18+
19+
public function delete(string $url, array $headers): Response;
20+
}

0 commit comments

Comments
 (0)