Permalink
Browse files

Added client library for Google APIs

  • Loading branch information...
1 parent 7ea6522 commit 9629f0b85bf9e201361701649f67a05cce17acec @snytkine committed Jul 16, 2012

Large diffs are not rendered by default.

Oops, something went wrong.
@@ -457,7 +457,7 @@ protected function closeWindow(array $a = array())
}
if(window.opener){
%s
- setTimeout(myclose, 100); // give opener window time to process login and cancell intervals
+ setTimeout(myclose, 100); // give opener window time to process login and cancel intervals
}else{
alert("@@This is not a popup window or opener window gone away@@");
}';
@@ -203,7 +203,7 @@ protected function main()
* @throws Exception
* @throws \Exception in case something goes wrong during
* this stage
- * @return \Lampcms\Controllers\object $this
+ * @return object $this
*/
protected function startOauthDance()
{
@@ -277,7 +277,7 @@ protected function finishOauthDance()
*/
$this->oAuth->setToken($this->Request['oauth_token'], $_SESSION['oauth']['oauth_token_secret']);
$aAccessToken = $this->oAuth->getAccessToken(self::ACCESS_TOKEN_URL);
- d('$aAccessToken: ' . print_r($aAccessToken, 1));
+ d('$aAccessToken: ' . \json_encode($aAccessToken));
unset($_SESSION['oauth']);
@@ -375,7 +375,7 @@ protected function finishOauthDance()
* use same Twitter account by different users
*
* @throws \Exception
- * @return \Lampcms\Controllers\object $this
+ * @return object $this
*/
protected function createOrUpdate()
{
@@ -407,7 +407,7 @@ protected function createOrUpdate()
* re-throw as regular exception
* so that it can be caught and shown in popup window
*/
- e('@@Unable to process login@@: ' . $e->getMessage());
+ e('Unable to process login: ' . $e->getMessage());
throw new \Exception($e->getMessage());
}
@@ -496,7 +496,7 @@ protected function createNewUser()
$aUser['twitter_uid'] = $this->aUserData['_id'];
$aUser['i_rep'] = 1;
- $aUser = array_merge($this->Registry->Geo->Location->data, $aUser);
+ $aUser = \array_merge($this->Registry->Geo->Location->data, $aUser);
if (!empty($this->aUserData['url'])) {
$aUser['url'] = $this->aUserData['url'];
@@ -191,6 +191,7 @@ protected function createNewUser()
$coll = $this->Registry->Mongo->USERS;
$coll->ensureIndex(array(Schema::USERNAME_LOWERCASE => 1), array('unique' => true));
+
/**
* Cannot make email unique index because external users
* don't have email, and then value counts as null
View
@@ -52,14 +52,17 @@
namespace Lampcms;
+use \Lampcms\Http\Response;
/**
* Wrapper for curl functions
+ *
* @author admin
*
*/
class Curl extends LampcmsObject
{
+
/**
* Headers returned from the http server
*
@@ -80,18 +83,21 @@ class Curl extends LampcmsObject
/**
* Body of response (usually some html or xml)
* as returned from the http server
+ *
* @var srting
*/
protected $body;
/**
* Array or response http headers
+ *
* @var array
*/
protected $aResponseHeaders = array();
/**
* Array returned by curl_getinfo()
+ *
* @var array
*/
protected $info;
@@ -106,6 +112,7 @@ class Curl extends LampcmsObject
/**
*
* Curl handle object
+ *
* @var object curl handle
*/
protected $request;
@@ -143,8 +150,8 @@ class Curl extends LampcmsObject
* for cookies storage
*
* @param string $file must be a full path
- * if file does not exist php will attempt to create it
- * and if that fails that DevException is thrown
+ * if file does not exist php will attempt to create it
+ * and if that fails that DevException is thrown
*
* @return \Lampcms\Curl
* @throws DevException
@@ -170,10 +177,10 @@ public function setCookieFile($file)
* Sets the value for the referrer
*
* @param string $url should be a valid url,
- * ideally it will be the url from which the request
- * would come in real life. For example when posting
- * to a login form it's best to set referrer to
- * the page from where user would normally login
+ * ideally it will be the url from which the request
+ * would come in real life. For example when posting
+ * to a login form it's best to set referrer to
+ * the page from where user would normally login
*
* When posting to a "send message" form it's best to
* set the referrer to the page from which user normally
@@ -196,6 +203,7 @@ public function setReferrer($url)
*
*
* @param unknown_type $agent
+ *
* @throws DevException if $agent is not a string
*
* @return object $this
@@ -233,8 +241,8 @@ public function setBasicAuth($user, $pwd)
*
* @var array
*/
- protected $aOptions = array('timeout' => 8,
- 'useragent' => 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.1.4) Gecko/20091016 Firefox/3.5.4 GTB5');
+ protected $aOptions = array('timeout' => 8,
+ 'useragent' => 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.1.4) Gecko/20091016 Firefox/3.5.4 GTB5');
/**
@@ -247,7 +255,7 @@ public function setBasicAuth($user, $pwd)
*
* @param bool $reset
*
- * @return \Lampcms\object $this
+ * @return object $this
*/
public function initCurl($reset = false)
{
@@ -268,7 +276,7 @@ public function initCurl($reset = false)
*/
protected function checkSSL()
{
- $version = \curl_version();
+ $version = \curl_version();
$supported = ($version['features'] & CURL_VERSION_SSL);
return (bool)$supported;
@@ -303,7 +311,7 @@ protected function checkSSL()
* @throws HttpTimeoutException
* @throws DevException
* @throws \Exception
- * @return \Lampcms\object $this
+ * @return object $this
*/
public function getDocument($url, $since = null, $etag = null, array $aOptions = array())
{
@@ -356,19 +364,19 @@ public function getDocument($url, $since = null, $etag = null, array $aOptions =
$this->setHeaders($aHeaders);
}
- $response = \curl_exec($this->request);
- $this->info = \curl_getinfo($this->request);
- $error = \curl_error($this->request);
- $error_code = \curl_errno($this->request);
+ $response = \curl_exec($this->request);
+ $this->info = \curl_getinfo($this->request);
+ $error = \curl_error($this->request);
+ $error_code = \curl_errno($this->request);
$header_size = \curl_getinfo($this->request, CURLINFO_HEADER_SIZE);
if (28 === (int)$error_code) {
throw new HttpTimeoutException($error);
}
$this->httpResponseCode = \curl_getinfo($this->request, CURLINFO_HTTP_CODE);
- $this->body = \substr($response, $header_size);
- $header = \substr($response, 0, $header_size);
- $headers = \explode("\r\n", \str_replace("\r\n\r\n", '', $header));
+ $this->body = \substr($response, $header_size);
+ $header = \substr($response, 0, $header_size);
+ $headers = \explode("\r\n", \str_replace("\r\n\r\n", '', $header));
foreach ($headers as $h) {
if (\preg_match('#(.*?)\:\s(.*)#', $h, $matches)) {
@@ -384,7 +392,9 @@ public function getDocument($url, $since = null, $etag = null, array $aOptions =
/**
* Send DELETE request method to a given url
* This is useful for certain REST APIs that use DELETE HTTP method
+ *
* @param string $url
+ *
* @return object $this
*/
public function delete($url)
@@ -408,12 +418,12 @@ public function delete($url)
*
*
* @throws Http304Exception|Http400Exception|Http401Exception|Http404Exception|Http500Exception|HttpEmptyBodyException|HttpRedirectException|HttpResponseErrorException
- * @return \Lampcms\object $this
+ * @return object $this
*/
public function checkResponse()
{
- switch ($this->httpResponseCode) {
+ switch ( $this->httpResponseCode ) {
case 200:
case 201:
@@ -496,6 +506,7 @@ public function checkResponse()
*
*
* @param array $aHeaders
+ *
* @return \Lampcms\Curl
*/
public function setHeaders(array $aHeaders)
@@ -567,7 +578,7 @@ protected function setOptions(array $aOptions = array())
if (!empty($this->aOptions['formVars'])) {
$r1 = \curl_setopt($this->request, CURLOPT_POST, true);
$r2 = \curl_setopt($this->request, CURLOPT_POSTFIELDS, $this->aOptions['formVars']);
- d('set CURLOPT_POSTFIELDS: ' . print_r($this->aOptions['formVars'], 1) . ' ret: ' . $r2);
+ d('set CURLOPT_POSTFIELDS: ' . \json_encode($this->aOptions['formVars']) . ' ret: ' . $r2);
}
if (!empty($this->aOptions['method']) && 'DELETE' === \strtoupper($this->aOptions['method'])) {
@@ -662,6 +673,7 @@ public function getCurlInfo()
/**
* Getter for $this->body
+ *
* @return string body of http response
*/
public function getResponseBody()
@@ -688,6 +700,7 @@ public function getHttpResponseCode()
* if this header does not exists, will return empty string
*
* @param string $sHeader
+ *
* @return string
*/
public function getHeader($sHeader)
@@ -737,4 +750,45 @@ public function getResponseHeaders()
return $this->aResponseHeaders;
}
+
+ /**
+ * Convenience method to do a simple
+ * POST request and get response body back
+ *
+ * @param $url
+ * @param array $postVars
+ *
+ * @return string
+ */
+ public function post($url, array $postVars)
+ {
+
+ $this->getDocument($url, null, null, array('formVars' => $postVars));
+
+ $headers = $this->getResponseHeaders();
+ $body = $this->getResponseBody();
+ $code = $this->getHttpResponseCode();
+
+ return new Response($headers, $body, $code);
+ }
+
+ /**
+ * Convenience method to make a GET request
+ *
+ * @param string $url
+ *
+ * @return Http\Response
+ */
+ public function get($url)
+ {
+
+ $this->getDocument($url);
+
+ $headers = $this->getResponseHeaders();
+ $body = $this->getResponseBody();
+ $code = $this->getHttpResponseCode();
+
+ return new Response($headers, $body, $code);
+ }
+
}
Oops, something went wrong.

0 comments on commit 9629f0b

Please sign in to comment.