Skip to content

Instantly share code, notes, and snippets.

@judgej
Created May 1, 2019 10:59
Show Gist options
  • Save judgej/cd7890a6789c4c31bef70c456f0ca738 to your computer and use it in GitHub Desktop.
Save judgej/cd7890a6789c4c31bef70c456f0ca738 to your computer and use it in GitHub Desktop.
<?php
namespace App\Services;
use GuzzleHttp\Client;
class DataStoreApi
{
// set public variables
public $client;
public $uri;
public $headers;
public $baseUrl;
public function __construct()
{
// set our base url from the config
$this->baseUrl = config('services.datastore.baseurl');
// set our headers array from the config (bearer token and accept)
$this->headers = config('services.datastore.headers');
// create instance of client
$this->client = new Client();
}
public function get(array $params = [])
{
try {
// create the url with the set base url then the uri (the end of the url) that is set from one of functions (provisioningRequests())
$url = $this->baseUrl . $this->uri;
// do the get request and store the response
$response = $this->client->request(
'GET', $url, [
'headers' => $this->headers,
'parameters' => $params
]);
// return the response
return $response;
}
catch (\Exception $e) {
return $e->getCode();
}
}
public function create($body)
{
try {
// create the url with the set base url then the uri (the end of the url) that is set from one of functions (provisioningRequests())
$url = $this->baseUrl . $this->uri;
// do the get request and store the response
$response = $this->client->request(
'POST', $url, [
'headers' => $this->headers,
'body' => json_encode($body)
]);
// return the response
return $response;
}
catch (\Exception $e) {
return $e;
}
}
public function getUsers()
{
$this->uri = '/api/users/';
return $this;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment