-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from Ticketpark/v2
- Loading branch information
Showing
14 changed files
with
720 additions
and
370 deletions.
There are no files selected for viewing
File renamed without changes.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,86 @@ | ||
# Ticketpark PHP API Client | ||
|
||
A basic api client to consume the Ticketpark REST API. | ||
[![Build Status](https://github.com/Ticketpark/php-api-client/actions/workflows/ci.yml/badge.svg)](https://github.com/sprain/php-swiss-qr-bill/actions) | ||
|
||
|
||
A PHP client to consume the Ticketpark REST API. | ||
|
||
## Installation | ||
|
||
Simply add this library to your composer.json: | ||
Add this library to your composer.json: | ||
|
||
``` | ||
composer require ticketpark/php-api-client | ||
``` | ||
|
||
## Usage | ||
See example.php | ||
|
||
Also see `example.php`. | ||
|
||
### Getting data (GET) | ||
|
||
```php | ||
<?php | ||
|
||
include('vendor/autoload.php'); | ||
|
||
$client = new \Ticketpark\ApiClient\TicketparkApiClient('yourApiKey', 'yourApiSecret'); | ||
$client->setUserCredentials('[email protected]', 'yourPassword'); | ||
|
||
$response = $client->get('/events/', ['maxResults' => 2]); | ||
|
||
if ($response->isSuccessful()) { | ||
$data = $response->getContent(); | ||
} | ||
``` | ||
|
||
### Creating data (POST) | ||
|
||
```php | ||
<?php | ||
|
||
include('vendor/autoload.php'); | ||
|
||
$client = new \Ticketpark\ApiClient\TicketparkApiClient('yourApiKey', 'yourApiSecret'); | ||
$client->setUserCredentials('[email protected]', 'yourPassword'); | ||
|
||
$response = $client->post('/events/', [ | ||
'host' => 'yourHostPid', | ||
'name' => 'Some great event', | ||
'currency' => 'CHF' | ||
]); | ||
|
||
if ($response->isSuccessful()) { | ||
$pidOfNewEvent = $response->getGeneratedPid(); | ||
|
||
// if you created a collection of records, the response will contain a link instead | ||
// that can be used to fetch the data of the newly generated records. | ||
// | ||
// $path = $response->getGeneratedListLink(); | ||
// $newResponse = $client->get($path); | ||
} | ||
``` | ||
|
||
### Updating data (PATCH) | ||
|
||
```php | ||
<?php | ||
|
||
include('vendor/autoload.php'); | ||
|
||
$client = new \Ticketpark\ApiClient\TicketparkApiClient('yourApiKey', 'yourApiSecret'); | ||
$client->setUserCredentials('[email protected]', 'yourPassword'); | ||
|
||
$response = $client->patch('/events/yourEventPid', [ | ||
'name' => 'Some changed event name' | ||
] | ||
|
||
if ($response->isSuccessful()) { | ||
// Data was successfully updated | ||
} | ||
``` | ||
|
||
|
||
## User credentials | ||
Get in touch with us to get your user credentials: | ||
[tech@ticketpark.ch](mailto:[email protected]), [www.ticketpark.ch](http://www.ticketpark.ch) | ||
Get in touch with us to get your user credentials:<br> | ||
[support@ticketpark.ch](mailto:support@ticketpark.ch) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Ticketpark\ApiClient\Exception; | ||
|
||
class HttpRequestException extends \Exception | ||
{ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Ticketpark\ApiClient\Exception; | ||
|
||
class HttpTimeOutException extends \Exception | ||
{ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Ticketpark\ApiClient\Exception; | ||
|
||
class UnexpectedResponseException extends \Exception | ||
{ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Ticketpark\ApiClient\Http; | ||
|
||
use GuzzleHttp\Client as GuzzleClient; | ||
use GuzzleHttp\Exception\ClientException; | ||
use GuzzleHttp\Exception\ConnectException; | ||
use GuzzleHttp\Psr7\Response as GuzzleResponse; | ||
use Ticketpark\ApiClient\Exception\HttpRequestException; | ||
use Ticketpark\ApiClient\Exception\HttpTimeOutException; | ||
|
||
final class Client implements ClientInterface | ||
{ | ||
private readonly GuzzleClient $guzzle; | ||
|
||
public function __construct() | ||
{ | ||
$this->guzzle = new GuzzleClient(); | ||
} | ||
|
||
public function head(string $url, array $headers): Response | ||
{ | ||
return $this->execute('head', $url, $headers); | ||
} | ||
|
||
public function get(string $url, array $headers): Response | ||
{ | ||
return $this->execute('get', $url, $headers); | ||
} | ||
|
||
public function post(string $url, string $content, array $headers): Response | ||
{ | ||
return $this->execute('post', $url, $headers, $content); | ||
} | ||
|
||
public function postForm(string $url, array $formData, array $headers): Response | ||
{ | ||
return $this->execute('post', $url, $headers, null, $formData); | ||
} | ||
|
||
public function patch(string $url, string $content, array $headers): Response | ||
{ | ||
return $this->execute('patch', $url, $headers, $content); | ||
} | ||
|
||
public function delete(string $url, array $headers): Response | ||
{ | ||
return $this->execute('delete', $url, $headers); | ||
} | ||
|
||
private function execute( | ||
string $method, | ||
string $url, | ||
array $headers = [], | ||
string $content = null, | ||
array $formData = [] | ||
): Response { | ||
try { | ||
$guzzleResponse = $this->doExecute( | ||
$method, | ||
$url, | ||
$headers, | ||
$content, | ||
$formData | ||
); | ||
|
||
} catch (ConnectException $e) { | ||
if (str_contains($e->getMessage(), 'cURL error 28')) { | ||
throw new HttpTimeOutException(); | ||
} | ||
|
||
} catch (ClientException $e) { | ||
/** @var GuzzleResponse $response */ | ||
$guzzleResponse = $e->getResponse(); | ||
|
||
} catch (\Exception $e) { | ||
throw new HttpRequestException($e->getMessage()); | ||
} | ||
|
||
return new Response( | ||
$guzzleResponse->getStatusCode(), | ||
(string) $guzzleResponse->getBody(), | ||
$guzzleResponse->getHeaders() | ||
); | ||
} | ||
|
||
private function doExecute( | ||
string $method, | ||
string $url, | ||
array $headers, | ||
?string $content, | ||
array $formData | ||
): GuzzleResponse { | ||
$requestData = [ | ||
'headers' => $headers, | ||
'timeout' => 30 | ||
]; | ||
|
||
if ($formData) { | ||
$requestData['form_params'] = $formData; | ||
} else { | ||
$requestData['body'] = $content; | ||
} | ||
|
||
/** @var GuzzleResponse $response */ | ||
$guzzleResponse = $this->guzzle->request( | ||
$method, | ||
$url, | ||
$requestData | ||
); | ||
|
||
return $guzzleResponse; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Ticketpark\ApiClient\Http; | ||
|
||
interface ClientInterface | ||
{ | ||
public function head(string $url, array $headers): Response; | ||
|
||
public function get(string $url, array $headers): Response; | ||
|
||
public function post(string $url, string $content, array $headers): Response; | ||
|
||
public function postForm(string $url, array $formData, array $headers): Response; | ||
|
||
public function patch(string $url, string $content, array $headers): Response; | ||
|
||
public function delete(string $url, array $headers): Response; | ||
} |
Oops, something went wrong.