/// <summary>
        /// Makes a DELETE call to the specified API.
        /// </summary>
        /// <param name="serviceName">Name of the service as specified by <see cref="ServiceLocatorDomain"/>.</param>
        /// <param name="path">Path to the API being called on the service.</param>
        /// <returns>A boolean value representing result of operation.</returns>
        public async Task <bool> DeleteAsync(ServiceLocatorDomain serviceName, string path)
        {
            var uri = new Uri($"{this.serviceLocator.GetServiceUri(serviceName)}/{path}");

            this.ResetRestClientHeaders();
            var response = await Client.DeleteAsync(uri).ConfigureAwait(true);

            response.EnsureSuccessStatusCode();

            return(response.IsSuccessStatusCode);
        }
Example #2
0
        /// <summary>
        /// Method that retrieves the service endpoint from applicatoin configuration as specified by <see cref="ServiceLocatorDomain"/>.
        /// </summary>
        /// <param name="serviceName">Name of your service as specified by <see cref="ServiceLocatorDomain"/>.</param>
        /// <returns>Service endpoint URL.</returns>
        public Uri GetServiceUri(ServiceLocatorDomain serviceName)
        {
            // Transform from Enum to string
            var key = serviceName.ToString();

            this.logger.LogInformation($"Locating Service URL with Key {key}");

            // Retrieve URL from Configuration
            var uri = this.configuration.GetSection("ServiceLocatorEndpoints")[key];

            this.logger.LogInformation($"Target Service URL is {uri}");

            if (uri == null)
            {
                throw new ArgumentNullException($"Uri key for {key} has not been configured. Add to it UserSecrets or appsettings.json");
            }

            return(new Uri(uri));
        }
        /// <summary>
        /// Makes a POST call to the specified API.
        /// </summary>
        /// <typeparam name="TReturnMessage">Object type returned by the API.</typeparam>
        /// <param name="serviceName">Name of the service as specified by <see cref="ServiceLocatorDomain"/>.</param>
        /// <param name="path">Path to the API being called on the service.</param>
        /// <param name="dataObject">Object to post to the API.</param>
        /// <returns>Return object as specified by the API.</returns>
        public async Task <TReturnMessage> PostAsync <TReturnMessage>(ServiceLocatorDomain serviceName, string path, object dataObject = null)
            where TReturnMessage : class, new()
        {
            var uri = new Uri($"{this.serviceLocator.GetServiceUri(serviceName)}/{path}");

            var content = dataObject != null?JsonConvert.SerializeObject(dataObject) : "{}";

            using (StringContent stringContent = new StringContent(content, Encoding.UTF8, "application/json"))
            {
                this.ResetRestClientHeaders();
                var response = await Client.PostAsync(uri, stringContent).ConfigureAwait(false);

                response.EnsureSuccessStatusCode();

                if (!response.IsSuccessStatusCode)
                {
                    return(await Task.FromResult(new TReturnMessage()).ConfigureAwait(false));
                }

                var result = await response.Content.ReadAsStringAsync().ConfigureAwait(false);

                return(JsonConvert.DeserializeObject <TReturnMessage>(result));
            }
        }
        /// <summary>
        /// Makes a GET call to the specified API.
        /// </summary>
        /// <typeparam name="TReturnMessage">Object type returned by the API.</typeparam>
        /// <param name="serviceName">Name of the service as specified by <see cref="ServiceLocatorDomain"/>.</param>
        /// <param name="path">Path to the API being called on the service.</param>
        /// <returns>Return bject as specified by the API.</returns>
        public async Task <TReturnMessage> GetAsync <TReturnMessage>(ServiceLocatorDomain serviceName, string path)
            where TReturnMessage : class, new()
        {
            HttpResponseMessage response;

            var uri = new Uri($"{this.serviceLocator.GetServiceUri(serviceName)}/{path}");

            // Here is actual call to target service
            this.ResetRestClientHeaders();
            response = await Client.GetAsync(uri).ConfigureAwait(true);

            if (!response.IsSuccessStatusCode)
            {
                var ex = new HttpRequestException($"{response.StatusCode} -- {response.ReasonPhrase}");

                // Stuff the Http StatusCode in the Data collection with key 'StatusCode'
                ex.Data.Add("StatusCode", response.StatusCode);
                throw ex;
            }

            var result = await response.Content.ReadAsStringAsync().ConfigureAwait(true);

            return(JsonConvert.DeserializeObject <TReturnMessage>(result));
        }