-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Company Search * Company Details * Company Officers * Company Financial Statements * Company Credit Reports
- Loading branch information
1 parent
5cab4b9
commit 5a459a5
Showing
19 changed files
with
1,393 additions
and
0 deletions.
There are no files selected for viewing
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,2 @@ | ||
/vendor/ | ||
composer.lock |
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,47 @@ | ||
# Creditsafe API | ||
``` | ||
composer require synergitech/creditsafe-connect | ||
``` | ||
|
||
## Usage | ||
|
||
### Setting up Client | ||
```php | ||
$config = [ | ||
'username' => 'username', | ||
'password' => 'password' | ||
]; | ||
|
||
$creditsafe = new \SynergiTech\Creditsafe\Client($config); | ||
``` | ||
|
||
### Access countries and their codes | ||
```php | ||
$creditsafe->countries()->access(); | ||
``` | ||
|
||
### Search criteria using country code | ||
```php | ||
|
||
$creditsafe->companies()->searchCriteria(['countries' => 'GB']); | ||
|
||
|
||
``` | ||
### Company search pagination | ||
```php | ||
$search = $creditsafe->companies()->search(['countries' => 'GB', 'name' => 'GOOGLE UK LIMITED']); | ||
$search->setPageSize(100); | ||
foreach ($search as $result) { | ||
$company = $result->get(); | ||
} | ||
``` | ||
|
||
### Get company report | ||
```php | ||
$creditsafe->companies()->get('GB001-0-03977902'); | ||
``` | ||
|
||
## Running tests | ||
``` | ||
vendor/bin/phpunit tests | ||
``` |
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,25 @@ | ||
{ | ||
"name": "synergitech/creditsafe-connect", | ||
"type": "library", | ||
"require": { | ||
"php": ">=7.1", | ||
"guzzlehttp/guzzle": "^6.3", | ||
"lcobucci/jwt": "^3.2.5" | ||
}, | ||
"license": "MIT", | ||
"authors": [ | ||
{ | ||
"name": "Sean Lane", | ||
"email": "[email protected]" | ||
} | ||
], | ||
"autoload": { | ||
"psr-4": { | ||
"SynergiTech\\Creditsafe\\" : "src/", | ||
"SynergiTech\\Creditsafe\\Tests\\" : "tests/" | ||
} | ||
}, | ||
"require-dev": { | ||
"phpunit/phpunit": "^7.5" | ||
} | ||
} |
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,10 @@ | ||
<?php | ||
|
||
return [ | ||
[ | ||
[ | ||
'body' => json_encode(['message' => 'authentication failed']), | ||
'code' => 400, | ||
], | ||
] | ||
]; |
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 | ||
|
||
return [ | ||
[ | ||
[ | ||
'body' => json_encode(['token' => 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJzdWIiOiIyMTQ4MzAxZC04Y2MzLTQyNjgtODY5MC00MmI4M2MzNzU4NGUiLCJlbWFpbF92ZXJpZmllZCI6dHJ1ZSwiaXNzIjoiaHR0cHM6Ly9jb2duaXRvLWlkcC5ldS13ZXN0LTEuYW1hem9uYXdzLmNvbS8iLCJjb2duaXRvOnVzZXJuYW1lIjoiZXhhbXBsZUBleGFtcGxlLm9yZyIsImN1c3RvbTpjc1VzZXJJZCI6IjEyMzQ1NiIsImF1ZCI6IjEyMzQ1NiIsImN1c3RvbTpjc1VzZXJDb3VudHJ5Q29kZSI6IlVLIiwiZXZlbnRfaWQiOiIyMTQ4MzAxZC04Y2MzLTQyNjgtODY5MC00MmI4M2MzNzU4NGYiLCJ0b2tlbl91c2UiOiJpZCIsImF1dGhfdGltZSI6MTU0NDQzNjE4MiwiZXhwIjoxNTQ0NDM5OTU4LCJpYXQiOjE1NDQ0MzYxODIsImVtYWlsIjoiZXhhbXBsZUBleGFtcGxlLm9yZyIsImp0aSI6ImY1NTBhN2I4LTA2OTItNDk1Ny04ZGFiLTFhMjUzN2M3ZDU3MyJ9.xZ_aCeEEq9zweOzm8FqCIhddQ0dWi_c7XyfxOWchSPg']), | ||
], | ||
] | ||
]; |
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,234 @@ | ||
<?php | ||
|
||
namespace SynergiTech\Creditsafe; | ||
|
||
use Lcobucci\JWT\Parser; | ||
use Lcobucci\JWT\Token; | ||
use GuzzleHttp\Psr7; | ||
|
||
/** | ||
* Client Class used to store data relating to the client created | ||
*/ | ||
class Client | ||
{ | ||
private $http_client; | ||
private $token; | ||
private $config; | ||
|
||
/** | ||
* construct function that builds the client class | ||
* @param array $config creditsafe configuration | ||
*/ | ||
public function __construct(array $config = []) | ||
{ | ||
$this->config = array_merge($this->getDefaultConfig(), $config); | ||
|
||
if (isset($this->config['http_client'])) { | ||
$this->http_client = $this->config['http_client']; | ||
} else { | ||
$this->http_client = new \GuzzleHttp\Client([ | ||
'base_uri' => $this->getBaseURL(), | ||
]); | ||
} | ||
} | ||
|
||
/** | ||
* This function is used to authenticate a username and password | ||
* @return void | ||
* @throws Exception\Unauthorized if the provided authentication details are not valid | ||
*/ | ||
public function authenticate() : void | ||
{ | ||
try { | ||
$authenticate = $this->http_client->request('POST', 'authenticate', [ | ||
'json'=> [ | ||
'username'=> $this->config['username'], | ||
'password' => $this->config['password'] | ||
] | ||
]); | ||
} catch (\GuzzleHttp\Exception\ClientException $e) { | ||
$message = 'There was a problem authenticating with the Creditsafe API'; | ||
if ($e->hasResponse()) { | ||
$body = $e->getResponse()->getBody(); | ||
$details = json_decode($body, true); | ||
if (json_last_error() === JSON_ERROR_NONE && isset($details['message'])) { | ||
$message = $details['message']; | ||
} | ||
} | ||
throw new Exception\Unauthorized($message, 400, $e); | ||
} | ||
$decode = json_decode((string) $authenticate->getBody(), true); | ||
$this->setToken($decode['token']); | ||
} | ||
|
||
/** | ||
* set token | ||
* @param string $token Token must be set to have access | ||
* to the api and is only valid for an hour | ||
* @return void | ||
*/ | ||
public function setToken(string $token) : void | ||
{ | ||
$this->token = (new Parser())->parse($token); | ||
} | ||
|
||
public function getToken() : Token | ||
{ | ||
return $this->token; | ||
} | ||
|
||
/** | ||
* Checks if token is valid | ||
* @return void | ||
*/ | ||
public function checkToken() : void | ||
{ | ||
if ($this->token === null || $this->token->isExpired()) { | ||
$this->authenticate(); | ||
} | ||
} | ||
|
||
/** | ||
* This request function handles all requests for the api | ||
* @param string $type Sets the type of HTTP Request e.g GET ,POST | ||
* @param string $endpoint Sets the endpoint | ||
* @param array $params Sets params for a endpoint | ||
* @return array Returns the results of the endpoint | ||
*/ | ||
public function request(string $type, string $endpoint, array $params = []) : array | ||
{ | ||
|
||
$this->checkToken(); | ||
|
||
$guzzleArgs = [ | ||
'headers' => [ | ||
'Authorization' => (string) $this->token | ||
], | ||
]; | ||
|
||
if ($type == 'GET') { | ||
$guzzleArgs['query'] = $params; | ||
} else { | ||
$guzzleArgs['json'] = $params; | ||
} | ||
|
||
try { | ||
$res = $this->http_client->request($type, $endpoint, $guzzleArgs); | ||
} catch (\GuzzleHttp\Exception\ClientException $e) { | ||
$message = $e->getMessage(); | ||
$correlationID = null; | ||
|
||
if ($e->hasResponse()) { | ||
$body = $e->getResponse()->getBody(); | ||
$details = json_decode($body, true); | ||
|
||
if (json_last_error() === JSON_ERROR_NONE) { | ||
$apiMessage = []; | ||
if (isset($details['message'])) { | ||
$apiMessage[] = $details['message']; | ||
} | ||
if (isset($details['details'])) { | ||
$apiMessage[] = $details['details']; | ||
} | ||
if (!empty($apiMessage)) { | ||
$message = implode(': ', $apiMessage); | ||
} | ||
if (isset($details['correlationId'])) { | ||
$correlationID = $details['correlationId']; | ||
} | ||
} | ||
} | ||
|
||
$exception = new Exception\APIException($message, 400, $e); | ||
if ($correlationID) { | ||
$exception->setCorrelationID($correlationID); | ||
} | ||
throw $exception; | ||
} | ||
|
||
$res = (array)json_decode((string) $res->getBody(), true); | ||
|
||
return $res; | ||
} | ||
|
||
/** | ||
* A function that handles the creation of a GET Request | ||
* @param string $endpoint An endpoint used to create an request | ||
* @param array $params Sets params for a endpoint | ||
* @return array Returns the results of the endpoint | ||
*/ | ||
public function get(string $endpoint, array $params = []) : array | ||
{ | ||
return $this->request('GET', $endpoint, $params); | ||
} | ||
|
||
/** | ||
* Get Company Events | ||
* @return Service\CompanyEventService Returns Company Events | ||
*/ | ||
public function monitoring() : Service\CompanyEventService | ||
{ | ||
if (!isset($this->monitor)) { | ||
$this->monitor = new Service\CompanyEventService($this); | ||
} | ||
return $this->monitor; | ||
} | ||
|
||
/** | ||
* Get company services | ||
* @return Service\CompanyService Returns Company Services | ||
*/ | ||
public function companies() : Service\CompanyService | ||
{ | ||
if (!isset($this->company)) { | ||
$this->company = new Service\CompanyService($this); | ||
} | ||
return $this->company; | ||
} | ||
|
||
/** | ||
* Get Countries | ||
* @return Service\CountryService Returns the Countries | ||
*/ | ||
public function countries() : Service\CountryService | ||
{ | ||
if (!isset($this->countries)) { | ||
$this->countries = new Service\CountryService($this); | ||
} | ||
return $this->countries; | ||
} | ||
|
||
public function getDefaultConfig() : array | ||
{ | ||
return [ | ||
'apiURI' => 'https://connect.creditsafe.com/', | ||
]; | ||
} | ||
|
||
/** | ||
* Function gets the base url for the api by concatenating the API URL and the API version | ||
* @return string Returns a string containing the base url | ||
*/ | ||
public function getBaseURL() : string | ||
{ | ||
return $this->getApiURL().$this->getApiVersion().'/'; | ||
} | ||
|
||
/** | ||
* Function gets the Api url | ||
* @return string Returns a string containing the api url | ||
*/ | ||
private function getApiURL() : string | ||
{ | ||
return $this->config['apiURI']; | ||
} | ||
|
||
/** | ||
* Function gets the Api Version | ||
* @return string Returns a string containing the Api Version | ||
*/ | ||
private function getApiVersion() : string | ||
{ | ||
return 'v1'; | ||
} | ||
} |
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,29 @@ | ||
<?php | ||
|
||
namespace SynergiTech\Creditsafe\Exception; | ||
|
||
/** | ||
* This class is used to handle API Expections | ||
*/ | ||
class APIException extends \RuntimeException | ||
{ | ||
private $correlationID = null; | ||
|
||
/** | ||
* Sets the CorrelationID | ||
* @param string $correlationID Contains the CorrelationID | ||
*/ | ||
public function setCorrelationID(string $correlationID) : void | ||
{ | ||
$this->correlationID = $correlationID; | ||
} | ||
|
||
/** | ||
* Get CorrelationID | ||
* @return ?string Contains the CorrelationID | ||
*/ | ||
public function getCorrelationID() : ?string | ||
{ | ||
return $this->correlationID; | ||
} | ||
} |
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,7 @@ | ||
<?php | ||
|
||
namespace SynergiTech\Creditsafe\Exception; | ||
|
||
class Unauthorized extends \RuntimeException | ||
{ | ||
} |
Oops, something went wrong.