private static void SetEtagHeader(HttpClient httpClient, DownloadUsageDetailsRequest request) { if (!request.ForceRun && !string.IsNullOrWhiteSpace(request.ETagHeaderValue)) { httpClient.DefaultRequestHeaders.IfNoneMatch.Add( new EntityTagHeaderValue(request.ETagHeaderValue)); } }
public async Task <IList <UsageDetailItem> > GetUsageDetailsAsync(DownloadUsageDetailsRequest request) { var httpClient = _httpClientFactory.GetClient(); SetCredentials(httpClient, request); SetEtagHeader(httpClient, request); return(await DownloadUsageDataAsync(httpClient, request).ConfigureAwait(false)); }
static async Task Main(string[] args) { IConfiguration config = new ConfigurationBuilder() .AddJsonFile("appsettings.json", true, true) .Build(); var downloadUsageDetailsRequest = new DownloadUsageDetailsRequest { ForceRun = true, AccessToken = config["accessToken"], BillingPeriod = "201804", EnrollmentNumber = config["enrollmentNumber"] }; var httpClientFactory = new HttpClientFactory(); var usageDetailsDataProvider = new UsageDetailsDataProvider(httpClientFactory); var usageDetails = await usageDetailsDataProvider.GetUsageDetailsAsync(downloadUsageDetailsRequest) .ConfigureAwait(false); Console.ReadLine(); }
private static void SetCredentials(HttpClient httpClient, DownloadUsageDetailsRequest request) { httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", request.AccessToken); }
private static async Task <IList <UsageDetailItem> > DownloadUsageDataAsync(HttpClient httpClient, DownloadUsageDetailsRequest request) { var usageDetails = new List <UsageDetailItem>(); var data = await DownloadUsageDetailsAsync(httpClient, $"{request.EnrollmentNumber}/billingPeriods/{request.BillingPeriod}/usagedetails", request.ForceRun).ConfigureAwait(false); usageDetails.AddRange(data.data); if (data is NotModifiedUsageDetailResponse) { } if (!string.IsNullOrWhiteSpace(data.ETagHeaderValue)) { // save data.ETagHeaderValue; } while (data.MakeNextCall) { data = await DownloadUsageDetailsAsync(httpClient, data.nextLink, request.ForceRun).ConfigureAwait(false); usageDetails.AddRange(data.data); } return(usageDetails); }