public Models.ServiceDescriptionNStatusInterMed allServices(int token) { string status; Models.ServiceDescriptionNStatusInterMed serviceDescription = new Models.ServiceDescriptionNStatusInterMed(); //creating a connection with the Authenticator via static AuthenticatorAccessInterface.remoteConnection() authenticateInterface = Models.AuthenticatorAccessInterface.remoteConnection(); //fetch result provided by the authenticator status = authenticateInterface.validate(token); //If token funtion return result is "validated" will return all services if (status.Equals("validated")) { List <Models.ServiceDescriptionIntermed> services = new List <Models.ServiceDescriptionIntermed>(); //Read all text in appropriate json text file String allServicesJson = File.ReadAllText(SOA_Utilities.Constants.pathForServiceReg); //Deserialize all services in json string to List<Models.ServiceDescriptionIntermed> object services = JsonConvert.DeserializeObject <List <Models.ServiceDescriptionIntermed> >(allServicesJson); //assigning to object of return type serviceDescription.Services = services; serviceDescription.Status = "Access Guranteed!"; serviceDescription.Reason = "Validated"; return(serviceDescription); } //If token funtion return result is not validated return authentication error else if (status.Equals("not validated"))//invalid token { serviceDescription.Reason = "Authentication Error"; serviceDescription.Status = "Denied"; return(serviceDescription); } //If token funtion return result is not either "not validated" or "validated" means there is a internal code error //could be a business logic error in the in the authenticator class else { serviceDescription.Reason = "Logic Error"; serviceDescription.Reason = "Authentication return error !"; return(serviceDescription); } }
/* * * This function searches for services in the registry * @param query which is the query * @returns Models.ServiceDescriptionNStatusInterMed which contains a list of services and the status of the function invocation * * References: Lab 3 * ReadAllLines and WriteAllText : https://docs.microsoft.com/en-us/dotnet/api/system.io.file.readalllines?view=net-5.0 * Uri.UnescapeDataString and EscapeDataString: https://docs.microsoft.com/en-us/dotnet/api/system.uri.unescapedatastring?view=net-5.0 * https://docs.microsoft.com/en-us/dotnet/api/system.text.regularexpressions.regex.unescape?view=net-5.0 * ASP.NET Web API Tutorial for Beginners _ ASP.NET Web API Crash Course 2020 (720p) : https://www.youtube.com/watch?v=iaeHaydhatE * Creating A REST Webservice With C# And Visual Studio (360p) : https://www.youtube.com/watch?v=LXZnuJY0dGI * Intro to WebAPI - One of the most powerful project types in C# (720p) : https://www.youtube.com/watch?v=vN9NRqv7xmY&t=1186s * C# Tutorial - How to create and Parse JSON Data : https://www.youtube.com/watch?v=NX3Um9E-AY0 * Stackover flow : https://stackoverflow.com/questions/9854917/how-can-i-find-a-specific-element-in-a-listt * Search query : https://docs.microsoft.com/en-us/powerapps/developer/data-platform/org-service/linq-query-examples */ public Models.ServiceDescriptionNStatusInterMed search(String query, int token) { string tokenStatus; //create connection with the authenticator authenticateInterface = Models.AuthenticatorAccessInterface.remoteConnection(); //fetch token status tokenStatus = authenticateInterface.validate(token); Models.ServiceDescriptionNStatusInterMed descriptionNStatusInterMed = new Models.ServiceDescriptionNStatusInterMed(); List <Models.ServiceDescriptionIntermed> serviceDescriptionsList = new List <Models.ServiceDescriptionIntermed>(); Models.ServiceDescriptionIntermed service = new Models.ServiceDescriptionIntermed(); //checking if token is "validated" if (tokenStatus.Equals("validated")) { //Checking if file exists if (File.Exists(SOA_Utilities.Constants.pathForServiceReg)) { //Read all text and deserialise string allServicesJSONFormat = File.ReadAllText(SOA_Utilities.Constants.pathForServiceReg); serviceDescriptionsList = JsonConvert.DeserializeObject <List <Models.ServiceDescriptionIntermed> >(allServicesJSONFormat); //Search query in the deserialized list and fetch it in an IEnumerable list var fetch = from serviceDescription in serviceDescriptionsList //where servicedescrition name = query where serviceDescription.name.Contains(query) select serviceDescription; //To list converts to a list format, assign to the model class list(this can only be done as the list is IEnumerable) serviceDescriptionsList = fetch.ToList(); //has to be assigned to ServiceDescriptionNStatusInterMed as the return type needs both the status and the list descriptionNStatusInterMed.Services = serviceDescriptionsList; return(descriptionNStatusInterMed); } else //There are no services as no json file so return no services status { descriptionNStatusInterMed.Status = "No services!"; descriptionNStatusInterMed.Reason = "No JSON file!"; return(descriptionNStatusInterMed); } } //if token not validated else if (tokenStatus.Equals("not validated")) { descriptionNStatusInterMed.Reason = "Authentication Error"; descriptionNStatusInterMed.Status = "Denied"; return(descriptionNStatusInterMed); } //login error in the authenticator class else { descriptionNStatusInterMed.Reason = "Logic Error"; descriptionNStatusInterMed.Status = "Terminated"; return(descriptionNStatusInterMed); } }