Skip to content

Commit

Permalink
PSR-4; Guzzle 6.x
Browse files Browse the repository at this point in the history
  • Loading branch information
judgej committed Nov 29, 2018
1 parent 469be43 commit 65ee406
Show file tree
Hide file tree
Showing 17 changed files with 28 additions and 16 deletions.
7 changes: 3 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
"homepage": "https://github.com/academe/sugarrestapi",
"type": "library",
"license": "MIT",
"version": "0.9.0",
"authors": [
{
"name": "Jason Judge",
Expand All @@ -15,11 +14,11 @@
],
"require": {
"php": ">=5.3.0",
"guzzle/guzzle": "3.*"
"guzzle/guzzle": "~6.3"
},
"autoload": {
"psr-0": {
"Academe\\SugarRestApi": "/"
"psr-4": {
"Academe\\SugarRestApi": "src/"
}
}
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,14 @@ public function setClient()
// Instantiate the controller if necessary.
if (empty($this->client)) {
// Build the URL.

$this->buildEntryPoint();

// Instantiate the client REST controller with the URL.
$this->client = new \Guzzle\Http\Client($this->entryPointUrl);

$this->client = new \GuzzleHttp\Client([
'base_uri' => $this->entryPointUrl,
]);
}
}

Expand All @@ -44,22 +48,31 @@ public function post($data)
// Clear the error message.
$this->resetErrorMessage();

$request = $this->client
->post($path)
->addPostFields($data);

try {
// Send the request for a resource and get the result back, with the assumption that it is JSON.
$response = $request->send();
$result = $response->json();
// Send the request for a resource and get the result back,
// with the assumption that it is JSON.

//$response = $request->send();
$response = $this->client->request('POST', $path, [
'form_params' => $data,
]);

$body = $response->getBody();
$result = json_decode((string)$body, true);

if (json_last_error() != \JSON_ERROR_NONE) {
// Failure to decode the response as JSON.
$result = null;
$this->errorMessage = $e->getMessage();
}
}
catch (\Guzzle\Http\Exception\BadResponseException $e) {
// 4xx or 5xx
catch (\GuzzleHttp\Exception\ClientException $e) {
// 4xx
$result = null;
$this->errorMessage = $e->getMessage();
}
catch (\Guzzle\Common\Exception\RuntimeException $e) {
// Failure to decode the response as JSON.
catch (\GuzzleHttp\Exception\ServerException $e) {
// 5xx
$result = null;
$this->errorMessage = $e->getMessage();
}
Expand Down
File renamed without changes.

0 comments on commit 65ee406

Please sign in to comment.