예제 #1
0
        public async static Task <Object> Request(Method requestMethod, string apiAddress, object data)
        {
            string responseContent = null;

            if (!BaseService.InternetConnection())
            {
                return(responseContent);
            }

            var client = new RestClient(apiAddress);

            client.Timeout = -1;
            var request = new RestRequest(requestMethod);

            request.AddHeader("Content-Type", "application/json; charset=utf-8");

            if (data != null)
            {
                request.Parameters.Clear();
                var json = JsonConvert.SerializeObject(data);
                request.AddParameter("application/json", json, ParameterType.RequestBody);
            }

            IRestResponse response = client.Execute(request);

            if (!String.IsNullOrEmpty(response.Content))
            {
                var res = JsonConvert.DeserializeObject <BaseVm>(response.Content);

                if (res.MessageType == (int)MesseageType.Error)
                {
                    Xamarin.Forms.Device.BeginInvokeOnMainThread(async() =>
                    {
                        ToastMessage.ShowToastError(res.Message, ToastLength.Long);
                    });
                }
                if (res.MessageType == (int)MesseageType.Warning)
                {
                    Xamarin.Forms.Device.BeginInvokeOnMainThread(async() =>
                    {
                        ToastMessage.ShowToastWarning(res.Message, ToastLength.Long);
                    });
                }

                return(res.ReturnedObject);
            }
            else
            {
                return(null);
            }
        }