private static async Task <GenericJsonOdata <T> > CallOdataEndpointAsync <T>(string requestUri) { HttpClient client = new HttpClient(); string token = Authenticator.GetAuthData(Common.ErpTasks.ErpTaskStep.AuthenticationType.D365).AuthToken; client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token); client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); client.Timeout = new TimeSpan(0, 3, 0); var result = new GenericJsonOdata <T>(); try { using (var response = await client.GetAsync(requestUri)) { var responseString = await response.Content.ReadAsStringAsync(); if (response.IsSuccessStatusCode) { result = JsonConvert.DeserializeObject <GenericJsonOdata <T> >(responseString); return(result); } else { return(new GenericJsonOdata <T> { Exception = new AxBaseException { ApplicationException = new ApplicationException(responseString) } }); } } } catch (ArgumentNullException ne) { return(new GenericJsonOdata <T> { Exception = new AxBaseException { ApplicationException = ne } }); } catch (TaskCanceledException) { return(new GenericJsonOdata <T> { Exception = new AxBaseException { ApplicationException = new ApplicationException("Timeout Expired for " + requestUri) } }); } catch (Exception e) { return(new GenericJsonOdata <T> { Exception = new AxBaseException { ApplicationException = e } }); } }
public static async Task <AxBaseException> CallServiceByDate <T>(DateTime date, int actionId, string webMethod, string serviceName, string dbTable, Func <DateTime, DateTime> nextPeriod = null) { if (nextPeriod == null) { nextPeriod = AddDay; } DateTime startTime = DateTime.Now; try { GenericJsonOdata <T> result = new GenericJsonOdata <T>(); bool firstResult = false; for (DateTime d = date.Date; d < DateTime.Now.Date && result.Exception == null; d = nextPeriod(d)) { result = await WriteFromService <T>(0, 10000, webMethod, serviceName, d, nextPeriod(d), true); if (!firstResult && result.value.Any()) { DataWriter.TruncateSingleTable(dbTable); firstResult = true; } DataWriter.WriteToTable <T>(result.value.GetDataReader(), dbTable); } if (result.Exception == null) { DataWriter.LogErpActionStep(actionId, dbTable, startTime, true, null, null); } else { DataWriter.LogErpActionStep(actionId, dbTable, startTime, false, result.Exception.ErrorMessage, result.Exception.StackTrace); } return(result.Exception); } catch (Exception e) { DataWriter.LogErpActionStep(actionId, dbTable, startTime, false, e.Message, e.StackTrace); return(new AxBaseException { ApplicationException = e }); } }
public static async Task <AxBaseException> CallService <T>(int actionId, string webMethod, string serviceName, string dbTable, int pageSize) { DateTime startTime = DateTime.Now; try { long recId = 0; GenericJsonOdata <T> result = new GenericJsonOdata <T>(); result = await WriteFromService <T>(recId, pageSize, webMethod, serviceName, DateTime.MinValue, DateTime.MinValue, false); if (result.value.Any()) { DataWriter.TruncateSingleTable(dbTable); } while (result.value.Any() && result.Exception == null) { result = await WriteFromService <T>(recId, pageSize, webMethod, serviceName, DateTime.MinValue, DateTime.MinValue, false); DataWriter.WriteToTable <T>(result.value.GetDataReader(), dbTable); recId = DataWriter.GetMaxRecId(dbTable); } if (result.Exception == null) { DataWriter.LogErpActionStep(actionId, dbTable, startTime, true, null, null); } else { DataWriter.LogErpActionStep(actionId, dbTable, startTime, false, result.Exception.ErrorMessage, result.Exception.StackTrace); } return(result.Exception); } catch (Exception e) { DataWriter.LogErpActionStep(actionId, dbTable, startTime, false, e.Message, e.StackTrace); return(new AxBaseException { ApplicationException = e }); } }
//private static async Task<GenericJsonOdata<T>> CallOdataEndpoint<T>(string requestUri) //{ // var request =(HttpWebRequest)HttpWebRequest.Create(requestUri); // request.Accept = "application/json;odata.metadata=none"; // string token = Authenticator.GetAdalHeader(); // request.Headers["Authorization"] = token; // request.Method = "GET"; // request.Timeout = 1; // //request.Timeout = 1000 * 60 * 2; // we set it to 2 minutes. // var result = new GenericJsonOdata<T>(); // try // { // using (var response = (HttpWebResponse)(await request.GetResponseAsync())) // { // using (var responseStream = response.GetResponseStream()) // { // using (var streamReader = new StreamReader(responseStream)) // { // var responseString = streamReader.ReadToEnd(); // //string sanitized = SanitizeJsonString(responseString); // result = JsonConvert.DeserializeObject<GenericJsonOdata<T>>(responseString); // return result; // } // } // } // } // catch(WebException e) // { // using (var rStream = e.Response.GetResponseStream()) // { // using (var reader = new StreamReader(rStream)) // { // result.Exception = JsonConvert.DeserializeObject<AxBaseException>(reader.ReadToEnd()); // // TODO: Need to log error; // return result; // } // } // } //} private static async Task <GenericJsonOdata <T> > CallAGRServiceArray <T>(string service, string serviceMethod, string postData, string serviceGroup) { var baseUrl = System.Configuration.ConfigurationManager.AppSettings["base_url"]; serviceGroup = serviceGroup ?? System.Configuration.ConfigurationManager.AppSettings["StandardServiceGroup"]; var endpoint = baseUrl + "/api/services/" + serviceGroup + "/" + service + "/" + serviceMethod + ApplyCrossCompanyFilter(""); HttpClient client = new HttpClient(); string token = Authenticator.GetAuthData(Common.ErpTasks.ErpTaskStep.AuthenticationType.D365).AuthToken; client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token); client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); client.Timeout = new TimeSpan(0, 3, 0); System.Diagnostics.Debug.Write("Endpoint :" + endpoint + " "); System.Diagnostics.Debug.WriteLine("postData: " + postData); var result = new GenericJsonOdata <T>(); try { using (var response = await client.PostAsync(endpoint, new StringContent(postData))) { var responseString = await response.Content.ReadAsStringAsync(); if (response.IsSuccessStatusCode) { result.value = JsonConvert.DeserializeObject <List <T> >(responseString); return(result); } else { return(new GenericJsonOdata <T> { Exception = new AxBaseException { ApplicationException = new ApplicationException(responseString) } }); } } } catch (ArgumentNullException ne) { return(new GenericJsonOdata <T> { Exception = new AxBaseException { ApplicationException = ne } }); } catch (TaskCanceledException) { return(new GenericJsonOdata <T> { Exception = new AxBaseException { ApplicationException = new ApplicationException("Timeout Expired for " + endpoint) } }); } catch (Exception e) { return(new GenericJsonOdata <T> { Exception = new AxBaseException { ApplicationException = e } }); } }