Ejemplo n.º 1
0
        /// <summary>
        /// Common method for making GET calls
        /// </summary>
        public async Task <T> GetAsync <T>(string relativePath,
                                           Dictionary <string, string> headerDictionary      = null,
                                           Dictionary <string, string> queryStringDictionary = null)
        {
            T      result      = default(T);
            string queryString = string.Empty;

            if (queryStringDictionary != null)
            {
                queryString = BuildQueryString(queryStringDictionary);
            }

            var requestUrl = CreateRequestUri(string.Format(System.Globalization.CultureInfo.InvariantCulture,
                                                            relativePath), queryString);

            if (headerDictionary != null)
            {
                foreach (var headerItem in headerDictionary)
                {
                    _httpClient.DefaultRequestHeaders.Add(headerItem.Key, headerItem.Value);
                }
            }

            var response = await _httpClient.GetAsync(requestUrl, HttpCompletionOption.ResponseHeadersRead);

            response.EnsureSuccessStatusCode();
            var data = await response.Content.ReadAsStringAsync();

            if (!string.IsNullOrEmpty(data))
            {
                result = JsonConverter.ConvertFromJson <T>(data);
            }
            return(result);
        }
Ejemplo n.º 2
0
        public async Task <TResponse> PatchAsync <TResponse, TRequest>(string relativePath, TRequest content,
                                                                       Dictionary <string, string> headerDictionary = null)
        {
            var requestUrl = CreateRequestUri(string.Format(System.Globalization.CultureInfo.InvariantCulture,
                                                            relativePath), string.Empty);

            if (headerDictionary != null)
            {
                foreach (var headerItem in headerDictionary)
                {
                    _httpClient.DefaultRequestHeaders.Add(headerItem.Key, headerItem.Value);
                }
            }

            var response = await _httpClient.PatchAsync(requestUrl.ToString(), CreateHttpContent(content));

            if (response.IsSuccessStatusCode)
            {
                var data = await response.Content.ReadAsStringAsync();

                return(JsonConverter.ConvertFromJson <TResponse>(data));
            }

            return(default(TResponse));
        }
Ejemplo n.º 3
0
        public async Task <TokenDto> GetToken(BaseTokenSettings baseTokenSettings,
                                              string baseTokenUrl, string tokenPath,
                                              CancellationToken cancellationToken = default(CancellationToken))
        {
            if (!baseTokenSettings.TokenNeeded)
            {
                return(null);
            }

            var clientCredentials = Credentials.BuildCredentialsDictionary(baseTokenSettings);

            var builder = new UriBuilder(new Uri(baseTokenUrl))
            {
                Path = tokenPath
            };

            var response = await _httpClient.PostAsync(builder.Uri, new FormUrlEncodedContent(clientCredentials), cancellationToken);

            if (response.IsSuccessStatusCode)
            {
                var data = await response.Content.ReadAsStringAsync();

                return(JsonConverter.ConvertFromJson <TokenDto>(data));
            }

            return(null);
        }
        private async Task <Tuple <ResponseMessageDto, MessageQueue> > Action(MessageQueue messageQueue)
        {
            var employee = JsonConverter.ConvertFromJson <EmployeeDto>(messageQueue.SourceRawJson);
            // TODO: introduce a template
            var message = $"Happy birthday {employee.Name}, {employee.LastName}";

            var sendEmail = await _sendEmailHelper.SendEmailAsync("*****@*****.**", message,
                                                                  "Birthday Wishes", true, false);

            if (sendEmail)
            {
                messageQueue.MessageStatus = MessageStatusEnum.Sent;
                return(new Tuple <ResponseMessageDto, MessageQueue>(new ResponseMessageDto
                {
                    Success = true
                }, messageQueue));
            }
            else
            {
                messageQueue.MessageStatus = MessageStatusEnum.Failed;
                return(new Tuple <ResponseMessageDto, MessageQueue>(new ResponseMessageDto
                {
                    Success = false,
                    Errors = new List <string>
                    {
                        "Email could not be sent"
                    }
                }, messageQueue));
            }
        }
Ejemplo n.º 5
0
        private async Task <Tuple <ResponseMessageDto, MessageQueue> > Action(MessageQueue messageQueue)
        {
            var employee = JsonConverter.ConvertFromJson <EmployeeDto>(messageQueue.SourceRawJson);
            // TODO: introduce a template
            var message = $"Welcome to iOCO {employee.Name}, {employee.LastName}, we are so proud to have you on board";

            var sendEmail = await _sendEmailHelper.SendEmailAsync("*****@*****.**", message,
                                                                  "Birthday Wishes", true, false);

            if (sendEmail)
            {
                return(new Tuple <ResponseMessageDto, MessageQueue>(new ResponseMessageDto
                {
                    Success = true
                }, messageQueue));
            }
            else
            {
                return(new Tuple <ResponseMessageDto, MessageQueue>(new ResponseMessageDto
                {
                    Success = false,
                    Errors = new List <string>
                    {
                        "Email could not be sent"
                    }
                }, messageQueue));
            }
        }
Ejemplo n.º 6
0
        public void ConvertFromJson_PassInvalidJsonFile2_ThrowsArgumentException()
        {
            //Arange
            string filepath = @"D:\InvalidExport2.json";

            //Assert
            Assert.ThrowsException <ArgumentException>(() => JsonConverter.ConvertFromJson(filepath), "Invalid data!");
        }
Ejemplo n.º 7
0
        public void ConvertFromJson_PassInvalidFilepath_ThrowsArugmentException()
        {
            //Arrange
            string filepath = @"D:\Expot.json";

            //Assert
            Assert.ThrowsException <ArgumentException>(() => JsonConverter.ConvertFromJson(filepath), "Invalid filepath!");
        }
Ejemplo n.º 8
0
        /// <summary>
        /// Common method for making POST calls
        /// </summary>
        public async Task <T> PostAsync <T>(Uri requestUrl, T content)
        {
            var response = await _httpClient.PostAsync(requestUrl.ToString(), CreateHttpContent <T>(content));

            if (response.IsSuccessStatusCode)
            {
                var data = await response.Content.ReadAsStringAsync();

                return(JsonConverter.ConvertFromJson <T>(data));
            }

            return(default(T));
        }