Skip to content

Commit

Permalink
Merge pull request #1 from vhx/beta-2
Browse files Browse the repository at this point in the history
Beta 2
  • Loading branch information
scottusrobus committed Nov 28, 2015
2 parents b5e18cc + f6e78fd commit db55b3e
Show file tree
Hide file tree
Showing 9 changed files with 104 additions and 31 deletions.
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.1.0-beta
0.1.0-beta.2
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "vhx/vhx-php",
"version": "1.0.0-beta",
"version": "1.0.0-beta.2",
"description": "VHX PHP API Wrapper",
"keywords": [
"vhx",
Expand Down
8 changes: 4 additions & 4 deletions init.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@

// Errors
require(dirname(__FILE__) . '/lib/errors/base.php');
// require(dirname(__FILE__) . '/lib/Error/Api.php');
// require(dirname(__FILE__) . '/lib/Error/ApiConnection.php');
// require(dirname(__FILE__) . '/lib/Error/Authentication.php');
require(dirname(__FILE__) . '/lib/errors/api.php');
require(dirname(__FILE__) . '/lib/errors/connection.php');
require(dirname(__FILE__) . '/lib/errors/authentication.php');
require(dirname(__FILE__) . '/lib/errors/invalidRequest.php');
// require(dirname(__FILE__) . '/lib/Error/RateLimit.php');
require(dirname(__FILE__) . '/lib/errors/resourceNotFound.php');
6 changes: 2 additions & 4 deletions lib/api.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,8 @@
class API
{
public static $key;
const HOST = 'api.crystal.dev';
const PROTOCOL = 'http://';
const PORT = '7000';
const BASE_PATH = '/v1/';
const HOST = 'api.vhx.tv';
const PROTOCOL = 'https://';
const API_VERSION = null;

public static function setKey($api_key) {
Expand Down
9 changes: 9 additions & 0 deletions lib/errors/api.php
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);
}
}
18 changes: 12 additions & 6 deletions lib/errors/base.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,17 @@
use Exception;

abstract class Base extends Exception {
public function __construct($message, $http_status = null, $http_body = null, $json_body = null, $http_headers = null) {
$this->message = $message;
$this->httpStatus = $http_status;
$this->httpBody = $http_body;
$this->jsonBody = $json_body;
$this->httpHeaders = $http_headers;
public function __construct($message, $http_status = null) {
$this->message = $message;
$this->httpStatus = $http_status;
}
public function getHttpStatus() {
return $this->httpStatus;
}
public function getErrorResponse() {
return json_decode($this->message, true);
}
public function getErrorJSONResponse() {
return $this->message;
}
}
2 changes: 1 addition & 1 deletion lib/errors/connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace VHX\Error;

class Connection extends Base {
class ApiConnection 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);
}
Expand Down
9 changes: 9 additions & 0 deletions lib/errors/resourceNotFound.php
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);
}
}
79 changes: 65 additions & 14 deletions lib/resource.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -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;
Expand All @@ -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);
}
}

0 comments on commit db55b3e

Please sign in to comment.