<commit_before><?php

/**
 * @author    Proximify Inc <<EMAIL>>
 * @copyright Copyright (c) 2020, Proximify Inc
 * @license   MIT
 */

namespace Proximify\Uniweb\API;

use Exception;

require 'RemoteConnection.php';

/**
 * Reference API Client for the Uniweb application.
 */
class UniwebClient
{
	const FILES = '_files_';

	/** @var string Default path for the connection credentials. */
	const CREDENTIALS_PATH = 'settings/credentials.json';

	/** @var string Alternative path for the connection credentials. */
	const ALT_CREDENTIALS_PATH = 'settings/credentials/credentials.json';

	/** @var array API credentials: homepage, clientName, clientSecret. */
	private $credentials;

	/**
	 * Constructs a UNIWeb client with given credentials. There is also a helper static
	 * function, getClient(), that can be used by passing the credential parameters
	 * as individual function arguments in order to construct a client object.
	 */
	public function __construct(?array $credentials = null)
	{
		date_default_timezone_set('UTC');

		$this->assertClientParams($credentials);

		$this->credentials = $credentials;
		$this->conn = new RemoteConnection();
	}

	/**
	 * Get the homepage URL defined in the credentials.
	 *
	 * @return string
	 */
	public function getInstanceUrl(): string
	{
		$url = trim($this->credentials['homepage'] ?? '');

		if (!$url) {
			throw new Exception("Invalid empty homepage URL in credentials");
		}

		$parts = parse_url($url);
		$host = $parts['host'] ?? '';
		$path = $parts['path'] ?? '';

		// If there is no host, there might be a missing '//'
		if (!$host && $path) {
			$parts = parse_url('//' . $url);
			$host = $parts['host'] ?? '';
			$path = $parts['path'] ?? '';
		}

		if (!$host) {
			throw new Exception("Invalid homepage URL");
		}

		// Path must end with a '/' iff it's not empty
		if ($path = trim($path, '/')) {
			$path .= '/';
		}

		// Only allow for the secure HTTPS protocol
		return 'https://' . $host . '/' . $path;
	}

	public function getClientName(): string
	{
		if (!($clientName = $this->credentials['clientName'] ?? false)) {<commit_msg>Added special case for localhost connections<commit_after><?php

/**
 * @author    Proximify Inc <<EMAIL>>
 * @copyright Copyright (c) 2020, Proximify Inc
 * @license   MIT
 */

namespace Proximify\Uniweb\API;

use Exception;

require 'RemoteConnection.php';

/**
 * Reference API Client for the Uniweb application.
 */
class UniwebClient
{
	const FILES = '_files_';

	/** @var string Default path for the connection credentials. */
	const CREDENTIALS_PATH = 'settings/credentials.json';

	/** @var string Alternative path for the connection credentials. */
	const ALT_CREDENTIALS_PATH = 'settings/credentials/credentials.json';

	/** @var array API credentials: homepage, clientName, clientSecret. */
	private $credentials;

	/**
	 * Constructs a UNIWeb client with given credentials. There is also a helper static
	 * function, getClient(), that can be used by passing the credential parameters
	 * as individual function arguments in order to construct a client object.
	 */
	public function __construct(?array $credentials = null)
	{
		date_default_timezone_set('UTC');

		$this->assertClientParams($credentials);

		$this->credentials = $credentials;
		$this->conn = new RemoteConnection();
	}

	/**
	 * Get the homepage URL defined in the credentials.
	 *
	 * @return string
	 */
	public function getInstanceUrl(): string
	{
		$url = trim($this->credentials['homepage'] ?? '');

		if (!$url) {
			throw new Exception("Invalid empty homepage URL in credentials");
		}

		$parts = parse_url($url);
		$host = $parts['host'] ?? '';
		$path = $parts['path'] ?? '';

		// If there is no host, there might be a missing '//'
		if (!$host && $path) {
			$parts = parse_url('//' . $url);
			$host = $parts['host'] ?? '';
			$path = $parts['path'] ?? '';
		}

		if (!$host) {
			throw new Exception("Invalid homepage URL");
		}

		// Path must end with a '/' iff it's not empty
		if ($path = trim($path, '/')) {
			$path .= '/';
		}

		if ($host == 'localhost' || $host == '127.0.0.1') {
			$scheme = $parts['scheme'] ?? 'http';
		} else {
			$scheme = 'https';
		}

		// Only allow for the secure HTTPS protocol
		return $scheme . '://' . $host . '/' . $path;
	}

	public function getClientName(): string
	{
		if (!($clientName = $this->credentials['clientName'] ?? false)) {
{'fraction_non_alphanumeric': 0.15778641664761034, 'fraction_numerical': 0.003201463526183398, 'mean_word_length': 3.5657620041753653, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 17, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 1, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5070412', 'n_tokens_mistral': 1464, 'n_tokens_neox': 1320, 'n_words': 416}