Ejemplo n.º 1
0
        public ReturnMessage <string> HttpGetRequest(string requestUrl, ExecutionSettings settings, CredentialModel credentials, ApplicationUtil appUtil, Logger logger)
        {
            var retMessage = new ReturnMessage <string>();

            //Validate Credential Access Token
            if (!string.IsNullOrEmpty(credentials.AccessToken))
            {
                string     responseData = string.Empty;
                HttpClient client       = new HttpClient();
                try
                {
                    HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, requestUrl);
                    request.Headers.Add("orgId", credentials.OrganizationId);
                    request.Headers.Add("Authorization", "Zoho-oauthtoken " + credentials.AccessToken);
                    HttpResponseMessage response = client.SendAsync(request).Result;
                    responseData = response.Content.ReadAsStringAsync().Result;
                    if (!string.IsNullOrEmpty(responseData) && ((int)response.StatusCode >= 200 && (int)response.StatusCode <= 204))
                    {
                        var jsonData = XElement.Parse(JsonConvert.DeserializeXmlNode(responseData, "data").OuterXml);
                        retMessage.Value   = jsonData.ToString();
                        retMessage.Status  = true;
                        retMessage.Message = ((int)response.StatusCode).ToString();
                    }
                    else if ((int)response.StatusCode == 401)
                    {
                        retMessage.Message = ((int)response.StatusCode).ToString();
                        retMessage.Value   = responseData;
                    }
                    else
                    {
                        logger.StatusLog($"No data found from zoho desk for GET method, Status {response.StatusCode}", requestUrl);
                        retMessage.Message = ((int)response.StatusCode).ToString();
                        retMessage.Value   = responseData;
                    }

                    return(retMessage);
                }
                catch (WebException ex)
                {
                    var error    = ex.ReadResponse();
                    var response = ex.Response as HttpWebResponse;
                    logger.ErrorLog($"GET operation failed for url {requestUrl}", $"WebException : Error Status : {ex.Status}, Response Status: {response.StatusCode} and Response is : {responseData}");
                    retMessage.SetError(((int)response.StatusCode).ToString());
                    retMessage.AddReturn(error.Message);
                    return(retMessage);
                }
                catch (Exception ex)
                {
                    retMessage.Value  = ex.Message;
                    retMessage.Status = false;
                    return(retMessage);
                }
                finally
                {
                    client.Dispose();
                }
            }
            else
            {
                retMessage.SetError("401");
            }
            return(retMessage);
        }
Ejemplo n.º 2
0
        public ReturnMessage <string> HttpPostRequest(XElement elements, ExecutionSettings settings, string actionParams, CredentialModel credentials, ApplicationUtil appUtil, Logger logger)
        {
            var returnMessage = new ReturnMessage <string>();

            //Validate Credential Access Token
            if (!string.IsNullOrEmpty(credentials.AccessToken))
            {
                //Stream reader = null;
                HttpWebRequest  request  = null;
                HttpWebResponse response = null;
                XmlDocument     xmlDoc   = new XmlDocument();

                var urlParam  = this.GetElementData(elements, "UploadURL", logger, string.Empty);
                var sourceKey = this.GetElementData(elements, "SourceKey", logger, string.Empty);
                var converter = new XmlToJsonConverter(elements.ToString());
                var url       = $"{credentials.BaseUrl}{urlParam}";
                var jsonData  = converter.Format().Message;


                request = (HttpWebRequest)WebRequest.Create(url);
                request.Headers.Add("orgId", credentials.OrganizationId);
                request.Headers.Add("Authorization", "Zoho-oauthtoken " + credentials.AccessToken);
                request.Method      = actionParams;
                request.ContentType = "application/json";
                string actualResponse = string.Empty;
                try
                {
                    using (var streamWriter = new StreamWriter(request.GetRequestStream()))
                    {
                        streamWriter.Write(jsonData);
                        streamWriter.Flush();
                        streamWriter.Close();
                    }
                    using (response = (HttpWebResponse)request.GetResponse())
                    {
                        using (Stream responseStream = response.GetResponseStream())
                        {
                            using (StreamReader responseReader = new StreamReader(responseStream))
                            {
                                actualResponse = responseReader.ReadToEnd();
                                xmlDoc         = JsonConvert.DeserializeXmlNode(actualResponse, "Results");
                            }
                        }
                    }
                    var xResponse = XElement.Parse(xmlDoc.InnerXml);
                    if (!string.IsNullOrEmpty(sourceKey))
                    {
                        xResponse.Add(new XElement("SourceKey", sourceKey));
                    }

                    returnMessage.Value  = xResponse.ToString();
                    returnMessage.Status = true;
                }
                catch (WebException wex)
                {
                    var error = wex.ReadResponse();
                    response = wex.Response as HttpWebResponse;
                    returnMessage.SetError(((int)response.StatusCode).ToString());
                    returnMessage.AddReturn(error.Message);
                    logger.ErrorLog($"Post operation failed for url {url}", $"WebException : Status : {response.StatusCode}, Data is : {jsonData} and Response is : {actualResponse}");
                }
                catch (Exception ex)
                {
                    logger.ErrorLog($"Post operation failed for url {url}", ex);
                    returnMessage.AddException(ex);
                }
            }
            else
            {
                returnMessage.SetError("401");
            }
            return(returnMessage);
        }