Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Throw exception on network errors and allow overriding network methods #1

Open
wants to merge 1 commit into from

1 participant

@ntemple

Fixes error when the request object is not valid due to a network error - instead of a fatal error, an exception is thrown. In theory, you can also override the new sendHTTPRequest method to use a different transport other than file_get_contents. For example, using curl if file_get_contents is disabled.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
Showing with 27 additions and 3 deletions.
  1. +27 −3 PlancakeApiClient.php
View
30 PlancakeApiClient.php
@@ -172,7 +172,7 @@ private function resetToken()
$request = $this->prepareRequest($params, 'getToken');
- $response = json_decode(file_get_contents($request));
+ $response = $this->sendHTTPRequest($request);
if (isset($response->error))
{
@@ -182,6 +182,30 @@ private function resetToken()
$this->token = $response->token;
}
+
+ /**
+ * @param url
+ * @param use inclide path
+ * @param context
+ * @param assoc
+ */
+
+ protected function sendHTTPRequest($url, $use_include_path = false, $context = null, $assoc = false) {
+
+ $result = null;
+
+ $result_data = file_get_contents($url, $use_include_path, $context);
+ if ($result_data) {
+ $result = json_decode($result_data, $assoc);
+ }
+
+ if (! $result) {
+ throw new Exception("Network Error");
+ }
+
+ return $result;
+ }
+
/**
*
* @param array $params
@@ -212,7 +236,7 @@ private function sendRequest(array $params, $methodName, array $requestOpts = nu
$request = $this->prepareRequest($params, $methodName);
- $response = json_decode(file_get_contents($request, false, $requestContext), true);
+ $response = $this->sendHTTPRequest($request, false, $requestContext, true);
// checking whether an error occurred
if (isset($response->error))
@@ -225,7 +249,7 @@ private function sendRequest(array $params, $methodName, array $requestOpts = nu
$request = $this->prepareRequest($params, $methodName);
- $response = json_decode(file_get_contents($request, false, $requestContext), true);
+ $response = $this->sendHTTPRequest($request, false, $requestContext, true);
if (isset($response->error))
{
Something went wrong with that request. Please try again.