public async Task <byte[]> DownloadData(string downloadUrl, IHeaderDictionary customHeaders = null) { var response = await WebRequest.ExecuteRequestAsStreamContent(downloadUrl, HttpMethod.Get, customHeaders); using (var responseStream = await response.ReadAsStreamAsync()) { responseStream.Position = 0; byte[] file = new byte[responseStream.Length]; responseStream.Read(file, 0, file.Length); return(file); } }
private async Task <Stream> RequestAndReturnDataStream(IHeaderDictionary customHeaders, HttpMethod method, string route = null, IList <KeyValuePair <string, string> > queryParameters = null, string payload = null) { var url = await GetUrl(route, customHeaders, queryParameters); // If we are calling to our own services, keep the JWT assertion var strippedHeaders = customHeaders.StripHeaders(IsInsideAuthBoundary); var streamPayload = payload != null ? new MemoryStream(Encoding.UTF8.GetBytes(payload)) : null; var result = await(await WebRequest.ExecuteRequestAsStreamContent(url, method, strippedHeaders, streamPayload)).ReadAsStreamAsync(); return(result); }
protected Task UploadData(string uploadUrl, Stream payload, IHeaderDictionary customHeaders = null) { return(WebRequest.ExecuteRequestAsStreamContent(uploadUrl, HttpMethod.Put, customHeaders, payload)); }
protected override async Task <ContractExecutionResult> ProcessAsyncEx <T>(T item) { if (!(item is SnsPayload payload)) { Logger.LogWarning($"{nameof(TagFileSnsProcessExecutor)} Invalid Request passed in. Expected {typeof(SnsPayload).Name} but got {(item == null ? "null" : item.GetType().Name)}"); return(ContractExecutionResult.ErrorResult("Invalid Request")); } Logger.LogInformation($"{nameof(TagFileSnsProcessExecutor)} Sns message type: {payload.Type}, topic: {payload.TopicArn}"); if (payload.Type == SnsPayload.SubscriptionType) { // Request for subscription Logger.LogInformation($"{nameof(TagFileSnsProcessExecutor)} SNS SUBSCRIPTION REQUEST: {payload.Message}, Subscription URL: '{payload.SubscribeURL}'"); return(new ContractExecutionResult()); } if (payload.IsNotification) { // Got a tag file SnsTagFile tagFile; try { tagFile = JsonConvert.DeserializeObject <SnsTagFile>(payload.Message); } catch (Exception e) { Logger.LogError(e, $"{nameof(TagFileSnsProcessExecutor)} Failed to deserialize SnsTagFile {payload.MessageId}"); tagFile = null; } if (tagFile == null) { // this will cause payload to be deleted from the SQS que Logger.LogWarning($"{nameof(TagFileSnsProcessExecutor)} Could not convert to Tag File Model. JSON: {payload.Message}"); return(new ContractExecutionResult(1, "Failed to parse tag file model")); } byte[] data; if (!string.IsNullOrEmpty(tagFile.DownloadUrl)) { Logger.LogInformation($"{nameof(TagFileSnsProcessExecutor)} Tag file {tagFile.FileName} needs to be downloaded from : {tagFile.DownloadUrl}"); var downloadTagFileData = await WebRequest.ExecuteRequestAsStreamContent(tagFile.DownloadUrl, HttpMethod.Get); await using var ms = new MemoryStream(); await downloadTagFileData.CopyToAsync(ms); data = ms.ToArray(); if (data.Length != tagFile.FileSize) { Logger.LogWarning($"{nameof(TagFileSnsProcessExecutor)} Downloaded data length {data.Length} is not equal to expected length {tagFile.FileSize}"); } Logger.LogInformation($"{nameof(TagFileSnsProcessExecutor)} Downloaded tag file {tagFile.FileName}, total bytes: {data.Length}"); } else { Logger.LogInformation($"{nameof(TagFileSnsProcessExecutor)} Tag file data is included in payload for file {tagFile.FileName}"); data = tagFile.Data; } var request = new CompactionTagFileRequest { Data = data, FileName = tagFile.FileName, OrgId = tagFile.OrgId, OriginSource = tagFile.originSource }; Logger.LogInformation($"{nameof(TagFileSnsProcessExecutor)} Attempting to process sns tag file {tagFile.FileName}"); var executor = Build <TagFileProcessExecutor>(); executor.ArchiveOnInternalError = false; var result = await executor.ProcessAsync(request); Logger.LogInformation($"{nameof(TagFileSnsProcessExecutor)} Got result {JsonConvert.SerializeObject(result)} for Tag file: {tagFile?.FileName}"); return(result); } Logger.LogWarning($"{nameof(TagFileSnsProcessExecutor)} Unknown SNS Type: {payload.Type} - not sure how to process"); return(new ContractExecutionResult(99, "Unknown SNS message")); }