Created
June 19, 2014 21:51
-
-
Save andreasciamanna/3bde445afe80a0e8aae6 to your computer and use it in GitHub Desktop.
Example of how we handle API responses
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
Example of a json response object: | |
{ | |
"response": { //Whatever is expected to be returned | |
"id": 11, | |
"firstName": "Woody", | |
"lastName": "Allen", | |
"email" : "wa@example.org" | |
}, | |
"status": { | |
"code": 0, | |
"messagge": "some message, or empty if not needed" | |
} | |
*/ | |
// Our PHP code handle standard API response as such: | |
protected static function _get_api_response( $response ) { | |
//No response and no status code | |
if ( !$response || !isset( $response->status->code ) ) { | |
throw new TranslationProxy_Api_Error( "Cannot communicate with the remote service" ); | |
} | |
//Status code is not 0 (0 = OK): throw an error and provide among with the status code | |
if ( $response->status->code != 0 ) { | |
throw new TranslationProxy_Api_Error( $response->status->message, $response->status->code ); | |
} | |
//Everything is fine, return just the response part | |
return $response->response; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment