/** * @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; }
/** * @param array $handlers Array of handlers * @param string $path Resource path relative to base service endpoint * @param string $method HTTP method - one of GET, POST, PUT, DELETE, PATCH etc * @param string $data Request payload * @param array $headers HTTP headers * @return mixed * @throws \BlockCypher\Exception\BlockCypherConnectionException */ public function execute($handlers = array(), $path, $method, $data = '', $headers = array()) { $config = $this->apiContext->getConfig(); $httpConfig = new BlockCypherHttpConfig(null, $method, $config); $headers = $headers ? $headers : array(); $httpConfig->setHeaders($headers + array('Content-Type' => 'application/json')); /** @var \BlockCypher\Handler\IBlockCypherHandler $handler */ foreach ($handlers as $handler) { if (!is_object($handler)) { $fullHandler = "\\" . (string) $handler; $handler = new $fullHandler($this->apiContext); } $handler->handle($httpConfig, $data, array('path' => $path, 'apiContext' => $this->apiContext)); } $connection = new BlockCypherHttpConnection($httpConfig, $config); $response = $connection->execute($data); return $response; }
/** * @test */ public function testProxyOpts() { $proxy = 'http://*****:*****@hostname:8081'; $o = new BlockCypherHttpConfig(); $o->setHttpProxy($proxy); $curlOpts = $o->getCurlOptions(); $this->assertEquals('hostname:8081', $curlOpts[CURLOPT_PROXY]); $this->assertEquals('me:secret', $curlOpts[CURLOPT_PROXYUSERPWD]); $this->setExpectedException('BlockCypher\\Exception\\BlockCypherConfigurationException'); $o->setHttpProxy('invalid string'); }