Parses an endpoint returned from the discovery API into an array with
'host' and 'path' keys.

@param $endpoint
@return array
340238
private function parseEndpoint($endpoint)
    {
        $parsed = parse_url($endpoint);

        // parse_url() will correctly parse full URIs with schemes
        if (isset($parsed['host'])) {
            return $parsed;
        }

        // parse_url() will put host & path in 'path' if scheme is not provided
        if (isset($parsed['path'])) {
            $split = explode('/', $parsed['path'], 2);
            $parsed['host'] = $split[0];
            if (isset($split[1])) {
                $parsed['path'] = $split[1];
            } else {
                $parsed['path'] = '';
            }
            return $parsed;
        }

        throw new UnresolvedEndpointException("The supplied endpoint '"
            . "{$endpoint}' is invalid.");
    }
840303
private function normalizeEndpoint(Endpoint $endpoint)
    {
        $normalizedEndpoint = [
            'serviceName' => $endpoint->getServiceName(),
        ];

        if (null !== ($ipv4 = $endpoint->getIpv4())) {
            $normalizedEndpoint['ipv4'] = $ipv4;
        }

        if (null !== ($ipv6 = $endpoint->getIpv6())) {
            $normalizedEndpoint['ipv6'] = $ipv6;
        }

        if (null !== ($port = $endpoint->getPort())) {
            $normalizedEndpoint['port'] = $port;
        }

        return $normalizedEndpoint;
    }
1281920
private function buildSchemaForEndpoint(string $endpoint)
    {
        $schemaPathAndFilename = $this->endpointsConfiguration[$endpoint]['schema'];
        $content = Files::getFileContents($schemaPathAndFilename);
        $documentNode = Parser::parse($content);
        $this->schemaCache->set(urlencode($endpoint), $documentNode, [md5($schemaPathAndFilename)]);
        return $documentNode;
    }
350762
public function getEndpoint($endpoint, $account = null)
    {
        $isSandbox = (new ConfigurationRepository)->getAccountKey('sandbox', true, $account);

        if ($isSandbox) {
            return $this->resolveUrl(MPESA_SANDBOX, $endpoint);
        }

        return $this->resolveUrl(MPESA_PRODUCTION, $endpoint);
    }
590887
public function post($endpoint, array $parameters = array())
    {
        return $this->execute("POST", sprintf("%s%s", $this->host, $endpoint), $parameters);
    }
220181
private ImmutableList<EndPoint> getEndPointsFromHostDiscovery() {
        Iterable<ServiceEndPoint> hosts = _hostDiscovery.getHosts();

        ImmutableList.Builder<EndPoint> endPoints = ImmutableList.builder();
        for (final ServiceEndPoint host : hosts) {
            if (host.equals(_self)) {
                continue;
            }
            endPoints.add(new EndPoint() {
                @Override
                public String getAddress() {
                    return _endPointAdapter.toEndPointAddress(host);
                }

                @Override
                public boolean isValid() {
                    return Iterables.contains(_hostDiscovery.getHosts(), host);
                }
            });
        }

        return endPoints.build();
    }
259473
public function get(string $endPoint)
    {
        $parsedEndPoint = self::parseEndPoint($endPoint);
        return $this->sendRequest($parsedEndPoint, "GET");
    }
1244462
protected function readMessage(EndpointInterface $endpoint)
    {
        $options = $endpoint->getOptions();
        $method = $options[NoSQLConfigurableProtocol::OPTION_METHOD];
        $config = $this->methodsConfiguration[$method];
        $steps = $config[ConfigurableConsumerInterface::CONFIG_QUERY_STEPS];

        $context = $this->getConfHelper()->createContext($options);

        try {
            $this->configurableStepsProvider->executeSteps($steps, $options, $context);

            $result = $this->getConfHelper()->resolve(
                $config[ConfigurableConsumerInterface::CONFIG_QUERY_RESULT],
                $context
            );
        } catch (NoResultsException $exception) {
            $result = null;
            if ($options[ConfigurableDbalProtocol::OPTION_STOP_ON_NO_RESULTS]) {
                $this->stop();
            }
        }

        if (null == $result) {
            return null;
        } elseif (is_array($result)) {
            $result = new SerializableArray($result);
        }

        $context = new Context([
            Context::FLOWS_VERSION => $this->getFlowsVersion(),
            Context::TRANSACTION_ID => uniqid('', true),
            Context::ORIGINAL_FROM => $endpoint->getURI(),
        ]);

        return $this->smartesbHelper->getMessageFactory()->createMessage($result, [], $context);
    }
941498
public function addEndpoint($endpoint) 
    {
        if (!is_string($endpoint) || !Endpoint::isValid($endpoint)) {
            throw new ClientException(sprintf("invalid endpoint specification '%s'", $endpoint));
        }
        $endpoint = Endpoint::normalize($endpoint);
        $normalized = Endpoint::normalizeHostname($endpoint);

        assert(is_array($this->_values[self::OPTION_ENDPOINT]));
        $found = false;
        foreach ($this->_values[self::OPTION_ENDPOINT] as $key => $value) {
            if ($normalized === Endpoint::normalizeHostname($value)) {
                $this->_currentEndpointIndex = $key;
                $found = true;
                break;
            }
        }
        
        if ($found === false) {
            // a new endpoint we have not seen before
            $this->_values[self::OPTION_ENDPOINT][] = $endpoint;
            $this->_currentEndpointIndex = count($this->_values[self::OPTION_ENDPOINT]) - 1;
        }

        $this->storeOptionsInCache();
    }
416568
protected function getEndpointsWithResourceMethods(EndpointCollection $endpoints)
    {
        return Collection::make($endpoints)->filter(function ($endpoint) {
            return $endpoint instanceof MethodEndpoint &&
                    in_array($endpoint->method, $this->methods);
        })->all();
    }
361060
public function getEndpoints()
    {
        $uri = 'endpoints';
        $response = $this->client->getEndpoint()->get($uri);

        $endpoints = [];
        foreach (\GuzzleHttp\json_decode($response->getBody()) as $endpoint) {
            $endpoints[] = new Endpoint($this->client, $endpoint);
        }

        return $endpoints;
    }
382325
private static function makeEndpoints($endpoints)
    {
        if (is_string($endpoints)) {
            return self::makeEndpointsFromString($endpoints, self::ROUTE_METHODS);
        }

        return $endpoints;
    }
762852
protected function getStream(string $endpoint, array $params)
    {
        $response = $this->requestRaw($endpoint, 'POST', ['json' => $params]);

        if (false !== stripos($response->getHeaderLine('Content-disposition'), 'attachment')) {
            return StreamResponse::buildFromPsrResponse($response);
        }

        return $this->castResponseToType($response, $this->app['config']->get('response_type'));
    }
914141
public function findServiceByEndpoint($endpoint)
    {
        $url = $this->getNormalizedUrl($endpoint);

        foreach ($this->services as $service) {
            foreach ($service->getEndpoints() as $endpoint) {
                $endpointUrl = $this->getNormalizedUrl($endpoint->getPublicUrl());
                if (substr($url, 0, strlen($endpointUrl)) === $endpointUrl) {
                    return $service;
                }
            }
        }

        throw new \OutOfBoundsException(sprintf('There is no service for endpoint %s', $url));
    }
1348200
protected function makeGetRequest($endpoint, array $params) {
        // Constructs get url
        // e.g:
        // endpoint = 'ip-address/report'
        //
        // params => array(
        //                'ip'       => '192.168.2.1',
        //                'apikey'   => 'supersecureapikey'
        //            )
        //
        // It maps to:
        // https://www.virustotal.com/vtapi/v2/ip-address/report?ip=192.168.2.1&apikey=supersecureapikey
        try {
            $url = self::API_ENDPOINT . $endpoint . '?'. http_build_query($params);
            $response = $this->_client->get($url);
            $this->validateResponse($response->getStatusCode());
            return $response;
        } catch(ClientException $e) {
            $this->validateResponse($e->getResponse()->getStatusCode());
        }
    }
956849
private function getOrCreateEndpoint($endpoint)
    {
        if (!array_key_exists($endpoint, $this->endpoints)) {
            if (!array_key_exists($endpoint, self::$endpointClassMap)) {
                throw new \Exception(sprintf('Unknown endpoint "%s"', $endpoint));
            }

            $endpointClass = self::$endpointClassMap[$endpoint];
            $this->endpoints[$endpoint] = $this->createEndpoint($endpointClass);
        }

        return $this->endpoints[$endpoint];
    }
1206493
private function buildRequest($endpoint)
    {
        $method = strtolower($this->config[$endpoint]['method']);

        if (! in_array($method, array('get', 'post', 'put', 'delete'))) {
            throw new \Exception($endpoint . ' method must be GET, POST, PUT, or DELETE');
        }

        $request = $this->client->$method($this->config[$endpoint]['path']);

        if (isset($this->config['auth'])) {
            $request->setAuth($this->config['auth']['user'], $this->config['auth']['password']);
        }

        return $request;
    }