Permalink
Browse files

use of marshaller better error handling

  • Loading branch information...
1 parent fff3f52 commit 42750bc57beba60335df88aff6829b26da54be66 @gruzilla committed Jan 9, 2011
View
10 Instagram/Client.php
@@ -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;
+ }
+}
View
3 Instagram/Client/Communication.php
@@ -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();
}
View
17 Instagram/Client/Marshaller.php
@@ -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);
+ }
+}
View
2 Instagram/Command/Feed/Timeline.php
@@ -24,7 +24,7 @@ public function exec() {
$body = $response->getBody();
$this->getResponse()->setData(
self::parseResponse(
- json_decode($body)
+ Instagram_Client_Marshaller::unmarshall($body)
)
);
}
View
2 Instagram/Command/Feed/User.php
@@ -25,7 +25,7 @@ public function exec() {
$body = $response->getBody();
$this->getResponse()->setData(
self::parseResponse(
- json_decode($body)
+ Instagram_Client_Marshaller::unmarshall($body)
)
);
}
View
8 Instagram/Command/Login.php
@@ -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);
View
3 Instagram/CommunicationException.php
@@ -0,0 +1,3 @@
+<?php
+class Instagram_CommunicationException extends Instagram_Exception {
+}
View
9 Instagram/Exception.php
@@ -7,4 +7,13 @@
*/
class Instagram_Exception extends Exception
{
+ protected $_response;
+
+ public function setResponse($response) {
+ $this->_response = $response;
+ }
+
+ public function getResponse() {
+ return $this->_response;
+ }
}
View
12 docs/example/example.php
@@ -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
@@ -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 {
@@ -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.