ASTE.Modules.APIDiscovery.Helpers.RestHelper.Log C# (CSharp) Метод

Log() публичный Метод

public Log ( string message ) : Task
message string
Результат Task
        public async Task<string> Log(string message)
        {
            LoggerModel logger = new LoggerModel();
            logger.eventTimestamp = DateTime.Now.ToString("yyyy-MM-dd HH:mm");
            logger.source = "API DISCOVERY";
            logger.message = message;

            APIDiscoveryContext ctx = new APIDiscoveryContext();
            var logmodule = ctx.modules.Where(x => x.guid == Constants.ASTE_MODULES_LOGGER && !x.isdeleted && x.active && x.version == "1.0").FirstOrDefault();
            if(logmodule == null)
            {
                throw new HttpRequestException("Mandatory module not installed: LoggerModule");
            }
            using (var client = new HttpClient())
            {
                client.BaseAddress = new Uri(logmodule.api_url + "/");
                client.DefaultRequestHeaders.Accept.Clear();
                client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

                var callUrl = logmodule.version + "/log";
                if (logger != null)
                {
                    HttpResponseMessage response = await client.PutAsJsonAsync(callUrl, logger);
                    if (response.IsSuccessStatusCode)
                    {
                        var data = await response.Content.ReadAsAsync<object>();
                        return data.ToString();

                    }
                    else
                    {
                        throw new HttpResponseException(response);
                    }
                }
            }
            return null;
        }
    }

Usage Example

        public async Task<ModuleInfo> GetActiveModules (string module, string method, string version, string source)
        {
            RestHelper rs = new RestHelper();
            Helper helper = new Helper();
            var api_key = helper.GetApiKey(Request);
   
            await rs.Log("Incoming getActiveModule call from: " + source + " at " + helper.GetClientIp(Request));

            if(string.IsNullOrEmpty(module))
            {
                await rs.Log("GetActiveModule failed: Missing module name from " + source + " at " + helper.GetClientIp(Request) );
                HttpResponseMessage no_name_response = this.Request.CreateErrorResponse(HttpStatusCode.BadRequest, string.Format("module name cannot be empty"));
                throw new HttpResponseException(no_name_response);
            }
            if (string.IsNullOrEmpty(method))
            {
                await rs.Log("GetActiveModule failed: Missing module method from " + source + " at " + helper.GetClientIp(Request));
                HttpResponseMessage no_method_response = this.Request.CreateErrorResponse(HttpStatusCode.BadRequest, string.Format("module method cannot be empty"));
                throw new HttpResponseException(no_method_response);
            }
            if (string.IsNullOrEmpty(version))
            {
                await rs.Log("GetActiveModule failed: Missing module version from " + source + " at " + helper.GetClientIp(Request));
                HttpResponseMessage no_version_response = this.Request.CreateErrorResponse(HttpStatusCode.BadRequest, string.Format("module version cannot be empty"));
                throw new HttpResponseException(no_version_response);
            }
            if (string.IsNullOrEmpty(source))
            {
                await rs.Log("GetActiveModule failed: Missing module source at " + helper.GetClientIp(Request));
                HttpResponseMessage no_source_response = this.Request.CreateErrorResponse(HttpStatusCode.BadRequest, string.Format("module source cannot be empty"));
                throw new HttpResponseException(no_source_response);
            }

            if (!string.IsNullOrEmpty(api_key))
            {
             
                var exists = ctx.clients.Where(x => x.api_key == api_key && !x.isdeleted).FirstOrDefault();
                if (exists != null)
                {

                    var activeModules = ctx.modules.Where(x => !x.isdeleted && x.active && !x.isProcess && x.name == module).ToList();
                    if(activeModules == null)
                    {
                        var module_not_found_message = string.Format("Module {0} not found from: " + source + " at " + helper.GetClientIp(Request), module);
                        await rs.Log(module_not_found_message);
                        HttpResponseMessage response = this.Request.CreateErrorResponse(HttpStatusCode.BadRequest, string.Format("Module {0} not found",module));
                        throw new HttpResponseException(response);
                    }
                    var VersionFound = false;
                    var MethodFound = false;
                    foreach(var m in activeModules)
                    {
                        if(m.version == version)
                        {
                            VersionFound = true;
                            foreach (var moduleMethod in m.methods)
                            {
                                if(moduleMethod.name == method)
                                {
                                    MethodFound = true;
                                    ModuleInfo info = new ModuleInfo()
                                    {
                                        api_url = m.api_url,
                                        name = m.name,
                                        version = m.version
                                    };
                                    return info;
                                }
                            }
                        }
                    }

                    if(!VersionFound)
                    {
                        var version_not_found_message = string.Format("Version {0} is not available for module {1} from: " + source + " at " + helper.GetClientIp(Request),version, module);
                        await rs.Log(version_not_found_message);
                        HttpResponseMessage version_response = this.Request.CreateErrorResponse(HttpStatusCode.BadRequest, string.Format("Version {0} is not available for module {1}.",version,module));
                        throw new HttpResponseException(version_response);
                    }
                    if(!MethodFound)
                    {
                        var method_not_found_message = string.Format("method {0} is not available for module {1} version {2} from: " + source + " at " + helper.GetClientIp(Request), method, module,version);
                        await rs.Log(method_not_found_message);
                        HttpResponseMessage method_response = this.Request.CreateErrorResponse(HttpStatusCode.BadRequest, string.Format("method {0} is not available for module {1} version {2}.", method, module, version));
                        throw new HttpResponseException(method_response);
                    }

                    var message = string.Format("Version {0} is not available for module {1} from: " + source + " at " + helper.GetClientIp(Request), version, module);
                    await rs.Log(message);
                    HttpResponseMessage error_response = this.Request.CreateErrorResponse(HttpStatusCode.BadRequest, "Error while fetching module information");
                    throw new HttpResponseException(error_response);

                }
                else
                {
                    var message = string.Format("Invalid API key from: " + source + " at " + helper.GetClientIp(Request));
                    await rs.Log(message);
                    HttpResponseMessage response = this.Request.CreateErrorResponse(HttpStatusCode.BadRequest, "Invalid API key.");
                    throw new HttpResponseException(response);
                }
            }
            else
            {
                var message = string.Format("API key missing from: " + source + " at " + helper.GetClientIp(Request));
                await rs.Log(message);
                HttpResponseMessage response = this.Request.CreateErrorResponse(HttpStatusCode.BadRequest, "API key missing.");
                throw new HttpResponseException(response);
            }

        }