ASTE.Modules.APIDiscovery.Controllers.ConfigApiController.getActiveProcess C# (CSharp) Method

getActiveProcess() private method

private getActiveProcess ( string process, string method, string version, string source ) : Task
process string
method string
version string
source string
return Task
        public async Task<ModuleInfo> getActiveProcess(string process, string method, string version, string source)
        {
            RestHelper rs = new RestHelper();
            Helper helper = new Helper();
            var api_key = helper.GetApiKey(Request);

            await rs.Log("Incoming getActiveProcess call from: " + source);

            if (string.IsNullOrEmpty(process))
            {
                await rs.Log("getActiveProcess failed: Missing process name from " + source + " at " + helper.GetClientIp(Request));
                HttpResponseMessage no_name_response = this.Request.CreateErrorResponse(HttpStatusCode.BadRequest, string.Format("Process name cannot be empty"));
                throw new HttpResponseException(no_name_response);
            }
            if (string.IsNullOrEmpty(method))
            {
                await rs.Log("getActiveProcess failed: Missing process method from " + source + " at " + helper.GetClientIp(Request));
                HttpResponseMessage no_method_response = this.Request.CreateErrorResponse(HttpStatusCode.BadRequest, string.Format("Process method cannot be empty"));
                throw new HttpResponseException(no_method_response);
            }
            if (string.IsNullOrEmpty(version))
            {
                await rs.Log("getActiveProcess failed: Missing process version from " + source + " at " + helper.GetClientIp(Request));
                HttpResponseMessage no_version_response = this.Request.CreateErrorResponse(HttpStatusCode.BadRequest, string.Format("Process version cannot be empty"));
                throw new HttpResponseException(no_version_response);
            }
            if (string.IsNullOrEmpty(source))
            {
                await rs.Log("getActiveProcess failed: Missing process source at " + helper.GetClientIp(Request));
                HttpResponseMessage no_source_response = this.Request.CreateErrorResponse(HttpStatusCode.BadRequest, string.Format("Process 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 == process).ToList();
                    if (activeModules == null)
                    {
                        var process_not_found_message = string.Format("Process {0} not found from: " + source + " at " + helper.GetClientIp(Request), process);
                        await rs.Log(process_not_found_message);
                        HttpResponseMessage response = this.Request.CreateErrorResponse(HttpStatusCode.BadRequest, string.Format("Process {0} not found.", process));
                        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 process {1} from: " + source + " at " + helper.GetClientIp(Request), version, process);
                        await rs.Log(version_not_found_message);
                        HttpResponseMessage version_response = this.Request.CreateErrorResponse(HttpStatusCode.BadRequest, string.Format("Version {0} is not available for process {1}.", version, process));
                        throw new HttpResponseException(version_response);
                    }
                    if (!MethodFound)
                    {
                        var method_not_found_message = string.Format("method {0} is not available for process {1} version {2} from: " + source +  " at" + helper.GetClientIp(Request), method, process, version);
                        await rs.Log(method_not_found_message);
                        HttpResponseMessage method_response = this.Request.CreateErrorResponse(HttpStatusCode.BadRequest, string.Format("method {0} is not available for process {1} version {2}.", method, process, version));
                        throw new HttpResponseException(method_response);
                    }

                    var message = string.Format("Version {0} is not available for process {1} from: " + source + " at " + helper.GetClientIp(Request), version, process);
                    await rs.Log(message);
                    HttpResponseMessage error_response = this.Request.CreateErrorResponse(HttpStatusCode.BadRequest, "Error while fetching process 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);
            }

        }
    }