private async Task <T> DoRequestAsync <T>(IVPNRestRequest <T> req, CancellationToken cancellationToken, int timeoutMs = Consts.DefaultTimeout) { try { __HostIPs.GetHostsInfo(out IPAddress alternateHost, out List <IPAddress> alternateHostIPs); // do API call HttpResponseEx ret = await req.RequestAsync(alternateHostIPs, alternateHost, cancellationToken, timeoutMs); var httpResp = ret.HttpResponse; // deserialize response // // Here we are not checking HTTP code from response. // There is a chance to receive API response with HTTPRetCode != OK (200). 'ResponseWithStatus' object T respObj; try { respObj = JsonConvert.DeserializeObject <T>(httpResp.Data); } catch (Exception ex) { Logging.Info($"[ERROR] (API) Response deserialization error: {ex.Message} (HTTP={(int)httpResp.HttpRetCode}-{httpResp.HttpRetCode})"); throw new IVPNRestRequestApiException(httpResp.HttpRetCode); } //if (respObj == default) if (respObj == null || respObj.Equals(default))
/// <summary> /// Downloads the specified file in <paramref name="info"/>. /// </summary> /// <param name="target">The target.</param> /// <param name="info">The extended response information.</param> private static void DownloadImpl(this Stream target, HttpResponseEx info) { target.Position = 0; info.Response.Clear(); info.Response.ContentType = info.ContentType; info.Response.AddHeader("content-disposition", info.ContentDisposition); info.Response.BinaryWrite(target.AsByteArray()); info.Response.Flush(); info.Response.End(); }
/// <summary> /// Downloads the specified data with the name specified in <paramref name="fileName"/>. /// </summary> /// <param name="target">Data to download.</param> /// <param name="fileName">Name of the file to download.</param> /// <param name="response">The response to use.</param> public static void Download(this Stream target, string fileName, HttpResponse response) { SentinelHelper.ArgumentNull(target); SentinelHelper.ArgumentNull(fileName); //SentinelHelper.IsFalse(FileHelper.IsValidFileName(fileName), "Filename does not have a valid name"); HttpResponseEx info = new HttpResponseEx { Response = response, ContentType = HttpResponseEx.GetMimeMapping(Path.GetExtension(fileName).Replace(".", string.Empty)), ContentDisposition = $"attachment; filename={fileName}" }; target.DownloadImpl(info); }