@@ -12,63 +12,63 @@ |
||
| 12 | 12 | use Requests; |
| 13 | 13 | |
| 14 | 14 | class OAuth { |
| 15 | - /* |
|
| 15 | + /* |
|
| 16 | 16 | * token life time in second |
| 17 | 17 | * */ |
| 18 | - public $tokenLifeTime; |
|
| 19 | - public $key; |
|
| 20 | - public $iss; |
|
| 18 | + public $tokenLifeTime; |
|
| 19 | + public $key; |
|
| 20 | + public $iss; |
|
| 21 | 21 | |
| 22 | - protected $expireTimestamp; |
|
| 23 | - protected $accessToken; |
|
| 22 | + protected $expireTimestamp; |
|
| 23 | + protected $accessToken; |
|
| 24 | 24 | |
| 25 | - /** |
|
| 26 | - * OAuth constructor. |
|
| 27 | - * |
|
| 28 | - * @param $key |
|
| 29 | - * @param $iss |
|
| 30 | - */ |
|
| 31 | - public function __construct( $key, $iss, $tokenLifeTime = 3600 ) { |
|
| 32 | - $this->key = $key; |
|
| 33 | - $this->iss = $iss; |
|
| 34 | - $this->tokenLifeTime = $tokenLifeTime; |
|
| 35 | - } |
|
| 25 | + /** |
|
| 26 | + * OAuth constructor. |
|
| 27 | + * |
|
| 28 | + * @param $key |
|
| 29 | + * @param $iss |
|
| 30 | + */ |
|
| 31 | + public function __construct( $key, $iss, $tokenLifeTime = 3600 ) { |
|
| 32 | + $this->key = $key; |
|
| 33 | + $this->iss = $iss; |
|
| 34 | + $this->tokenLifeTime = $tokenLifeTime; |
|
| 35 | + } |
|
| 36 | 36 | |
| 37 | - protected function requestAccessToken() { |
|
| 38 | - $currentTimestamp = time(); |
|
| 39 | - $this->expireTimestamp = $currentTimestamp + $this->tokenLifeTime; |
|
| 40 | - $jsonToken = array( |
|
| 41 | - "iss" => $this->iss, |
|
| 42 | - "scope" => "https://www.googleapis.com/auth/firebase.database https://www.googleapis.com/auth/userinfo.email", |
|
| 43 | - "aud" => "https://www.googleapis.com/oauth2/v4/token", |
|
| 44 | - "exp" => $this->expireTimestamp, |
|
| 45 | - "iat" => $currentTimestamp |
|
| 46 | - ); |
|
| 47 | - $jwt = JWT::encode( $jsonToken, $this->key, 'RS256' ); |
|
| 37 | + protected function requestAccessToken() { |
|
| 38 | + $currentTimestamp = time(); |
|
| 39 | + $this->expireTimestamp = $currentTimestamp + $this->tokenLifeTime; |
|
| 40 | + $jsonToken = array( |
|
| 41 | + "iss" => $this->iss, |
|
| 42 | + "scope" => "https://www.googleapis.com/auth/firebase.database https://www.googleapis.com/auth/userinfo.email", |
|
| 43 | + "aud" => "https://www.googleapis.com/oauth2/v4/token", |
|
| 44 | + "exp" => $this->expireTimestamp, |
|
| 45 | + "iat" => $currentTimestamp |
|
| 46 | + ); |
|
| 47 | + $jwt = JWT::encode( $jsonToken, $this->key, 'RS256' ); |
|
| 48 | 48 | |
| 49 | - $OAuthResponse = Requests::post( 'https://www.googleapis.com/oauth2/v4/token', array(), array( |
|
| 50 | - 'grant_type' => 'urn:ietf:params:oauth:grant-type:jwt-bearer', |
|
| 51 | - 'assertion' => $jwt |
|
| 52 | - ) ); |
|
| 49 | + $OAuthResponse = Requests::post( 'https://www.googleapis.com/oauth2/v4/token', array(), array( |
|
| 50 | + 'grant_type' => 'urn:ietf:params:oauth:grant-type:jwt-bearer', |
|
| 51 | + 'assertion' => $jwt |
|
| 52 | + ) ); |
|
| 53 | 53 | |
| 54 | - if ( $OAuthResponse->status_code == 200 ) { |
|
| 55 | - $this->accessToken = json_decode( $OAuthResponse->body )->access_token; |
|
| 54 | + if ( $OAuthResponse->status_code == 200 ) { |
|
| 55 | + $this->accessToken = json_decode( $OAuthResponse->body )->access_token; |
|
| 56 | 56 | |
| 57 | - return true; |
|
| 58 | - } |
|
| 57 | + return true; |
|
| 58 | + } |
|
| 59 | 59 | |
| 60 | - return false; |
|
| 61 | - } |
|
| 60 | + return false; |
|
| 61 | + } |
|
| 62 | 62 | |
| 63 | - public function getAccessToken() { |
|
| 64 | - $currentTime = time(); |
|
| 65 | - if ( $this->expireTimestamp < $currentTime ) { |
|
| 66 | - $startTime = time(); |
|
| 67 | - $this->requestAccessToken(); |
|
| 68 | - $endTime = time(); |
|
| 69 | - $this->expireTimestamp -= ( $endTime - $startTime ); |
|
| 70 | - } |
|
| 63 | + public function getAccessToken() { |
|
| 64 | + $currentTime = time(); |
|
| 65 | + if ( $this->expireTimestamp < $currentTime ) { |
|
| 66 | + $startTime = time(); |
|
| 67 | + $this->requestAccessToken(); |
|
| 68 | + $endTime = time(); |
|
| 69 | + $this->expireTimestamp -= ( $endTime - $startTime ); |
|
| 70 | + } |
|
| 71 | 71 | |
| 72 | - return $this->accessToken; |
|
| 73 | - } |
|
| 72 | + return $this->accessToken; |
|
| 73 | + } |
|
| 74 | 74 | } |
| 75 | 75 | \ No newline at end of file |
@@ -10,33 +10,33 @@ |
||
| 10 | 10 | |
| 11 | 11 | |
| 12 | 12 | class JCFirebaseOption { |
| 13 | - const __default = null; |
|
| 14 | - |
|
| 15 | - const OPTION_SHALLOW = 'shallow'; |
|
| 16 | - const SHALLOW_TRUE = 'true'; |
|
| 17 | - const SHALLOW_FALSE = 'false'; |
|
| 18 | - |
|
| 19 | - const OPTION_PRINT = 'print'; |
|
| 20 | - const PRINT_PRETTY = 'pretty'; |
|
| 21 | - const PRINT_SILENT = 'silent'; |
|
| 22 | - |
|
| 23 | - const REQ_TYPE_GET = 0; |
|
| 24 | - const REQ_TYPE_PUT = 1; |
|
| 25 | - const REQ_TYPE_POST = 2; |
|
| 26 | - const REQ_TYPE_PATCH = 3; |
|
| 27 | - const REQ_TYPE_DELETE = 4; |
|
| 28 | - |
|
| 29 | - public static function isAllowPrint( $reqType = self::REQ_TYPE_GET, $pType = self::PRINT_PRETTY ) { |
|
| 30 | - if ( $pType == self::PRINT_PRETTY ) { |
|
| 31 | - return true; |
|
| 32 | - } |
|
| 33 | - |
|
| 34 | - if ( $pType == self::PRINT_SILENT ) { |
|
| 35 | - if ( $reqType == self::REQ_TYPE_DELETE ) { |
|
| 36 | - return false; |
|
| 37 | - } |
|
| 38 | - } |
|
| 39 | - |
|
| 40 | - return true; |
|
| 41 | - } |
|
| 13 | + const __default = null; |
|
| 14 | + |
|
| 15 | + const OPTION_SHALLOW = 'shallow'; |
|
| 16 | + const SHALLOW_TRUE = 'true'; |
|
| 17 | + const SHALLOW_FALSE = 'false'; |
|
| 18 | + |
|
| 19 | + const OPTION_PRINT = 'print'; |
|
| 20 | + const PRINT_PRETTY = 'pretty'; |
|
| 21 | + const PRINT_SILENT = 'silent'; |
|
| 22 | + |
|
| 23 | + const REQ_TYPE_GET = 0; |
|
| 24 | + const REQ_TYPE_PUT = 1; |
|
| 25 | + const REQ_TYPE_POST = 2; |
|
| 26 | + const REQ_TYPE_PATCH = 3; |
|
| 27 | + const REQ_TYPE_DELETE = 4; |
|
| 28 | + |
|
| 29 | + public static function isAllowPrint( $reqType = self::REQ_TYPE_GET, $pType = self::PRINT_PRETTY ) { |
|
| 30 | + if ( $pType == self::PRINT_PRETTY ) { |
|
| 31 | + return true; |
|
| 32 | + } |
|
| 33 | + |
|
| 34 | + if ( $pType == self::PRINT_SILENT ) { |
|
| 35 | + if ( $reqType == self::REQ_TYPE_DELETE ) { |
|
| 36 | + return false; |
|
| 37 | + } |
|
| 38 | + } |
|
| 39 | + |
|
| 40 | + return true; |
|
| 41 | + } |
|
| 42 | 42 | } |
| 43 | 43 | \ No newline at end of file |
@@ -16,198 +16,198 @@ |
||
| 16 | 16 | * reference https://www.firebase.com/docs/rest/api/ |
| 17 | 17 | */ |
| 18 | 18 | class JCFirebase { |
| 19 | - public $firebaseURI; |
|
| 20 | - public $firebaseDefaultPath; |
|
| 21 | - public $requestHeader = array( |
|
| 22 | - 'accept' => 'application/json', |
|
| 23 | - 'contentType' => 'application/json; charset=utf-8', |
|
| 24 | - 'dataType' => 'json' |
|
| 25 | - ); |
|
| 26 | - public $requestOptions = array(); |
|
| 27 | - /** |
|
| 28 | - * @var OAuth |
|
| 29 | - */ |
|
| 30 | - protected $auth; |
|
| 31 | - |
|
| 32 | - |
|
| 33 | - /** |
|
| 34 | - * JCFirebase constructor. |
|
| 35 | - * |
|
| 36 | - * @param $firebaseURI |
|
| 37 | - * @param array $firebaseAuth |
|
| 38 | - * @param string $firebaseDefaultPath |
|
| 39 | - */ |
|
| 40 | - public function __construct( $firebaseURI, $firebaseAuth = array(), $firebaseDefaultPath = '/' ) { |
|
| 41 | - $this->firebaseURI = $firebaseURI; |
|
| 42 | - $this->firebaseDefaultPath = $firebaseDefaultPath; |
|
| 43 | - $this->setAuth( $firebaseAuth ); |
|
| 44 | - } |
|
| 45 | - |
|
| 46 | - /** |
|
| 47 | - * @param $firebaseURI |
|
| 48 | - * @param $keyFile |
|
| 49 | - * @param string $firebaseDefaultPath |
|
| 50 | - * |
|
| 51 | - * @return JCFirebase |
|
| 52 | - * @throws \Exception |
|
| 53 | - */ |
|
| 54 | - public static function fromKeyFile( $firebaseURI, $keyFile, $firebaseDefaultPath = '/' ) { |
|
| 55 | - $keyData = null; |
|
| 56 | - try { |
|
| 57 | - $keyData = json_decode( file_get_contents( $keyFile ) ); |
|
| 58 | - } catch ( \Exception $exception ) { |
|
| 59 | - $keyData = json_decode( Requests::get( $keyFile ) ); |
|
| 60 | - } |
|
| 61 | - |
|
| 62 | - if ( $keyData ) { |
|
| 63 | - $serviceAccount = $keyData->client_email; |
|
| 64 | - $privateKey = $keyData->private_key; |
|
| 65 | - |
|
| 66 | - return new self( $firebaseURI, array( |
|
| 67 | - 'key' => $privateKey, |
|
| 68 | - 'iss' => $serviceAccount |
|
| 69 | - ), $firebaseDefaultPath ); |
|
| 70 | - } else { |
|
| 71 | - throw new \Exception( "can't get data from key file" ); |
|
| 72 | - } |
|
| 73 | - } |
|
| 74 | - |
|
| 75 | - public function setAuth( $firebaseServiceAccount ) { |
|
| 76 | - if ( isset( $firebaseServiceAccount['key'] ) && isset( $firebaseServiceAccount['iss'] ) ) { |
|
| 77 | - $this->auth = new OAuth( $firebaseServiceAccount['key'], $firebaseServiceAccount['iss'] ); |
|
| 78 | - } |
|
| 79 | - } |
|
| 80 | - |
|
| 81 | - public function getPathURI( $path = '', $print = '' ) { |
|
| 82 | - //remove last slash from firebaseURI |
|
| 83 | - $template = '/'; |
|
| 84 | - $this->firebaseURI = rtrim( $this->firebaseURI, $template ); |
|
| 85 | - $path = rtrim( $path, $template ); |
|
| 86 | - $path = ltrim( $path, $template ); |
|
| 87 | - |
|
| 88 | - //check https |
|
| 89 | - if ( strpos( $this->firebaseURI, 'http://' ) !== false ) { |
|
| 90 | - throw new \Exception( "https is required." ); |
|
| 91 | - } |
|
| 92 | - |
|
| 93 | - //check firebaseURI |
|
| 94 | - if ( empty( $this->firebaseURI ) ) { |
|
| 95 | - throw new \Exception( "firebase URI is required" ); |
|
| 96 | - } |
|
| 97 | - |
|
| 98 | - if ( strpos( $this->firebaseDefaultPath, "/" ) !== 0 ) { |
|
| 99 | - throw new \Exception( "firebase default path must contain /" ); |
|
| 100 | - } |
|
| 101 | - |
|
| 102 | - $pathURI = $this->firebaseURI . $this->firebaseDefaultPath . $path . ".json"; |
|
| 103 | - |
|
| 104 | - //set query data |
|
| 105 | - $queryData = array(); |
|
| 106 | - if ( ! empty( $print ) ) { |
|
| 107 | - $queryData[ JCFirebaseOption::OPTION_PRINT ] = $print; |
|
| 108 | - } |
|
| 109 | - if ( ! empty( $queryData ) ) { |
|
| 110 | - $pathURI = $pathURI . '?' . http_build_query( $queryData ); |
|
| 111 | - } |
|
| 112 | - |
|
| 113 | - $this->refreshToken(); |
|
| 114 | - |
|
| 115 | - return $pathURI; |
|
| 116 | - } |
|
| 117 | - |
|
| 118 | - public function getShallow( $path = '', $options = array() ) { |
|
| 119 | - return Requests::get( |
|
| 120 | - $this->getPathURI( $path ) . '?' . http_build_query( array( |
|
| 121 | - JCFirebaseOption::OPTION_SHALLOW => JCFirebaseOption::SHALLOW_TRUE |
|
| 122 | - ) ), |
|
| 123 | - $this->requestHeader, |
|
| 124 | - $this->addDataToRequest( $options ) |
|
| 125 | - ); |
|
| 126 | - } |
|
| 127 | - |
|
| 128 | - /** |
|
| 129 | - * @param string $path |
|
| 130 | - * @param array $options |
|
| 131 | - * |
|
| 132 | - * @return \Requests_Response |
|
| 133 | - */ |
|
| 134 | - public function get( $path = '', $options = array() ) { |
|
| 135 | - return Requests::get( |
|
| 136 | - $this->addDataToPathURI( $path, $options ), $this->requestHeader, |
|
| 137 | - $this->addDataToRequest( $options ) |
|
| 138 | - ); |
|
| 139 | - } |
|
| 140 | - |
|
| 141 | - /** |
|
| 142 | - * @param string $path |
|
| 143 | - * @param array $options |
|
| 144 | - * |
|
| 145 | - * @return \Requests_Response |
|
| 146 | - */ |
|
| 147 | - public function put( $path = '', $options = array() ) { |
|
| 148 | - return Requests::put( $this->getPathURI( $path ), $this->requestHeader, |
|
| 149 | - $this->addDataToRequest( $options, true ) ); |
|
| 150 | - } |
|
| 151 | - |
|
| 152 | - /** |
|
| 153 | - * @param string $path |
|
| 154 | - * @param array $options |
|
| 155 | - * |
|
| 156 | - * @return \Requests_Response |
|
| 157 | - */ |
|
| 158 | - public function post( $path = '', $options = array() ) { |
|
| 159 | - return Requests::post( $this->getPathURI( $path ), $this->requestHeader, |
|
| 160 | - $this->addDataToRequest( $options, true ) ); |
|
| 161 | - } |
|
| 162 | - |
|
| 163 | - /** |
|
| 164 | - * @param string $path |
|
| 165 | - * @param array $options |
|
| 166 | - * |
|
| 167 | - * @return \Requests_Response |
|
| 168 | - */ |
|
| 169 | - public function patch( $path = '', $options = array() ) { |
|
| 170 | - return Requests::patch( $this->getPathURI( $path ), $this->requestHeader, |
|
| 171 | - $this->addDataToRequest( $options, true ) ); |
|
| 172 | - } |
|
| 173 | - |
|
| 174 | - /** |
|
| 175 | - * @param string $path |
|
| 176 | - * @param array $options |
|
| 177 | - * |
|
| 178 | - * @return \Requests_Response |
|
| 179 | - */ |
|
| 180 | - public function delete( $path = '', $options = array() ) { |
|
| 181 | - return Requests::delete( $this->getPathURI( $path ), $this->requestHeader, |
|
| 182 | - $this->addDataToRequest( $options ) ); |
|
| 183 | - } |
|
| 184 | - |
|
| 185 | - protected function refreshToken() { |
|
| 186 | - $this->requestHeader['Authorization'] = 'Bearer ' . $this->auth->getAccessToken(); |
|
| 187 | - } |
|
| 188 | - |
|
| 189 | - protected function addDataToPathURI( $path = '', $options = array(), $reqType = JCFirebaseOption::REQ_TYPE_GET ) { |
|
| 190 | - $print = ''; |
|
| 191 | - if ( isset( $options['print'] ) ) { |
|
| 192 | - if ( JCFirebaseOption::isAllowPrint( $reqType, $options['print'] ) ) { |
|
| 193 | - $print = $options['print']; |
|
| 194 | - } |
|
| 195 | - } |
|
| 196 | - |
|
| 197 | - return $this->getPathURI( $path, $print ); |
|
| 198 | - } |
|
| 199 | - |
|
| 200 | - protected function addDataToRequest( $options = array(), $jsonEncode = false ) { |
|
| 201 | - $requestOptions = array(); |
|
| 202 | - |
|
| 203 | - if ( isset( $options['data'] ) ) { |
|
| 204 | - $requestOptions = array_merge( $options['data'], $requestOptions ); |
|
| 205 | - } |
|
| 206 | - |
|
| 207 | - if ( $jsonEncode ) { |
|
| 208 | - $requestOptions = json_encode( $requestOptions ); |
|
| 209 | - } |
|
| 210 | - |
|
| 211 | - return $requestOptions; |
|
| 212 | - } |
|
| 19 | + public $firebaseURI; |
|
| 20 | + public $firebaseDefaultPath; |
|
| 21 | + public $requestHeader = array( |
|
| 22 | + 'accept' => 'application/json', |
|
| 23 | + 'contentType' => 'application/json; charset=utf-8', |
|
| 24 | + 'dataType' => 'json' |
|
| 25 | + ); |
|
| 26 | + public $requestOptions = array(); |
|
| 27 | + /** |
|
| 28 | + * @var OAuth |
|
| 29 | + */ |
|
| 30 | + protected $auth; |
|
| 31 | + |
|
| 32 | + |
|
| 33 | + /** |
|
| 34 | + * JCFirebase constructor. |
|
| 35 | + * |
|
| 36 | + * @param $firebaseURI |
|
| 37 | + * @param array $firebaseAuth |
|
| 38 | + * @param string $firebaseDefaultPath |
|
| 39 | + */ |
|
| 40 | + public function __construct( $firebaseURI, $firebaseAuth = array(), $firebaseDefaultPath = '/' ) { |
|
| 41 | + $this->firebaseURI = $firebaseURI; |
|
| 42 | + $this->firebaseDefaultPath = $firebaseDefaultPath; |
|
| 43 | + $this->setAuth( $firebaseAuth ); |
|
| 44 | + } |
|
| 45 | + |
|
| 46 | + /** |
|
| 47 | + * @param $firebaseURI |
|
| 48 | + * @param $keyFile |
|
| 49 | + * @param string $firebaseDefaultPath |
|
| 50 | + * |
|
| 51 | + * @return JCFirebase |
|
| 52 | + * @throws \Exception |
|
| 53 | + */ |
|
| 54 | + public static function fromKeyFile( $firebaseURI, $keyFile, $firebaseDefaultPath = '/' ) { |
|
| 55 | + $keyData = null; |
|
| 56 | + try { |
|
| 57 | + $keyData = json_decode( file_get_contents( $keyFile ) ); |
|
| 58 | + } catch ( \Exception $exception ) { |
|
| 59 | + $keyData = json_decode( Requests::get( $keyFile ) ); |
|
| 60 | + } |
|
| 61 | + |
|
| 62 | + if ( $keyData ) { |
|
| 63 | + $serviceAccount = $keyData->client_email; |
|
| 64 | + $privateKey = $keyData->private_key; |
|
| 65 | + |
|
| 66 | + return new self( $firebaseURI, array( |
|
| 67 | + 'key' => $privateKey, |
|
| 68 | + 'iss' => $serviceAccount |
|
| 69 | + ), $firebaseDefaultPath ); |
|
| 70 | + } else { |
|
| 71 | + throw new \Exception( "can't get data from key file" ); |
|
| 72 | + } |
|
| 73 | + } |
|
| 74 | + |
|
| 75 | + public function setAuth( $firebaseServiceAccount ) { |
|
| 76 | + if ( isset( $firebaseServiceAccount['key'] ) && isset( $firebaseServiceAccount['iss'] ) ) { |
|
| 77 | + $this->auth = new OAuth( $firebaseServiceAccount['key'], $firebaseServiceAccount['iss'] ); |
|
| 78 | + } |
|
| 79 | + } |
|
| 80 | + |
|
| 81 | + public function getPathURI( $path = '', $print = '' ) { |
|
| 82 | + //remove last slash from firebaseURI |
|
| 83 | + $template = '/'; |
|
| 84 | + $this->firebaseURI = rtrim( $this->firebaseURI, $template ); |
|
| 85 | + $path = rtrim( $path, $template ); |
|
| 86 | + $path = ltrim( $path, $template ); |
|
| 87 | + |
|
| 88 | + //check https |
|
| 89 | + if ( strpos( $this->firebaseURI, 'http://' ) !== false ) { |
|
| 90 | + throw new \Exception( "https is required." ); |
|
| 91 | + } |
|
| 92 | + |
|
| 93 | + //check firebaseURI |
|
| 94 | + if ( empty( $this->firebaseURI ) ) { |
|
| 95 | + throw new \Exception( "firebase URI is required" ); |
|
| 96 | + } |
|
| 97 | + |
|
| 98 | + if ( strpos( $this->firebaseDefaultPath, "/" ) !== 0 ) { |
|
| 99 | + throw new \Exception( "firebase default path must contain /" ); |
|
| 100 | + } |
|
| 101 | + |
|
| 102 | + $pathURI = $this->firebaseURI . $this->firebaseDefaultPath . $path . ".json"; |
|
| 103 | + |
|
| 104 | + //set query data |
|
| 105 | + $queryData = array(); |
|
| 106 | + if ( ! empty( $print ) ) { |
|
| 107 | + $queryData[ JCFirebaseOption::OPTION_PRINT ] = $print; |
|
| 108 | + } |
|
| 109 | + if ( ! empty( $queryData ) ) { |
|
| 110 | + $pathURI = $pathURI . '?' . http_build_query( $queryData ); |
|
| 111 | + } |
|
| 112 | + |
|
| 113 | + $this->refreshToken(); |
|
| 114 | + |
|
| 115 | + return $pathURI; |
|
| 116 | + } |
|
| 117 | + |
|
| 118 | + public function getShallow( $path = '', $options = array() ) { |
|
| 119 | + return Requests::get( |
|
| 120 | + $this->getPathURI( $path ) . '?' . http_build_query( array( |
|
| 121 | + JCFirebaseOption::OPTION_SHALLOW => JCFirebaseOption::SHALLOW_TRUE |
|
| 122 | + ) ), |
|
| 123 | + $this->requestHeader, |
|
| 124 | + $this->addDataToRequest( $options ) |
|
| 125 | + ); |
|
| 126 | + } |
|
| 127 | + |
|
| 128 | + /** |
|
| 129 | + * @param string $path |
|
| 130 | + * @param array $options |
|
| 131 | + * |
|
| 132 | + * @return \Requests_Response |
|
| 133 | + */ |
|
| 134 | + public function get( $path = '', $options = array() ) { |
|
| 135 | + return Requests::get( |
|
| 136 | + $this->addDataToPathURI( $path, $options ), $this->requestHeader, |
|
| 137 | + $this->addDataToRequest( $options ) |
|
| 138 | + ); |
|
| 139 | + } |
|
| 140 | + |
|
| 141 | + /** |
|
| 142 | + * @param string $path |
|
| 143 | + * @param array $options |
|
| 144 | + * |
|
| 145 | + * @return \Requests_Response |
|
| 146 | + */ |
|
| 147 | + public function put( $path = '', $options = array() ) { |
|
| 148 | + return Requests::put( $this->getPathURI( $path ), $this->requestHeader, |
|
| 149 | + $this->addDataToRequest( $options, true ) ); |
|
| 150 | + } |
|
| 151 | + |
|
| 152 | + /** |
|
| 153 | + * @param string $path |
|
| 154 | + * @param array $options |
|
| 155 | + * |
|
| 156 | + * @return \Requests_Response |
|
| 157 | + */ |
|
| 158 | + public function post( $path = '', $options = array() ) { |
|
| 159 | + return Requests::post( $this->getPathURI( $path ), $this->requestHeader, |
|
| 160 | + $this->addDataToRequest( $options, true ) ); |
|
| 161 | + } |
|
| 162 | + |
|
| 163 | + /** |
|
| 164 | + * @param string $path |
|
| 165 | + * @param array $options |
|
| 166 | + * |
|
| 167 | + * @return \Requests_Response |
|
| 168 | + */ |
|
| 169 | + public function patch( $path = '', $options = array() ) { |
|
| 170 | + return Requests::patch( $this->getPathURI( $path ), $this->requestHeader, |
|
| 171 | + $this->addDataToRequest( $options, true ) ); |
|
| 172 | + } |
|
| 173 | + |
|
| 174 | + /** |
|
| 175 | + * @param string $path |
|
| 176 | + * @param array $options |
|
| 177 | + * |
|
| 178 | + * @return \Requests_Response |
|
| 179 | + */ |
|
| 180 | + public function delete( $path = '', $options = array() ) { |
|
| 181 | + return Requests::delete( $this->getPathURI( $path ), $this->requestHeader, |
|
| 182 | + $this->addDataToRequest( $options ) ); |
|
| 183 | + } |
|
| 184 | + |
|
| 185 | + protected function refreshToken() { |
|
| 186 | + $this->requestHeader['Authorization'] = 'Bearer ' . $this->auth->getAccessToken(); |
|
| 187 | + } |
|
| 188 | + |
|
| 189 | + protected function addDataToPathURI( $path = '', $options = array(), $reqType = JCFirebaseOption::REQ_TYPE_GET ) { |
|
| 190 | + $print = ''; |
|
| 191 | + if ( isset( $options['print'] ) ) { |
|
| 192 | + if ( JCFirebaseOption::isAllowPrint( $reqType, $options['print'] ) ) { |
|
| 193 | + $print = $options['print']; |
|
| 194 | + } |
|
| 195 | + } |
|
| 196 | + |
|
| 197 | + return $this->getPathURI( $path, $print ); |
|
| 198 | + } |
|
| 199 | + |
|
| 200 | + protected function addDataToRequest( $options = array(), $jsonEncode = false ) { |
|
| 201 | + $requestOptions = array(); |
|
| 202 | + |
|
| 203 | + if ( isset( $options['data'] ) ) { |
|
| 204 | + $requestOptions = array_merge( $options['data'], $requestOptions ); |
|
| 205 | + } |
|
| 206 | + |
|
| 207 | + if ( $jsonEncode ) { |
|
| 208 | + $requestOptions = json_encode( $requestOptions ); |
|
| 209 | + } |
|
| 210 | + |
|
| 211 | + return $requestOptions; |
|
| 212 | + } |
|
| 213 | 213 | } |
| 214 | 214 | \ No newline at end of file |