Example #1
0
        public async Task <ServiceReaponseHeader> MakeServiceCall(ServiceRequest serviceRequest, CancellationToken cancellationToken)
        {
            ServiceReaponseHeader serviceReaponseHeader = new ServiceReaponseHeader();

            try
            {
                if (Xamarin.Essentials.Connectivity.NetworkAccess == Xamarin.Essentials.NetworkAccess.Internet)
                {
                    if (!string.IsNullOrEmpty(serviceRequest.AuthToken))
                    {
                        Client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", serviceRequest.AuthToken);
                    }
                    HttpResponseMessage response = null;
                    Debug.WriteLine("Api Call Started: " + serviceRequest.RequestUrl + " " + DateTime.Now.TimeOfDay);
                    switch (serviceRequest.RequestMethod)
                    {
                    case RequestMethodTypes.Get:
                        response = await Client.GetAsync(serviceRequest.RequestUrl, cancellationToken);

                        break;

                    case RequestMethodTypes.Post:
                        response = await Client.PostAsync(serviceRequest.RequestUrl, serviceRequest.Content, cancellationToken);

                        break;

                    case RequestMethodTypes.Put:
                        response = await Client.PutAsync(serviceRequest.RequestUrl, serviceRequest.Content, cancellationToken);

                        break;

                    case RequestMethodTypes.Delete:
                        response = await Client.DeleteAsync(serviceRequest.RequestUrl, cancellationToken);

                        break;
                    }
                    Debug.WriteLine("Api Call Ended: " + serviceRequest.RequestUrl + " " + DateTime.Now.TimeOfDay);
                    serviceReaponseHeader.StatusCode = response.StatusCode;
                    serviceReaponseHeader.Response   = await response.Content.ReadAsStringAsync();
                }
                else
                {
                    serviceReaponseHeader.ErrorException = new WebException("No internet connection.");
                }
            }
            catch (Exception ex)
            {
                serviceReaponseHeader.ErrorException = ex;
            }

            return(serviceReaponseHeader);
        }
Example #2
0
        public async Task <(ServiceReaponseHeader ServiceReaponseHeader, LoginResponseModel Result)> Login(LoginRequestModel loginRequest, CancellationToken cancellationToken)
        {
            ServiceReaponseHeader serviceReaponseHeader = new ServiceReaponseHeader();
            LoginResponseModel    result         = new LoginResponseModel();
            RestApiUtils          restApiUtils   = new RestApiUtils(Constants.BaseUrl);
            ServiceRequest        serviceRequest = new ServiceRequest()
            {
                Content       = new StringContent(loginRequest.Serialize(), Encoding.UTF8, "application/json"),
                RequestMethod = RequestMethodTypes.Post,
                RequestUrl    = Constants.LoginUrl,
            };

            serviceReaponseHeader = await restApiUtils.MakeServiceCall(serviceRequest, cancellationToken);

            try
            {
                if (serviceReaponseHeader.StatusCode == HttpStatusCode.OK)
                {
                    result                = JsonConvert.DeserializeObject <LoginResponseModel>(serviceReaponseHeader.Response);
                    result.IsSuccess      = true;
                    result.ErrorEcxeption = null;
                }
                else if (serviceReaponseHeader.StatusCode == HttpStatusCode.BadRequest)
                {
                    var errorData = JsonConvert.DeserializeObject <CommonResponseModel>(serviceReaponseHeader.Response);
                    result.ErrorMessage   = errorData.Message;
                    result.IsSuccess      = false;
                    result.ErrorEcxeption = null;
                }
                else
                {
                    result.IsSuccess      = false;
                    result.ErrorMessage   = "Something went wrong.";
                    result.ErrorEcxeption = null;
                }
            }
            catch (Exception ex)
            {
                result.IsSuccess      = false;
                result.ErrorEcxeption = ex;
            }
            return(ServiceReaponseHeader : serviceReaponseHeader, Result : result);
        }