Ejemplo n.º 1
0
        public ViewerApiClient(Credentials credentials, string BaseAddress, delActivity messageCalBack = null)
        {
            Activity = messageCalBack;


            ServerBaseAddress = BaseAddress;


            _credentials = credentials;

            ClientResult <HttpClient> result = NewNetClient();

            if (result.RequestStatus == RequestStatus.OK)
            {
                Client = result.ObjectValue;
                Status = RequestStatus.OK;
            }

            else

            {
                Status = result.RequestStatus;
            }
        }
Ejemplo n.º 2
0
        public ClientResult <T> getServerResponse <T>(string EndPointURL, int Attempt = 0)
        {
            string EndpointName = EndPointURL;
            int    n            = EndPointURL.IndexOf('?');

            if (n != -1)
            {
                EndpointName = EndPointURL.Substring(0, n);
            }


            if (Token == null || Token.RequestStatus != RequestStatus.OK)
            {
                Activity?.Invoke(EndpointName + " - There is no token");
                Token = GetToken(_credentials);

                if (Token.RequestStatus != RequestStatus.OK)
                {
                    return(new ClientResult <T>(Token.RequestStatus, Token.Message));
                }
            }

            try
            {
                HttpResponseMessage m = Client.GetAsync(EndPointURL).Result;

                string JsonPayload = m.Content.ReadAsStringAsync().Result;


                Activity?.Invoke(JsonPayload);

                if (m.IsSuccessStatusCode)
                {
                    T data = JsonConvert.DeserializeObject <T>(JsonPayload);

                    Activity?.Invoke(EndpointName + " - Data retrieved successfully");

                    return(new ClientResult <T>(RequestStatus.OK, data, JsonPayload));
                }

                else
                {
                    if (m.StatusCode == HttpStatusCode.Unauthorized)
                    {
                        if (m.ReasonPhrase == "NotFound" ||
                            m.ReasonPhrase == "Expired" ||
                            m.ReasonPhrase == "UnexpectedIp")
                        {
                            Token = GetToken(_credentials);

                            if (Token.RequestStatus == RequestStatus.OK)
                            {
                                if (Attempt < 4)
                                {
                                    Activity?.Invoke(EndpointName + " - Attempt number " + Attempt.ToString());
                                    ClientResult <T> result = getServerResponse <T>(EndPointURL, ++Attempt);
                                    return(result);
                                }

                                else
                                {
                                    Activity?.Invoke(EndpointName + " - Max attempts exceeded, exiting with error");
                                    return(new ClientResult <T>(RequestStatus.TooManyTries, ""));
                                }
                            }
                            else
                            {
                                return(new ClientResult <T>(Token.RequestStatus, Token.Message));
                            }
                        }
                        Activity?.Invoke(EndpointName + " - UNAUTHORIZED : " + m.ReasonPhrase);
                        return(new ClientResult <T>(RequestStatus.Unathorized, m.ReasonPhrase));
                    }

                    else
                    {
                        Activity?.Invoke(EndpointName + " - API error Server said: " + m.StatusCode.ToString() + "...and added: " + m.ReasonPhrase);
                        return(new ClientResult <T>(RequestStatus.Other, "API error - Server said: " + m.StatusCode.ToString() + "...and added: " + m.ReasonPhrase));
                    }
                }
            }

            catch (Exception ex)
            {
                Activity?.Invoke(EndpointName + " - Client error: " + GetErrMsgPath(ex));
                return(new ClientResult <T>(RequestStatus.ClientCodeError, ex.Message));
            }
        }