/** * @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); } }
/** * Gets all Http Headers * * @return array */ private function getHttpHeaders() { $ret = array(); foreach ($this->httpConfig->getHeaders() as $k => $v) { $ret[] = "{$k}: {$v}"; } return $ret; }
/** * @test */ public function testHeaderFunctions() { $o = new BlockCypherHttpConfig(); $o->addHeader('key1', 'value1'); $o->addHeader('key2', 'value'); $o->addHeader('key2', 'overwritten'); $this->assertEquals(2, count($o->getHeaders())); $this->assertEquals('overwritten', $o->getHeader('key2')); $this->assertNull($o->getHeader('key3')); $o = new BlockCypherHttpConfig(); $o->addHeader('key1', 'value1'); $o->addHeader('key2', 'value'); $o->addHeader('key2', 'and more', false); $this->assertEquals(2, count($o->getHeaders())); $this->assertEquals('value;and more', $o->getHeader('key2')); $o->removeHeader('key2'); $this->assertEquals(1, count($o->getHeaders())); $this->assertNull($o->getHeader('key2')); }