Skip to content

Commit

Permalink
added support for options hash in requests
Browse files Browse the repository at this point in the history
  • Loading branch information
tamagokun committed Sep 26, 2014
1 parent e729398 commit 4b97660
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 14 deletions.
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
Fridge API

```php
$client = new \FridgeApi\Client("sk_xxxxxxxxxxx", "xxxxxxxxxxxx");
$pages = $api->get('content', array(
'type' => 'pages'
));

foreach ($pages as $page) {
$page->title = "New Page Title";
// Save new title
$api->put("content/{$page->id}", $page->commit());
}
```
48 changes: 34 additions & 14 deletions lib/FridgeApi/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,29 +21,31 @@ public function authenticate()
'grant_type' => "client_credentials",
'client_id' => $this->api_key,
'client_secret' => $this->api_secret
), false)->json();
), array(
'auth' => false
))->json();
$this->access_token = $res['access_token'];
if (isset($res['refresh_token'])) $this->refresh_token = $res['refresh_token'];
}

public function get($path)
public function get($path, $options = array())
{
return $this->response_to_model($this->request("GET", $path));
return $this->response_to_model($this->request("GET", $path, null, $options));
}

public function post($path, $data)
public function post($path, $data, $options = array())
{
return $this->response_to_model($this->request("POST", $path, $data));
return $this->response_to_model($this->request("POST", $path, $data, $options));
}

public function put($path, $data)
public function put($path, $data, $options = array())
{
return $this->response_to_model($this->request("PUT", $path, $data));
return $this->response_to_model($this->request("PUT", $path, $data, $options));
}

public function delete($path)
public function delete($path, $options = array())
{
return $this->response_to_model($this->request("DELETE", $path));
return $this->response_to_model($this->request("DELETE", $path, null, $options));
}

public function response_to_model($res)
Expand All @@ -64,20 +66,41 @@ public function response_to_model($res)
}
}

public function request($method, $path, $data=null, $auth=true)
public function request($method, $path, $data=null, $options=null)
{
$req = $this->agent->createRequest($method, $this->base."/".$path);
if ($method == "POST" || $method == "PUT") {
$req = $this->agent->createRequest($method, $this->base."/".$path, array(
'body' => $data
));
}

// use Authentication by default
$auth = true;
$debug = $this->debug;

if ($options) {
// parse options
if (isset($options['auth'])) {
$auth = $options['auth'];
unset($options['auth']);
}
if (isset($options['debug'])) {
$debug = $options['debug'];
unset($options['debug']);
}

// use other options as query string
$query = $req->getQuery();
foreach ($options as $q => $v) $query[$q] = $v;
}

if ($auth) {
if (!$this->access_token) $this->authenticate();
$req->setHeader('Authorization', 'token ' . $this->access_token);
}

if ($this->debug) error_log($req->__toString());
if ($debug) error_log($req->__toString());

try {
return $this->agent->send($req);
Expand All @@ -86,7 +109,4 @@ public function request($method, $path, $data=null, $auth=true)
return false;
}
}

// private

}

0 comments on commit 4b97660

Please sign in to comment.