/*
         * This function fetches any reponse invoked by the client -> Addtion or Multipcation or The generate prime numbers function
         *
         * The ServiceProviderIntermed returns are of two types -> string (invoke of the addition or multipication services will return a single string)
         *                                                      -> List<string> (invocation of generation of prime number will have many values)
         *
         *        To handle this a try catch block has been implemented in the concatMessage() static function.
         *
         *
         * @param APIEndPoint
         * @returns ServiceProviderIntermed
         *
         *      References : Lab 3
         *      Serialise and de-serialize : https://stackoverflow.com/questions/13297563/read-and-parse-a-json-file-in-c-sharp
         *                                   RestSharp
         *
         */


        public ServiceProviderIntermed response(string APIEndPoint)
        {
            ServiceProviderIntermed intermed    = new ServiceProviderIntermed();
            RestRequest             restRequest = new RestRequest(APIEndPoint);

            //Invoke get request with the given endpoint
            IRestResponse response = restClient.Get(restRequest);

            //De-serialize json return
            intermed = JsonConvert.DeserializeObject <ServiceProviderIntermed>(response.Content);

            return(intermed);
        }
Beispiel #2
0
        private void TestService_Click(object sender, RoutedEventArgs e)
        {
            string messageBoxReply = "----Test----\n";

            string variables = "";

            try
            {
                int numberOfOperands = 0;
                numberOfOperands = int.Parse(selectedService.numberOfOperands);

                if (selectedService == null)
                {
                    MessageBox.Show("You have not selected a service to test");
                }
                else
                {
                    List <TextBox> textBoxes = new List <TextBox>();

                    //Creating textboxes dynamically based on the numberOfOperands in the selectedService
                    for (int x = 0; x < numberOfOperands; x++)
                    {
                        //Finding the registered name for the TextBox
                        TextBox txtNumber1 = (TextBox)this.stackPanelName.FindName($"no{x}");
                        //Add to a list of textBoxes
                        textBoxes.Add(txtNumber1);

                        //Remove the dynamically created textbox
                        this.stackPanelName.Children.Remove(txtNumber1);

                        //Unregister name in stack panel
                        stackPanelName.UnregisterName($"no{x}");
                    }

                    string serviceEndPoint = selectedService.serviceEndPoint;

                    //concatinating all the input values so that it will come in the form -> /1/2/3/ or so
                    foreach (TextBox textBox in textBoxes)
                    {
                        variables = variables + "/" + textBox.Text;
                    }

                    //serviceEndPoint = "localhost:8090/ADDTwoNumbers" + "/2/3/4" + "/" + "{token}"
                    serviceEndPoint = serviceEndPoint + variables + "/" + TOKEN;

                    //MessageBox.Show(serviceEndPoint);

                    //Put the endpoint to accessObject of the Service Provider and fetch the object
                    intermed = accessObject.response(serviceEndPoint);

                    //Get a concatMessage with all the values
                    messageBoxReply = ServiceProviderAccessObject.concatMessage(messageBoxReply, intermed);

                    //Display the service in a MessageBox
                    MessageBox.Show(messageBoxReply);

                    //Re-initilize if another service is invoked
                    intermed        = null;
                    messageBoxReply = "";
                    serviceEndPoint = "";
                }
            }
            catch (NullReferenceException)
            {
                MessageBox.Show("You have not selected a service to test");
            }
        }
        /*
         * This function will concat all the values and status information to be dispayed on a MessageBox to the user
         * @param string message, ServiceProvider intermed object to concat
         *
         * In this function it is intially assumed that the function will not generate a System.ArgumentNullException
         * If generated will be handled in the catch block where it is assumed that the Exception was caused as there is a single return (multiply, substraction function)
         *
         * @return concat string
         *
         */
        public static string concatMessage(string message, ServiceProviderIntermed intermed)
        {
            try
            {
                //intermed.Values will be null if invoke a single string return service
                //It may which will generate a System.ArgumentNullException
                if (intermed.Values.Count() > 0)
                {
                    message = "Values : ";
                    //foreach value concat to string
                    foreach (String value in intermed.Values)
                    {
                        message = message + $"{value}, ";
                    }

                    if (!String.IsNullOrWhiteSpace(intermed.Status))
                    {
                        message = message + "\n";
                        message = message + "Status : ";
                        message = message + $"{intermed.Status}";
                    }

                    if (!String.IsNullOrWhiteSpace(intermed.Reason))
                    {
                        message = message + "\n";
                        message = message + "Reason : ";
                        message = message + $"{intermed.Reason}";
                    }
                }
            }
            catch (System.ArgumentNullException)
            {
                //System.ArgumentNullException means that it is a single string , reason and status returned

                if (!String.IsNullOrWhiteSpace(intermed.Value))
                {
                    message = $"value is : {intermed.Value} \n";
                    if (!String.IsNullOrWhiteSpace(intermed.Status))
                    {
                        message = message + "\n";
                        message = message + "Status : ";
                        message = message + $"{intermed.Status}";
                    }

                    if (!String.IsNullOrWhiteSpace(intermed.Reason))
                    {
                        message = message + "\n";
                        message = message + "Reason : ";
                        message = message + $"{intermed.Reason}";
                    }
                }
                else //this means the token has not been validated and so have a "Status" and "Reason"
                {
                    if (!String.IsNullOrWhiteSpace(intermed.Status))
                    {
                        message = message + "Status : ";
                        message = message + $"{intermed.Status}";
                    }

                    if (!String.IsNullOrWhiteSpace(intermed.Reason))
                    {
                        message = message + "\n";
                        message = message + "Reason : ";
                        message = message + $"{intermed.Reason}";
                    }
                }
            }

            return(message);
        }