protected function buildBasepath($url)
    {
        if (!preg_match('#(\w+:)?//#', $url)) {
            $url = '//' . $url;
        }

        $urlParts = parse_url($url);

        if (false === $urlParts) {
            throw new \InvalidArgumentException('Could not parse QBank URL.');
        }

        // Default to HTTP
        if (empty($urlParts['scheme'])) {
            $urlParts['scheme'] = 'http';
        }

        // Add the api path automatically if omitted for qbank.se hosted QBank instances
        if ((empty($urlParts['path']) || '/' == $urlParts['path'])
            && 'qbank.se' == substr($urlParts['host'], -strlen('qbank.se'))) {
            $urlParts['path'] = '/api/';
        }

        // Pad the end of the path with a slash
        if ('/' != substr($urlParts['path'], -1)) {
            $urlParts['path'] .= '/';
        }

        return $urlParts['scheme'] . '://' . $urlParts['host'] . (!empty($urlParts['port']) ? ':' . $urlParts['port'] : '') . $urlParts['path'];
    }