/// <summary> /// Downloads a given url and return a path to its local version. /// Files are cached.Second call for the same URL will return cached version. /// </summary> /// <param name="url">URL to download</param> /// <returns>Absolute path to the local downloaded version of file</returns> public static FileStreamInfo GetFile(string url) { string originalFileName = Path.GetFileName(url); string filename = "sdk_tests" + toSHA1(url) + ".tmp"; string tmpPath = Path.GetTempPath(); string filePath = Path.Combine(tmpPath, filename); FileInfo fileInfo = new FileInfo(filePath); FileStream fileStream = null; // if file does not exist locally, download it if (!fileInfo.Exists) { IHttpClient client = ControllerTestBase.GetClient().SharedHttpClient; HttpRequest req = client.Get(url, null, null); req.Headers = new Dictionary <string, string> { { "user-agent", "APIMATIC 2.0" } }; HttpResponse resp = client.ExecuteAsBinary(req); fileStream = System.IO.File.Create(filePath); byte[] buffer = new byte[2048]; int len = resp.RawBody.Read(buffer, 0, 2048); while (len > 0) { fileStream.Write(buffer, 0, len); len = resp.RawBody.Read(buffer, 0, 2048); } fileStream.Position = 0; } else { fileStream = System.IO.File.OpenRead(filePath); } return(new FileStreamInfo(fileStream, originalFileName)); }
/// <summary> /// Downloads a given url and return a path to its local version. /// Files are cached.Second call for the same URL will return cached version. /// </summary> /// <param name="url">URL to download</param> /// <returns>Absolute path to the local downloaded version of file</returns> public static FileStreamInfo GetFile(string url) { var originalFileName = Path.GetFileName(url); var filename = "sdk_tests" + ToSHA1(url) + ".tmp"; var tmpPath = Path.GetTempPath(); var filePath = Path.Combine(tmpPath, filename); var fileInfo = new FileInfo(filePath); FileStream fileStream = null; // if file does not exist locally, download it if (!fileInfo.Exists) { var client = ControllerTestBase.GetClient().SharedHTTPClient; var req = client.Get(url, null); req.Headers = new Dictionary <string, string> { { "user-agent", "Starling Bank C# Client" } }; var resp = client.ExecuteAsBinary(req); fileStream = File.Create(filePath); var buffer = new byte[2048]; var len = resp.RawBody.Read(buffer, 0, 2048); while (len > 0) { fileStream.Write(buffer, 0, len); len = resp.RawBody.Read(buffer, 0, 2048); } fileStream.Position = 0; } else { fileStream = File.OpenRead(filePath); } return(new FileStreamInfo(fileStream, originalFileName)); }