Beispiel #1
0
        public INotifoClient Build()
        {
            if (string.IsNullOrWhiteSpace(apiKey))
            {
                throw new InvalidOperationException("API Key not defined.");
            }

            if (string.IsNullOrWhiteSpace(apiUrl))
            {
                throw new InvalidOperationException("API URL not defined.");
            }

            if (!Uri.IsWellFormedUriString(apiUrl, UriKind.Absolute))
            {
                throw new InvalidOperationException("API URL is not a well defined absolute URL.");
            }

            var httpClient = new HttpClient
            {
                Timeout = timeout
            };

            httpClient.DefaultRequestHeaders.Add("ApiKey", apiKey);

            var client = new NotifoClient(httpClient, apiUrl);

            return(client);
        }
Beispiel #2
0
        /// <summary>
        /// Build a new instance of the <see cref="INotifoClient"/> class.
        /// </summary>
        /// <returns>The generated <see cref="INotifoClient"/> instance.</returns>
        /// <exception cref="InvalidOperationException">Configuration is not valid.</exception>
        public INotifoClient Build()
        {
            if (string.IsNullOrWhiteSpace(apiKey) && string.IsNullOrWhiteSpace(clientId) && string.IsNullOrWhiteSpace(clientSecret))
            {
                throw new InvalidOperationException("Neiter, API Key, nor Client ID and secret is defined.");
            }

            if (string.IsNullOrWhiteSpace(apiUrl))
            {
                throw new InvalidOperationException("API URL not defined.");
            }

            if (!Uri.IsWellFormedUriString(apiUrl, UriKind.Absolute))
            {
                throw new InvalidOperationException("API URL is not a well defined absolute URL.");
            }

            if (!string.IsNullOrWhiteSpace(apiKey))
            {
                httpClient ??= new HttpClient();
                httpClient.DefaultRequestHeaders.Add("ApiKey", apiKey);
            }
            else
            {
                httpClient ??= new HttpClient(new AuthenticatingHttpMessageHandler(new CachingAuthenticator(new Authenticator(apiUrl.TrimEnd('/'), clientId, clientSecret))));
            }

            httpClient.Timeout = timeout;

            var client = new NotifoClient(httpClient, apiUrl, readResponseAsString);

            return(client);
        }