예제 #1
0
 /**
  * @param BlockCypherHttpConfig $httpConfig
  * @param string $request
  * @param mixed $options
  * @return mixed|void
  * @throws BlockCypherConfigurationException
  * @throws BlockCypherInvalidCredentialException
  * @throws BlockCypherMissingCredentialException
  */
 public function handle($httpConfig, $request, $options)
 {
     $credential = $this->apiContext->getCredential();
     $config = $this->apiContext->getConfig();
     if ($credential == null) {
         // Try picking credentials from the config file
         $credMgr = BlockCypherCredentialManager::getInstance($config);
         $credValues = $credMgr->getCredentialObject();
         if (!is_array($credValues)) {
             throw new BlockCypherMissingCredentialException("Empty or invalid credentials passed");
         }
         $credential = new SimpleTokenCredential($credValues['accessToken']);
     }
     if ($credential == null || !$credential instanceof SimpleTokenCredential) {
         throw new BlockCypherInvalidCredentialException("Invalid credentials passed");
     }
     $path = isset($options['path']) ? $options['path'] : '';
     $url = $this->getAbsoluteUrl($path, $this->apiContext);
     $httpConfig->setUrl($url);
     if (!array_key_exists("User-Agent", $httpConfig->getHeaders())) {
         $httpConfig->addHeader("User-Agent", BlockCypherUserAgent::getValue(BlockCypherConstants::SDK_NAME, BlockCypherConstants::SDK_VERSION));
     }
     /*if (!is_null($credential) && $credential instanceof SimpleTokenCredential && is_null($httpConfig->getHeader('Authorization'))) {
           $httpConfig->addHeader('Authorization', "Bearer " . $credential->getAccessToken($config), false);
       }*/
     if ($httpConfig->getMethod() == 'POST' || $httpConfig->getMethod() == 'PUT') {
         $httpConfig->addHeader('BlockCypher-Request-Id', $this->apiContext->getRequestId());
     }
     // Add any additional Headers that they may have provided
     $headers = $this->apiContext->getRequestHeaders();
     foreach ($headers as $key => $value) {
         $httpConfig->addHeader($key, $value);
     }
 }
예제 #2
0
 public function testGetValue()
 {
     $ua = BlockCypherUserAgent::getValue("name", "version");
     list($id, $version, $features) = sscanf($ua, "BlockCypherSDK/%s %s (%s)");
     // Check that we pass the user agent in the expected format
     $this->assertNotNull($id);
     $this->assertNotNull($version);
     $this->assertNotNull($features);
     $this->assertEquals("name", $id);
     $this->assertEquals("version", $version);
     // Check that we pass in these minimal features
     $this->assertThat($features, $this->stringContains("OS="));
     $this->assertThat($features, $this->stringContains("Bit="));
     $this->assertThat($features, $this->stringContains("Lang="));
     $this->assertThat($features, $this->stringContains("V="));
     $this->assertGreaterThan(5, count(explode(';', $features)));
 }