-
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 #311 from wri/release/jovial-jacaranda
[RELEASE] Jovial Jacaranda
- Loading branch information
Showing
245 changed files
with
11,975 additions
and
1,309 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
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,77 @@ | ||
<?php | ||
|
||
namespace App\Clients; | ||
|
||
use App\Exceptions\ExternalAPIException; | ||
use GuzzleHttp\Client; | ||
use GuzzleHttp\Exception\GuzzleException; | ||
use Illuminate\Support\Facades\Log; | ||
|
||
class GreenhouseClient | ||
{ | ||
protected string $url; | ||
|
||
protected string $token; | ||
|
||
protected Client $client; | ||
|
||
public function __construct(Client $client) | ||
{ | ||
$this->url = config('services.greenhouse_api.url'); | ||
$this->token = config('services.greenhouse_api.token'); | ||
$this->client = $client; | ||
} | ||
|
||
public function getEnabled(): bool | ||
{ | ||
return ! empty($this->url) && ! empty($this->token); | ||
} | ||
|
||
/** | ||
* @throws ExternalAPIException | ||
*/ | ||
public function notifyPolygonUpdated(string $polygonUuid): ?array | ||
{ | ||
return $this->runQuery('tmNotifyFeatureUpdated', $polygonUuid); | ||
} | ||
|
||
/** | ||
* @throws ExternalAPIException | ||
*/ | ||
public function notifyMediaDeleted(string $mediaUuid): ?array | ||
{ | ||
return $this->runQuery('tmNotifyMediaDeleted', $mediaUuid); | ||
} | ||
|
||
/** | ||
* @throws ExternalAPIException | ||
*/ | ||
protected function runQuery(string $functionName, string $uuid): ?array | ||
{ | ||
if (! $this->getEnabled()) { | ||
return null; | ||
} | ||
|
||
try { | ||
$response = $this->client->request('POST', $this->url, [ | ||
'headers' => [ | ||
'api-key' => $this->token, | ||
'Content-Type' => 'application/json', | ||
], | ||
'body' => json_encode([ | ||
'query' => 'mutation ($uuid: Id!) { ' . $functionName . '(uuid: $uuid) { ok } }', | ||
'variables' => [ 'uuid' => $uuid ], | ||
]), | ||
]); | ||
} catch (GuzzleException $exception) { | ||
Log::error( | ||
"Exception sending query to Greenhouse [fn=$functionName, uuid=$uuid]: " . | ||
$exception->getMessage() | ||
); | ||
|
||
throw new ExternalAPIException($exception->getMessage(), $exception->getCode(), $exception); | ||
} | ||
|
||
return json_decode($response->getBody()->getContents(), true); | ||
} | ||
} |
Oops, something went wrong.