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);
        }
        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;
        }
Exemple #3
0
        public async Task <ActionResult> Add(string url, string version)
        {
            var ctx = new APIDiscoveryContext();

            if (ModelState.IsValid)
            {
                RestHelper helper = new RestHelper();
                try
                {
                    var status = await helper.Ping(url, version);

                    if (status != null && status.status.ToLower() == Constants.ASTE_PINGSTATUS_SUCCESS)
                    {
                        try
                        {
                            var config = await helper.Config(url, version);

                            if (config != null)
                            {
                                if (config.type.ToLower() == Constants.ASTE_MODULETYPE_MODULE)
                                {
                                    Module new_module = new Module();
                                    new_module.name            = config.name;
                                    new_module.created         = DateTime.Now;
                                    new_module.guid            = config.guid;
                                    new_module.description     = config.description;
                                    new_module.author          = config.author;
                                    new_module.authorContact   = config.authorContact;
                                    new_module.isProcess       = false;
                                    new_module.methods         = new List <ModuleMethod>();
                                    new_module.my_dependencies = new List <ModuleDependency>();
                                    new_module.api_url         = url;
                                    new_module.version         = version;
                                    if (config.methods != null)
                                    {
                                        foreach (var m in config.methods)
                                        {
                                            new_module.methods.Add(new ModuleMethod()
                                            {
                                                created    = new DateTime(),
                                                name       = m.method,
                                                methodInfo = m.@params
                                            });
                                        }
                                    }
                                    var dependency_not_found = false;
                                    foreach (var d in config.dependencies)
                                    {
                                        var db_module = ctx.modules.Where(x => x.guid == d).FirstOrDefault();
                                        if (db_module == null)
                                        {
                                            ModelState.AddModelError("Config", string.Format("Module dependency {0} was not found, could not install API to discovery", d));
                                            return(View("New", new Models.newModelOrProcess()
                                            {
                                                url = url, version = version
                                            }));
                                        }
                                        else
                                        {
                                            new_module.my_dependencies.Add(new ModuleDependency()
                                            {
                                                created       = DateTime.Now,
                                                dependency_id = db_module.id,
                                                name          = db_module.name
                                            });
                                        }
                                    }
                                    if (!dependency_not_found)
                                    {
                                        ctx.modules.Add(new_module);
                                        ctx.SaveChanges();
                                    }
                                }
                                else
                                {
                                    ModelState.AddModelError("Config", "Contacted api is a process, not a module");
                                    return(View("New", new Models.newModelOrProcess()
                                    {
                                        url = url, version = version
                                    }));
                                }
                            }
                            else
                            {
                                ModelState.AddModelError("Config", "config did not return any data");
                                return(View("New", new Models.newModelOrProcess()
                                {
                                    url = url, version = version
                                }));
                            }
                        }
                        catch (Exception ex)
                        {
                            ModelState.AddModelError("Config", "Url of the API could not be resolved, or the service is not listening - config method");
                            return(View("New", new Models.newModelOrProcess()
                            {
                                url = url, version = version
                            }));
                        }
                    }
                    else
                    {
                        ModelState.AddModelError("Ping", status.message);
                        return(View("New", new Models.newModelOrProcess()
                        {
                            url = url, version = version
                        }));
                    }
                }
                catch (Exception ex)
                {
                    ModelState.AddModelError("Ping", "Url of the API could not be resolved, or the service is not listening - ping method");
                    return(View("New", new Models.newModelOrProcess()
                    {
                        url = url, version = version
                    }));
                }

                return(RedirectToAction("Index", "Module"));
            }

            return(View("New", new Models.newModelOrProcess()
            {
                url = url, version = version
            }));
        }
        public async Task<ActionResult> Add(string url, string version)
        {
            var ctx = new APIDiscoveryContext();
            if (ModelState.IsValid)
            {
                RestHelper helper = new RestHelper();
                try
                {
                    var status = await helper.Ping(url, version);
                    if (status != null && status.status.ToLower() == Constants.ASTE_PINGSTATUS_SUCCESS)
                    {
                        try
                        {
                            var config = await helper.Config(url, version);
                            if (config != null)
                            {
                                if (config.type.ToLower() == Constants.ASTE_MODULETYPE_PROCESS)
                                {
                                    Module new_module = new Module();
                                    new_module.name = config.name;
                                    new_module.created = DateTime.Now;
                                    new_module.guid = config.guid;
                                    new_module.description = config.description;
                                    new_module.author = config.author;
                                    new_module.authorContact = config.authorContact;
                                    new_module.isProcess = true;
                                    new_module.methods = new List<ModuleMethod>();
                                    new_module.my_dependencies = new List<ModuleDependency>();
                                    new_module.api_url = url;
                                    new_module.version = version;
                                    if (config.methods != null)
                                    {
                                        foreach (var m in config.methods)
                                        {
                                            new_module.methods.Add(new ModuleMethod()
                                            {
                                                created = new DateTime(),
                                                name = m.method,
                                                methodInfo = m.@params
                                            });
                                        }
                                    }
                                    var dependency_not_found = false;
                                    foreach (var d in config.dependencies)
                                    {
                                        var db_module = ctx.modules.Where(x => x.guid == d).FirstOrDefault();
                                        if (db_module == null)
                                        {
                                            ModelState.AddModelError("Config", "Process dependency {0} was not found, could not install API to discovery");
                                            dependency_not_found = true;
                                            break;
                                        }
                                        else
                                        {
                                            new_module.my_dependencies.Add(new ModuleDependency()
                                            {
                                                created = DateTime.Now,
                                                dependency_id = db_module.id,
                                                name = db_module.name

                                            });
                                        }
                                    }
                                    if (!dependency_not_found)
                                    {
                                        ctx.modules.Add(new_module);
                                        ctx.SaveChanges();
                                    }
                                }
                                else
                                {
                                    ModelState.AddModelError("Config", "Contacted api is a module, not a process");
                                    return View("New", new Models.newModelOrProcess() { url = url, version = version });
                                }

                            }
                            else
                            {
                                ModelState.AddModelError("Config", "config did not return any data");
                                return View("New", new Models.newModelOrProcess() { url = url, version = version });
                            }
                        }
                        catch (Exception ex)
                        {
                            ModelState.AddModelError("Config", "Url of the API could not be resolved, or the service is not listening - config method");
                            return View("New", new Models.newModelOrProcess() { url = url, version = version });
                        }
                    }
                    else
                    {
                        ModelState.AddModelError("Ping", status.message);
                        return View("New", new Models.newModelOrProcess() { url = url, version = version });
                    }
                }
                catch (Exception ex)
                {
                    ModelState.AddModelError("Ping", "Url of the API could not be resolved, or the service is not listening - ping method");
                    return View("New", new Models.newModelOrProcess() { url = url, version = version });
                }

                return RedirectToAction("Index", "Process");
            }

            return View("New", new Models.newModelOrProcess() { url = url, version = version });
        }