Skip to content
This repository was archived by the owner on Mar 27, 2023. It is now read-only.
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
105 changes: 59 additions & 46 deletions lib/Semantics3/ApiConnector.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,59 +2,72 @@

abstract class Api_Connector
{
private $apiKey;
private $apiSecret;

public function __construct($apiKey=null,$apiSecret=null,$apiBase=null){
$this->apiKey = $apiKey;
$this->apiSecret = $apiSecret;
$this->apiBase = is_null($apiBase) ? "https://api.semantics3.com/v1/" : $apiBase;
}

public function run_query($endpoint, $params, $method="GET")
{
if (!$this->apiKey)
throw new Semantics3_AuthenticationError('No API key provided.');

if (!$this->apiSecret)
throw new Semantics3_AuthenticationError('No API secret provided.');

$options = array( 'consumer_key' => $this->apiKey, 'consumer_secret' => $this->apiSecret );
OAuthStore::instance("2Leg", $options );
$url = $this->apiBase.$endpoint;
if ($method == "GET") {
$url = $url."?q=".urlencode(json_encode($params));
$params = null;
}
else {
$params = json_encode($params);
private $apiKey;
private $apiSecret;
private $catchExceptions;

public function __construct($apiKey=null,
$apiSecret=null,
$apiBase=null,
$catchExceptions=true){
$this->catchExceptions = $catchExceptions;
$this->apiKey = $apiKey;
$this->apiSecret = $apiSecret;
$this->apiBase = is_null($apiBase) ? "https://api.semantics3.com/v1/" : $apiBase;
}

try
public function run_query($endpoint, $params, $method="GET")
{
switch ($method) {
if (!$this->apiKey)
throw new Semantics3_AuthenticationError('No API key provided.');

if (!$this->apiSecret)
throw new Semantics3_AuthenticationError('No API secret provided.');

$options = array( 'consumer_key' => $this->apiKey, 'consumer_secret' => $this->apiSecret );
OAuthStore::instance("2Leg", $options );
$url = $this->apiBase.$endpoint;
if ($method == "GET") {
$url = $url."?q=".urlencode(json_encode($params));
$params = null;
}
else {
$params = json_encode($params);
}

if(!$this->catchExceptions) {
return $this->getOAuthRequester($method, $url, $params);
}

try
{
return $this->getOAuthRequester($method, $url, $params);
}
catch(OAuthException2 $e)
{
print "\n";
$error = $e->getMessage();
print $error."\n";
}

}

protected function getOAuthRequester($method, $url, $params){
switch ($method) {
case "GET":
$request = new OAuthRequester($url, $method, $params);
break;
$request = new OAuthRequester($url, $method, $params);
break;
case "POST":
$request = new OAuthRequester($url, $method, '', $params);
break;
$request = new OAuthRequester($url, $method, '', $params);
break;
case "DELETE":
$request = new OAuthRequester($url, $method);
break;
$request = new OAuthRequester($url, $method);
break;
default:
$request = new OAuthRequester($url, $method);
}
$request = new OAuthRequester($url, $method);
}

$result = $request->doRequest();
return $result['body'];
$result = $request->doRequest();
return $result['body'];
}
catch(OAuthException2 $e)
{
print "\n";
$error = $e->getMessage();
print $error."\n";
}

}
}