Skip to content

Commit

Permalink
doofinder/helpapp#3815 new auth header and some rf
Browse files Browse the repository at this point in the history
  • Loading branch information
JoeZ99 committed Apr 15, 2016
1 parent 3411d23 commit 84499b6
Showing 1 changed file with 10 additions and 15 deletions.
25 changes: 10 additions & 15 deletions lib/doofinder_api.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ function __construct($hashid, $api_key, $fromParams=false, $init_options = array
if(2 === count($zone_key_array)){
$this->api_key = $zone_key_array[1];
$this->zone = $zone_key_array[0];
$this->url = "http://" . $this->zone . self::URL_SUFFIX;
$this->url = "https://" . $this->zone . self::URL_SUFFIX;
} else {
throw new DoofinderException("API Key is no properly set.");
}
Expand Down Expand Up @@ -151,18 +151,16 @@ private function translateFilter($filter){
private function reqHeaders(){
$headers = array();
$headers[] = 'Expect:'; //Fixes the HTTP/1.1 417 Expectation Failed
$headers[] = "API Token: " . $this->api_key; //API Authorization
$authHeaderName = $this->apiVersion == '4' ? 'API Token: ' : 'authorization: ';
$headers[] = $authHeaderName . $this->api_key; //API Authorization
return $headers;
}

private function apiCall($params,$method='search'){
private function apiCall($entry_point='search', $params=array()){
$params['hashid'] = $this->hashid;
$args = http_build_query($this->sanitize($params)); // remove any null value from the array
switch($method){
case 'search': $url = $this -> url . '/' . $this->apiVersion . '/search?' . $args;break;
case 'options': $url = $this -> url . '/' . $this->apiVersion . '/options/' . $this->hashid; break;
default: $url = $this -> url . '/' . $this->apiVersion . '/search?' . $args; break;
}

$url = $this->url.'/'.$this->apiVersion.'/'.$entry_point.'?'.$args;

$session = curl_init($url);
curl_setopt($session, CURLOPT_CUSTOMREQUEST, 'GET');
Expand All @@ -174,16 +172,13 @@ private function apiCall($params,$method='search'){
curl_close($session);

if (floor($httpCode / 100) == 2) {
if($method=='options'){
return $response;
}
return new DoofinderResults($response);
return $response;
}
throw new DoofinderException($httpCode.' - '.$response, $httpCode);
}

public function getOptions(){
return $this->apiCall('', 'options');
return $this->apiCall('options/'.$this->hashid);
}

/**
Expand Down Expand Up @@ -232,11 +227,11 @@ public function query($query=null, $page=null, $options = array()){
{
$filter = $params['filter'];
unset($params['filter']);
$dfResults = $this->apiCall($params);
$dfResults = $this->apiCall('search', $params);
$params['query_name'] = $dfResults->getProperty('query_name');
$params['filter'] = $filter;
}
$dfResults = $this->apiCall($params);
$dfResults = new DoofinderResults($this->apiCall('search', $params));
$this->page = $dfResults->getProperty('page');
$this->total = $dfResults->getProperty('total');
$this->search_options['query'] = $dfResults->getProperty('query');
Expand Down

0 comments on commit 84499b6

Please sign in to comment.