Skip to content

Commit

Permalink
use of marshaller better error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
gruzilla committed Jan 9, 2011
1 parent fff3f52 commit 42750bc
Show file tree
Hide file tree
Showing 9 changed files with 57 additions and 9 deletions.
10 changes: 10 additions & 0 deletions Instagram/Client.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php
class Instagram_Client extends Zend_Http_Client {
public function request($method = null) {
$response = parent::request($method);
if ($response->getStatus() != 200) {
throw new Instagram_CommunicationException('HTTP-Code: '.$response->getStatus());
}
return $response;
}
}
3 changes: 2 additions & 1 deletion Instagram/Client/Communication.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,10 @@ class Instagram_Client_Communication


public function __construct() {
$this->_client = new Zend_Http_Client('http://instagr.am/api', array(
$this->_client = new Instagram_Client('http://instagr.am/api', array(
'keepalive' => true
));
$this->_client->setHeaders('Accept', 'application/xml');
$this->_client->setCookieJar();
}

Expand Down
17 changes: 17 additions & 0 deletions Instagram/Client/Marshaller.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php
/**
* @category Instagram
* @package Instagram_Client
* @copyright Copyright (c) 2010-2011 Matthias Steinböck <matthias@abendstille.at>
* @license New BSD License (enclosed file docs/LICENSE)
*/
class Instagram_Client_Marshaller
{
public static function marshall($data) {

}

public static function unmarshall($data) {
return json_decode($data);
}
}
2 changes: 1 addition & 1 deletion Instagram/Command/Feed/Timeline.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public function exec() {
$body = $response->getBody();
$this->getResponse()->setData(
self::parseResponse(
json_decode($body)
Instagram_Client_Marshaller::unmarshall($body)
)
);
}
Expand Down
2 changes: 1 addition & 1 deletion Instagram/Command/Feed/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public function exec() {
$body = $response->getBody();
$this->getResponse()->setData(
self::parseResponse(
json_decode($body)
Instagram_Client_Marshaller::unmarshall($body)
)
);
}
Expand Down
8 changes: 5 additions & 3 deletions Instagram/Command/Login.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,12 @@ public function exec() {

$response = $client->request(Zend_Http_Client::POST);

$success = json_decode($response->getBody(), true);
$success = Instagram_Client_Marshaller::unmarshall($response->getBody(), true);

if ($success['status'] != 'ok') {
throw new Instagram_Command_LoginFailedException($success['message']);
if ($success->status != 'ok') {
$ex = new Instagram_Command_LoginFailedException($success->message);
$ex->setResponse($response);
throw $ex;
}

$this->getResponse()->setData($success);
Expand Down
3 changes: 3 additions & 0 deletions Instagram/CommunicationException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<?php
class Instagram_CommunicationException extends Instagram_Exception {
}
9 changes: 9 additions & 0 deletions Instagram/Exception.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,13 @@
*/
class Instagram_Exception extends Exception
{
protected $_response;

public function setResponse($response) {
$this->_response = $response;
}

public function getResponse() {
return $this->_response;
}
}
12 changes: 9 additions & 3 deletions docs/example/example.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,13 @@
div {
margin-bottom: 50px;
}
</style>
</style><?php
if (isset($_GET['customcss'])) {
$url = parse_url($_GET['customcss']);
$url = 'http://'.$url['host'].$url['path'].(!empty($url['query']) ? $url['query'] : '');
echo '<link href="'.$url.'" rel="stylesheet" ref="stylesheet" />';
}
?>
</head>
<body>
<?php
Expand Down Expand Up @@ -95,7 +101,7 @@ function printEntry(Instagram_Feed_Entry $entry) {

$inst->auth->login($username, $password, $deviceId);

if (isset($_GET['user']) && is_numeric($_GET['user'])) {
if (isset($_GET['user']) && is_numeric($_GET['user']) && !isset($_GET['hidebacklink'])) {
echo '<p><a href="index.php">&lt;&lt; Back to my feed</a></p>';
$feed = $inst->feed->user((int)$_GET['user']);
} else {
Expand All @@ -110,7 +116,7 @@ function printEntry(Instagram_Feed_Entry $entry) {
echo printEntry($rentry);
}
} catch (Instagram_Exception $ex) {
echo "\n\n ERROR: \n\n$ex\n\n";
echo "<pre>\n\n ERROR: {$ex->getMessage()}\n\n".var_export($ex->getResponse(),1)."\n\n$ex\n\n</pre>";
}
?>
</body></html>

0 comments on commit 42750bc

Please sign in to comment.