-
Notifications
You must be signed in to change notification settings - Fork 3
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 #1 from vhx/beta-2
Beta 2
- Loading branch information
Showing
9 changed files
with
104 additions
and
31 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 |
---|---|---|
@@ -1 +1 @@ | ||
0.1.0-beta | ||
0.1.0-beta.2 |
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
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,9 @@ | ||
<?php | ||
|
||
namespace VHX\Error; | ||
|
||
class Api extends Base { | ||
public function __construct($message = null, $http_status = null, $http_body = null, $json_body = null, $http_headers = null) { | ||
parent::__construct($message, $http_status, $http_body, $json_body, $http_headers); | ||
} | ||
} |
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
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 | ||
|
||
namespace VHX\Error; | ||
|
||
class ResourceNotFound extends Base { | ||
public function __construct($message = null, $http_status = null, $http_body = null, $json_body = null, $http_headers = null) { | ||
parent::__construct($message, $http_status, $http_body, $json_body, $http_headers); | ||
} | ||
} |
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 |
---|---|---|
|
@@ -21,7 +21,7 @@ private static function _hasID($id, $request) { | |
return; | ||
else: | ||
$message = 'You must provide a UUID when making an ' . $request . ' request.'; | ||
throw new Error\InvalidRequest($message); | ||
throw new Error\InvalidRequest($message, 400); | ||
endif; | ||
} | ||
|
||
|
@@ -36,7 +36,6 @@ private static function _request($method, $path, $data = array()) { | |
if ($method === 'POST' || $method === 'PUT'): | ||
curl_setopt($curl, CURLOPT_POST, 1); | ||
if ($data): | ||
var_dump($data); | ||
curl_setopt($curl, CURLOPT_POSTFIELDS, $data); | ||
endif; | ||
endif; | ||
|
@@ -48,45 +47,97 @@ private static function _request($method, $path, $data = array()) { | |
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC); | ||
curl_setopt($curl, CURLOPT_USERPWD, API::$key . ':'); | ||
curl_setopt($curl, CURLOPT_URL, $url); | ||
curl_setopt($curl, CURLOPT_VERBOSE, 1); | ||
curl_setopt($curl, CURLOPT_HEADER, true); | ||
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); | ||
curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 30); | ||
curl_setopt($curl, CURLOPT_TIMEOUT, 80); | ||
|
||
$result = curl_exec($curl); | ||
|
||
if (curl_errno($curl)) { | ||
die('Error: "' . curl_error($curl) . '" - Code: ' . curl_errno($curl)); | ||
if ($result === false): | ||
$errno = curl_errno($curl); | ||
$message = curl_error($curl); | ||
curl_close($curl); | ||
} | ||
else { | ||
return $result; | ||
return self::_handleCurlError($url, $errno, $message); | ||
else: | ||
$header_size = curl_getinfo($curl, CURLINFO_HEADER_SIZE); | ||
$code = curl_getinfo($curl, CURLINFO_HTTP_CODE); | ||
$body = substr($result, $header_size); | ||
curl_close($curl); | ||
} | ||
return self::_handleResponse($body, $code); | ||
endif; | ||
} | ||
|
||
protected static function _retrieve($id) { | ||
self::_hasID($id, 'retrieve'); | ||
return json_decode(self::_request('GET', self::_getResourceName() . '/' . $id), true); | ||
return self::_request('GET', self::_getResourceName() . '/' . $id); | ||
} | ||
|
||
protected static function _list($params) { | ||
return json_decode(self::_request('GET', self::_getResourceName() . '/', $params), true); | ||
return self::_request('GET', self::_getResourceName() . '/', $params); | ||
} | ||
|
||
protected static function _items($id) { | ||
self::_hasID($id, 'items'); | ||
return json_decode(self::_request('GET', self::_getResourceName() . '/' . $id . '/items'), true); | ||
return self::_request('GET', self::_getResourceName() . '/' . $id . '/items'); | ||
} | ||
|
||
protected static function _create($params = null) { | ||
return json_decode(self::_request('POST', self::_getResourceName() . '/', $params), true); | ||
return self::_request('POST', self::_getResourceName() . '/', $params); | ||
} | ||
|
||
protected static function _update($id, $params = null) { | ||
self::_hasID($id, 'update'); | ||
return json_decode(self::_request('PUT', self::_getResourceName() . '/' . $id, $params), true); | ||
return self::_request('PUT', self::_getResourceName() . '/' . $id, $params); | ||
} | ||
|
||
protected static function _delete($id) { | ||
self::_hasID($id, 'delete'); | ||
return json_decode( self::_request('DELETE', self::_getResourceName() . '/' . $id), true); | ||
return self::_request('DELETE', self::_getResourceName() . '/' . $id); | ||
} | ||
|
||
protected static function _handleResponse($body, $code) { | ||
|
||
if ($code >= 200 && $code < 300): | ||
return json_decode($body, true); | ||
else: | ||
self::_handleResponseError($body, $code); | ||
endif; | ||
} | ||
|
||
protected static function _handleResponseError($result, $code) { | ||
switch ($code) { | ||
case 400: | ||
throw new Error\InvalidRequest($result, $code); | ||
break; | ||
case 401: | ||
throw new Error\Authentication($result, $code); | ||
break; | ||
case 404: | ||
throw new Error\ResourceNotFound($result, $code); | ||
break; | ||
case 408: | ||
throw new Error\ApiConnection($result, $code); | ||
break; | ||
case 500: | ||
default: | ||
throw new Error\API($result, $code); | ||
break; | ||
} | ||
} | ||
|
||
protected static function handleCurlError($url, $errno, $message) { | ||
switch ($errno) { | ||
case CURLE_COULDNT_CONNECT: | ||
case CURLE_COULDNT_RESOLVE_HOST: | ||
case CURLE_OPERATION_TIMEOUTED: | ||
$msg = "Could not connect to VHX ($url). Please check your internet connection and try again. If this problem persists, you should check VHX's service status at https://twitter.com/vhxstatus, http://status.vhx.tv/, or"; | ||
break; | ||
default: | ||
$msg = "Unexpected error communicating with VHX. If this problem persists,"; | ||
} | ||
$msg .= " let us know at [email protected]. \n\n(Network error [errno $errno]: $message)"; | ||
throw new Error\ApiConnection(msg, 408); | ||
} | ||
} |