コード例 #1
0
ファイル: RestClient.cs プロジェクト: Hat-Factory/PTHippo
        public async Task <string> LoginAsync(string userName, string password, AccountIdApi account, string url, List <KeyValuePair <string, string> > KeyValues)
        {
            try
            {
                await PreAPICall();

                var request = new HttpRequestMessage(HttpMethod.Post, $"{constants.serverUrl}/{url}");

                request.Content = new FormUrlEncodedContent(KeyValues);
                var respons = await httpClient.SendAsync(request);

                var jwt = await respons.Content.ReadAsStringAsync();

                JObject jwtDynamic  = JsonConvert.DeserializeObject <dynamic>(jwt);
                var     accessToken = jwtDynamic.Value <string>("access_token");
                Debug.WriteLine(jwt);
                AfterAPICall();
                return(accessToken);
            }
            catch (Exception ex)
            {
                var logged = new LoggedException($"Error while trying to call login api {url}", ex);
                await logged.LoggAPI();
            }
            return(null);
        }
コード例 #2
0
ファイル: RestClient.cs プロジェクト: Hat-Factory/PTHippo
        public async Task <bool> DeleteAsync(string id, string url)
        {
            try
            {
                await PreAPICall();

                httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", Pt_Hippo_Mobile.Helpers.Settings.AccessToken);
                var response = await httpClient.DeleteAsync($"{constants.serverUrl}/{url}/{id}");

                AfterAPICall();
                if (response.IsSuccessStatusCode == false)
                {
                    await ValidateErrors(response);

                    return(false);
                }
                return(response.IsSuccessStatusCode);
            }
            catch (Exception ex)
            {
                var logged = new LoggedException($"Error while trying to call deleteasync api {url}", ex);
                await logged.LoggAPI();
            }
            return(false);
        }
コード例 #3
0
ファイル: RestClient.cs プロジェクト: Hat-Factory/PTHippo
        public async Task <T> GetClientId(string url)
        {
            try
            {
                await PreAPICall();

                HttpResponseMessage response = await httpClient.GetAsync($"{constants.serverUrl}/{url}");

                var stream = await response.Content.ReadAsStreamAsync();

                StreamReader readStream = new StreamReader(stream, Encoding.UTF8);
                var          json       = readStream.ReadToEnd();
                var          account    = JsonConvert.DeserializeObject <T>(json);
                AfterAPICall();
                if (response.IsSuccessStatusCode == false)
                {
                    await ValidateErrors(response);

                    return(default(T));
                }
                return(account);
            }
            catch (Exception ex)
            {
                var logged = new LoggedException($"Error while trying to call getclientid api {url}", ex);
                await logged.LoggAPI();
            }
            return(default(T));
        }
コード例 #4
0
ファイル: RestClient.cs プロジェクト: Hat-Factory/PTHippo
        //update List Async
        public async Task <bool> PutListAsync(List <T> data, string api)
        {
            try
            {
                await PreAPICall();

                httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", Pt_Hippo_Mobile.Helpers.Settings.AccessToken);
                var         json        = JsonConvert.SerializeObject(data);
                HttpContent httpContent = new StringContent(json);
                httpContent.Headers.ContentType = new MediaTypeHeaderValue("application/json");
                var result = await httpClient.PutAsync($"{constants.serverUrl}/{api}", httpContent);

                AfterAPICall();
                if (result.IsSuccessStatusCode == false)
                {
                    await ValidateErrors(result);

                    return(false);
                }
                return(result.IsSuccessStatusCode);
            }
            catch (Exception ex)
            {
                var logged = new LoggedException($"Error while trying to call putlist api {api}", ex);
                await logged.LoggAPI();
            }
            return(false);
        }
コード例 #5
0
ファイル: RestClient.cs プロジェクト: Hat-Factory/PTHippo
        public async Task <List <T> > GetListOptionalId(string api, string id, bool authorize = true)
        {
            try
            {
                await PreAPICall();

                if (authorize)
                {
                    httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", Pt_Hippo_Mobile.Helpers.Settings.AccessToken);
                }
                string url = id == null ? $"{constants.serverUrl}/{api}" : $"{constants.serverUrl}/{api}/" + id;

                var json = await httpClient.GetStringAsync(url);

                var taskModels = JsonConvert.DeserializeObject <List <T> >(json);
                AfterAPICall();
                return(taskModels);
            }
            catch (Exception ex)
            {
                var logged = new LoggedException($"Error while trying to call  api {api}", ex);
                await logged.LoggAPI();
            }
            return(null);
        }
コード例 #6
0
ファイル: RestClient.cs プロジェクト: Hat-Factory/PTHippo
        public async Task <T> GetAsync(string api, string id)
        {
            try
            {
                await PreAPICall();

                httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", Pt_Hippo_Mobile.Helpers.Settings.AccessToken);

                string url = id == null ? $"{constants.serverUrl}/{api}" : $"{constants.serverUrl}/{api}/" + id;

                var result = await httpClient.GetAsync(url);

                if (result.IsSuccessStatusCode == false)
                {
                    AfterAPICall();
                    await ValidateErrors(result);

                    return(default(T));
                }

                var json = await result.Content.ReadAsStringAsync();

                var taskModels = JsonConvert.DeserializeObject <T>(json);

                AfterAPICall();
                return(taskModels);
            }
            catch (Exception ex)
            {
                var logged = new LoggedException($"Error while trying to call getasync api {api}", ex);
                await logged.LoggAPI();
            }
            return(default(T));
        }
コード例 #7
0
ファイル: RestClient.cs プロジェクト: Hat-Factory/PTHippo
        public async Task <T> GetSearch(string api, string searchtext, int pagesize, int pagenumber, string zipcode, DateTime startdate, DateTime enddate, double minhourrate, double maxhourate)
        {
            try
            {
                await PreAPICall();

                httpClient.DefaultRequestHeaders.Authorization =
                    new AuthenticationHeaderValue("Bearer", Pt_Hippo_Mobile.Helpers.Settings.AccessToken);
                //string json = "";
                httpClient.DefaultRequestHeaders.Authorization =
                    new AuthenticationHeaderValue("Bearer", Pt_Hippo_Mobile.Helpers.Settings.AccessToken);
                //string json = "";
                //GET /api/job/getBasicSearchForJobs/{pageSize}/{pageNumber}/{zipCode}/{startDate}/{endDate}/{minHourRate}/{maxHourRate}
                string startDateText = "null";
                string endDateText   = "null";
                if (startdate != DateTime.MinValue)
                {
                    startDateText = startdate.Date.ToString("MM-dd-yyyy");
                }

                if (enddate != DateTime.MinValue)
                {
                    endDateText = enddate.Date.ToString("MM-dd-yyyy");
                }

                string url =
                    $"{constants.serverUrl}/{api}/{pagesize}/{pagenumber}/{zipcode}/{startDateText}/{endDateText}/{minhourrate}/{maxhourate}";
                var json = await httpClient.GetStringAsync(url);

                var taskModels = JsonConvert.DeserializeObject <T>(json);
                AfterAPICall();
                return(taskModels);
            }
            catch (Exception ex)
            {
                var logged = new LoggedException($"Error while trying to call getsearchapi api {api}", ex);
                await logged.LoggAPI();
            }

            return(default(T));
        }
コード例 #8
0
ファイル: RestClient.cs プロジェクト: Hat-Factory/PTHippo
        public async Task <T> Post(string api, object data)
        {
            try
            {
                await PreAPICall();

                httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", Pt_Hippo_Mobile.Helpers.Settings.AccessToken);

                var         json        = JsonConvert.SerializeObject(data);
                HttpContent httpContent = new StringContent(json);
                httpContent.Headers.ContentType = new MediaTypeHeaderValue("application/json");
                string url    = $"{constants.serverUrl}/{api}";
                var    result = await httpClient.PostAsync(url, httpContent);

                var Data = await result.Content.ReadAsStringAsync();

                if (api == URLConfig.MobileLogging)
                {
                    AfterAPICall();
                    return(default(T));
                }

                if (result.IsSuccessStatusCode == false)
                {
                    AfterAPICall();
                    await ValidateErrors(result);

                    return(default(T));
                }

                var taskModels = JsonConvert.DeserializeObject <T>(Data);
                AfterAPICall();
                return(taskModels);
            }
            catch (Exception ex)
            {
                var logged = new LoggedException($"Error while trying to call postdata api {api}", ex);
                await logged.LoggAPI();
            }
            return(default(T));
        }
コード例 #9
0
ファイル: RestClient.cs プロジェクト: Hat-Factory/PTHippo
        public async Task <List <T> > UploadMediafile(string path, string docuemntId, bool IsProfilePic, DocumentType documentType, bool IsEmployee, byte[] bytearray, string fileName, string url, string Title = null)
        {
            string response = string.Empty;

            try
            {
                await PreAPICall();

                if (!CrossConnectivity.Current.IsConnected)
                {
                    await App.Current.MainPage.DisplayAlert("you have trouble connecting the internet", AlertMessages.internetconnection, AlertMessages.OkayTitle);

                    return(null);
                }
                var client = new HttpClient();
                using (var content = new MultipartFormDataContent())
                {
                    if (path != null && bytearray != null)
                    {
                        content.Add(new StreamContent(new MemoryStream(bytearray)), Path.GetDirectoryName(path), fileName);

                        // content.Add(new StreamContent(new MemoryStream(bytearray)), fileName, fileName);
                    }

                    content.Add(new StringContent("true"), "IsEmployee");
                    if (string.IsNullOrEmpty(Title) == false)
                    {
                        content.Add(new StringContent(Title), "Title");
                    }
                    content.Add(new StringContent(IsProfilePic == true ? "true" : "false"), "IsProfilePic");
                    if (string.IsNullOrEmpty(docuemntId) == false)
                    {
                        content.Add(new StringContent(docuemntId), "DocumentId");
                    }
                    content.Add(new StringContent(((int)documentType).ToString()), "DocumentType");
                    client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", Pt_Hippo_Mobile.Helpers.Settings.AccessToken);
                    var httpResponseMessage = await client.PostAsync($"{constants.serverUrl}/{url}", content);

                    response = await httpResponseMessage.Content.ReadAsStringAsync();

                    var imageinfo = JsonConvert.DeserializeObject <List <T> >(response);
                    AfterAPICall();
                    await ValidateErrors(httpResponseMessage);

                    if (httpResponseMessage.IsSuccessStatusCode == false)
                    {
                        await ValidateErrors(httpResponseMessage);

                        return(null);
                    }
                    return(imageinfo);
                }
            }

            catch (Exception ex)
            {
                var logged = new LoggedException($"Error while trying to call uploadfiledata api {url} with response {response}", ex);
                await logged.LoggAPI();
            }
            return(null);
        }