예제 #1
0
 function testApiKey()
 {
     Postmaster::setApiKey("example-api-key");
     $result = Postmaster::getApiKey();
     $expected = "example-api-key";
     $this->assertEquals($expected, $result);
 }
예제 #2
0
 function testInvalidAuthError()
 {
     $apiKey = Postmaster::getApiKey();
     Postmaster::setApiKey(substr($apiKey, 0, -1));
     try {
         $result = Postmaster_AddressValidation::validate(array());
     } catch (Permission_Error $expected) {
         $msg = $expected->getMessage();
         $this->assertStringStartsWith('Invalid authorization', $msg);
         return;
     }
     $this->fail('An expected exception has not been raised.');
 }
예제 #3
0
 public function request($meth, $url, $params = null, $headers = null)
 {
     $absUrl = self::apiUrl($url);
     $apiKey = Postmaster::getApiKey();
     if (!$params) {
         $params = array();
     }
     $ua = array('bindings_version' => Postmaster::VERSION, 'lang' => 'php', 'lang_version' => phpversion(), 'publisher' => 'stripe', 'uname' => php_uname());
     $allHeaders = array('X-Postmaster-Client-User-Agent: ' . json_encode($ua), 'User-Agent: Postmaster/v1 PhpBindings/' . Postmaster::VERSION);
     if ($headers) {
         $allHeaders = array_merge($allHeaders, $headers);
     }
     list($rbody, $rcode) = $this->_curlRequest($meth, $absUrl, $allHeaders, $params, $apiKey);
     if ($rbody == 'OK') {
         $resp = $rbody;
     } else {
         try {
             $resp = json_decode($rbody, true);
         } catch (Exception $e) {
             throw new Postmaster_Error("Invalid response body from API: {$rbody} (HTTP response code was {$rcode})", $rcode, $rbody);
         }
     }
     if ($rcode < 200 || $rcode >= 300) {
         if (is_array($resp) && array_key_exists('message', $resp)) {
             $msg = $resp['message'];
         } else {
             $msg = "Unknown API error";
         }
         if ($rcode == 400) {
             throw new InvalidData_Error($msg, $rbody, $rcode, $resp);
         } else {
             if ($rcode == 401) {
                 throw new Authentication_Error($msg, $rbody, $rcode, $resp);
             } else {
                 if ($rcode == 403) {
                     throw new Permission_Error($msg, $rbody, $rcode, $resp);
                 }
             }
         }
         throw new API_Error($msg, $rbody, $rcode, $resp);
     }
     return $resp;
 }