Exemplo n.º 1
0
        //public void CreateLogger(ILoggerFactory factory)
        //{
        //    _logger = factory.CreateLogger<Profile>();
        //}
        private uint GetScriptingProxyPort()
        {
            //throw new NotImplementedException();
            uint port = 36036;

            foreach (string appName in AppNames)
            {
                string path = "";
                if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
                {
                    path = Path.Combine(new string[] { AppDataFolder, AppAuthor, appName, FileName });
                }
                else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
                {
                    path = Path.Combine(new string[] { AppDataFolder, "Library", "Application Support", appName, FileName });
                }
                // string path = Path.Combine(new string[] { AppDataFolder, AppAuthor, appName, FileName });

                if (File.Exists(path))
                {
                    _logger?.LogInformation("Find port in a file: {0}", path);
                    try {
                        port = Convert.ToUInt16(File.ReadAllText(path));
                    }catch (Exception ex)
                    {
                        _logger?.LogWarning("Can't convert port to a number: {0}",
                                            JSONRequest.GetInnerMostException(ex).Message.ToString());
                        continue;
                        //Log Error
                    }
                    break;
                }
            }
            _logger?.LogInformation("use port: {0}", port);
            return(port);
        }
        public async Task <Tuple <string, EikonException> > SendJSONRequestAsync(ILogger logger,
                                                                                 string entity,
                                                                                 string payload,
                                                                                 uint id = 123)
        {
            string udfRequest = $"{{\"Entity\":{{\"E\":\"{entity}\",\"W\":{payload}}},\"ID\":\"{id}\" }}";
            string jsonData;
            HttpResponseMessage response = null;
            EikonException      error    = null;

            logger?.LogDebug("UDF Request: {0}", udfRequest);
            //using (HttpClient client = new HttpClient())
            {
                var request = new HttpRequestMessage(HttpMethod.Post, profile.Url);
                request.Headers.Add("x-tr-applicationid", profile.AppId);
                request.Content = new StringContent(udfRequest);
                request.Content.Headers.ContentType = new MediaTypeHeaderValue("application/json");
                try
                {
                    response = await client.SendAsync(request);
                }
                catch (Exception ex)
                {
                    error        = new EikonException(HttpStatusCode.InternalServerError, JSONRequest.GetInnerMostException(ex).Message.ToString(), ex.InnerException);
                    error.Source = "JSONRequest";
                    jsonData     = null;
                    logger?.LogError(JsonConvert.SerializeObject(error));
                    throw (error);
                    //return new Tuple<string, EikonException>(jsonData, error);
                }
                if (response.IsSuccessStatusCode)
                {
                    jsonData = await response.Content.ReadAsStringAsync();

                    logger?.LogDebug("UDF Response: {0}", jsonData);
                    if (jsonData.StartsWith("<") && jsonData.EndsWith(">"))
                    {
                        error        = new EikonException(HttpStatusCode.InternalServerError, $"Invalid JSON: {jsonData}");
                        error.Source = "JSONRequest";
                        jsonData     = null;
                        logger?.LogError(JsonConvert.SerializeObject(error));
                        throw (error);
                    }
                    else if (jsonData.Contains("ErrorCode") && jsonData.Contains("ErrorMessage"))
                    {
                        var eikonError = JsonConvert.DeserializeObject <EikonError>(jsonData);
                        error.ErrorCode = (HttpStatusCode)Enum.ToObject(typeof(HttpStatusCode), eikonError.ErrorCode);
                        error.Source    = "JSONRequest";
                        jsonData        = null;
                        logger?.LogError(JsonConvert.SerializeObject(error));
                        throw (error);
                    }
                    else if (jsonData.Contains("error") && jsonData.Contains("transactionId"))
                    {
                        error = new EikonException(HttpStatusCode.InternalServerError, jsonData);

                        error.Source = "JSONRequest";
                        logger?.LogError(JsonConvert.SerializeObject(error));
                        throw (error);
                    }
                }
                else
                {
                    error    = new EikonException(response.StatusCode, response.ToString());
                    jsonData = null;
                    logger?.LogError(JsonConvert.SerializeObject(error));
                }
                return(new Tuple <string, EikonException>(jsonData, error));
            }
        }