0

I have an GUI application which is a xamarin forms application. The application talks to a python Flask service which has several endpoints defined in it.

For example, it the GUI wants to get a list of users, it will make a HTTPS request on the uir ("http://127.0.0.1:5000/MyCompany/employeelist/") and the Flask service has an endpoint defined which services this GET request.


    Client Code in Xamarin Forms: (Currently running the UWP version)

    Task.Run(async () => await GetFromServerAsync(uri)); // Code executes this line 

    public class TokenStructure 
        {
            private string token;
            public string Token   // This is your property
            {
                get => token;
                set => token = value;
            }

            private string oneTime;
            public string OneTime   // This is your property
            {
                get => oneTime;
                set => oneTime = value;
            }
        }

    // This part of code gets the Token from a service which the GUI application //will store.
    public async Task<string> GetFromServerAsync(Uri uri)
            {
                Windows.Web.Http.HttpClient client = PreparedClientAsync();
                Windows.Web.Http.HttpResponseMessage response;

                try
                {
                    var content = new HttpStringContent ( JsonConvert.SerializeObject(new { UserName = Username, Password = Password, MacAddress = MacAddress }), UnicodeEncoding.Utf8, "application/json");

                    response = await client.PutAsync(uri, content);
                    if (response.IsSuccessStatusCode)
                    {
                        var result = await response.Content.ReadAsStringAsync();
                        TokenString = JsonConvert.DeserializeObject<TokenStructure>(result).Token;
                        return TokenString;
                    }
                    else
                        return "Unauthorized access";
                }
                catch (Exception ex)
                {
                    return "PUT operation failed.";
                }
            }

            private Windows.Web.Http.HttpClient PreparedClientAsync()
            {
                var filter = new Windows.Web.Http.Filters.HttpBaseProtocolFilter();

                filter.IgnorableServerCertificateErrors.Add(ChainValidationResult.Expired);
                filter.IgnorableServerCertificateErrors.Add(ChainValidationResult.Untrusted);
                filter.IgnorableServerCertificateErrors.Add(ChainValidationResult.InvalidName);

                Windows.Web.Http.HttpClient client = new Windows.Web.Http.HttpClient(filter);

                return client;
            }

// In another page in the application:
// In this part, the GUI application makes a call on the following uri to get //information from the Flask service. This endpoint calls the ```get``` in the //FlaskService code below.

string endpoint = "http://127.0.0.1:5000/MyCompany/employeelist/";
information = await information.GetStatusInformation(endpoint);

    public async Task<StateClass> GetStatusInformation(string uri)
            {
                FSTDStateInformation jsonData = null;
                try
                {
                    HttpResponseMessage response = await httpClient.GetAsync(uri);
                    if (response.IsSuccessStatusCode)
                    {
                        string JsonContent = await response.Content.ReadAsStringAsync();

                 jsonData = JsonConvert.DeserializeObject<StateClass>(JsonContent);
                    }
                }
                catch (Exception ex)
                {
                    Debug.WriteLine("\tERROR {0}", ex.Message);
                }
            }


    # Excerpt of the Flask service code
    api.add_resource(
        MyEmployees,
        "/<string:CompanyName>/employeelist",
        endpoint="/employeelist")

    class MyEmployees(Resource):
        def get(self, CompanyName):
           # Code goes here

The issue I am having is the following.

  1. From my GUI application, I want to be able to send also a token value with my ("http://127.0.0.1:5000/MyCompany/employeelist/) and the Flask Service will ONLY ALLOW me to service the GET command if the token value is valid.

I do not understand how to send the token value as a header from the GUI to the Flask Service. I do not want to append it to every single uri in the GUI as I could have many more uri and appending it to every single uri will not be an efficient solution.

Is there any "clean" and logical way to do it? I am really new to flask service.

Thanks to All.

Anna
  • 181
  • 1
  • 12

0 Answers0