Skip to content

Commit

Permalink
chore(repo): remove format arg from render links as can just use opti…
Browse files Browse the repository at this point in the history
…ons.Format
  • Loading branch information
AJCJ1 committed Dec 20, 2024
1 parent 9ca7d7d commit ec2b65d
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 26 deletions.
6 changes: 3 additions & 3 deletions UrlboxSDK.MsTest/Factory/RenderLinkFactoryTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,12 @@ public void GenerateRenderLink_SignFalse_ShouldReturnUnsignedUrl()
[TestMethod]
public void GenerateRenderLink_WithDiffFormatFormat_ShouldReturnExpectedLink()
{
var options = new UrlboxOptions(url: "https://example.com");
var options = Urlbox.Options(url: "https://example.com").Format(Format.Jpeg).Build();

string expectedQueryString = "url=https%3A%2F%2Fexample.com";
string expectedLink = $"{BaseUrl}/v1/{TestKey}/jpg?{expectedQueryString}";
string expectedLink = $"{BaseUrl}/v1/{TestKey}/jpeg?{expectedQueryString}";

string result = renderLinkFactory.GenerateRenderLink(BaseUrl, options, "jpg", sign: false);
string result = renderLinkFactory.GenerateRenderLink(BaseUrl, options, sign: false);

Assert.AreEqual(expectedLink, result);
}
Expand Down
13 changes: 7 additions & 6 deletions UrlboxSDK.MsTest/UrlboxTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -299,24 +299,25 @@ public void GenerateRenderLink_IgnoreEmptyValuesAndFormat()
};

string output = urlbox.GenerateRenderLink(options);
Assert.AreEqual("https://api.urlbox.com/v1/MY_API_KEY/8e00ad9a8d7c4abcd462a9b8ec041c3661f13995/png?url=https%3A%2F%2Fbbc.co.uk",
Assert.AreEqual("https://api.urlbox.com/v1/MY_API_KEY/8e00ad9a8d7c4abcd462a9b8ec041c3661f13995/pdf?url=https%3A%2F%2Fbbc.co.uk",
output);
}

[TestMethod]
public void GenerateSignedRenderLink_Succeeds()
{
UrlboxOptions options = new(url: "https://bbc.co.uk");
string output = urlbox.GenerateSignedRenderLink(options, "jpeg");
string output = urlbox.GenerateSignedRenderLink(options);
Assert.AreEqual("https://api.urlbox.com/v1/MY_API_KEY/8e00ad9a8d7c4abcd462a9b8ec041c3661f13995/jpeg?url=https%3A%2F%2Fbbc.co.uk", output, "Not OK!");
}

[TestMethod]
public void GenerateRenderLink_FormatWorks()
{
UrlboxOptions options = new(url: "https://bbc.co.uk");
string output = urlbox.GenerateRenderLink(options, "jpeg");
Assert.AreEqual("https://api.urlbox.com/v1/MY_API_KEY/8e00ad9a8d7c4abcd462a9b8ec041c3661f13995/jpeg?url=https%3A%2F%2Fbbc.co.uk", output, "Not OK!");
options.Format = Format.Avif;
string output = urlbox.GenerateRenderLink(options);
Assert.AreEqual("https://api.urlbox.com/v1/MY_API_KEY/8e00ad9a8d7c4abcd462a9b8ec041c3661f13995/avif?url=https%3A%2F%2Fbbc.co.uk", output, "Not OK!");
}

[TestMethod]
Expand Down Expand Up @@ -1024,7 +1025,7 @@ public async Task DownloadToFile_succeeds_overload()
"somebuffer" // No response body or error headers
);

string result = await urlbox.DownloadToFile(options, filename, format: "png", sign: false);
string result = await urlbox.DownloadToFile(options, filename, sign: false);
Assert.IsNotNull(result);
Assert.IsInstanceOfType(result, typeof(String));
Assert.IsTrue(result.Length >= 0);
Expand Down Expand Up @@ -1108,7 +1109,7 @@ public async Task DownloadBase64_succeeds_overload()
HttpStatusCode.OK,
mockContent
);
string base64result = await urlbox.DownloadAsBase64(options, "png", sign: false);
string base64result = await urlbox.DownloadAsBase64(options, sign: false);

Assert.IsNotNull(base64result);
Assert.AreEqual(expectedBase64, base64result, "Expected the base64 string to match the mocked content.");
Expand Down
5 changes: 4 additions & 1 deletion UrlboxSDK/Factory/RenderLinkFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -143,8 +143,11 @@ private static string ConvertToString(object value)
/// <param name="options"></param>
/// <param name="format"></param>
/// <returns>The Urlbox Render Link</returns>
public string GenerateRenderLink(string baseUrl, UrlboxOptions options, string format = "png", bool sign = true)
public string GenerateRenderLink(string baseUrl, UrlboxOptions options, bool sign = true)
{
// Either the options.Format or PNG as default
string format = options.Format?.ToString().ToLower() ?? "png";

string queryString = ToQueryString(options);
if (sign)
{
Expand Down
6 changes: 3 additions & 3 deletions UrlboxSDK/IUrlbox.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,16 @@ public interface IUrlbox
Task<string> ExtractMhtml(UrlboxOptions options);

// Download and File Handling Methods
Task<string> DownloadAsBase64(UrlboxOptions options, string format = "png", bool sign = true);
Task<string> DownloadAsBase64(UrlboxOptions options, bool sign = true);
Task<string> DownloadAsBase64(string urlboxUrl);
Task<string> DownloadToFile(string urlboxUrl, string filename);
Task<string> DownloadToFile(UrlboxOptions options, string filename, string format = "png", bool sign = true);
Task<string> DownloadToFile(UrlboxOptions options, string filename, bool sign = true);

// URL Generation Methods
string GeneratePNGUrl(UrlboxOptions options, bool sign = true);
string GenerateJPEGUrl(UrlboxOptions options, bool sign = true);
string GeneratePDFUrl(UrlboxOptions options, bool sign = true);
string GenerateRenderLink(UrlboxOptions options, string format = "png", bool sign = true);
string GenerateRenderLink(UrlboxOptions options, bool sign = true);

// Status and Validation Methods
Task<AsyncUrlboxResponse> GetStatus(string statusUrl);
Expand Down
29 changes: 16 additions & 13 deletions UrlboxSDK/Urlbox.cs
Original file line number Diff line number Diff line change
Expand Up @@ -330,11 +330,11 @@ public async Task<string> ExtractMhtml(UrlboxOptions options)
/// Downloads a screenshot as a Base64-encoded string from a Urlbox render link.
/// </summary>
/// <param name="options">The options for the screenshot</param>
/// <param name="format">The image format (e.g., "png", "jpg").</param>
/// <param name="format">The image format (e.g., "png" ).</param>
/// <returns>A Base64-encoded string of the screenshot.</returns>
public async Task<string> DownloadAsBase64(UrlboxOptions options, string format = "png", bool sign = true)
public async Task<string> DownloadAsBase64(UrlboxOptions options, bool sign = true)
{
string urlboxUrl = GenerateRenderLink(options, format, sign);
string urlboxUrl = GenerateRenderLink(options, sign);
return await DownloadAsBase64(urlboxUrl);
}

Expand Down Expand Up @@ -381,11 +381,11 @@ async Task<string> onSuccess(HttpResponseMessage result)
/// </summary>
/// <param name="options">The options for the screenshot.</param>
/// <param name="filename">The file path where the screenshot will be saved.</param>
/// <param name="format">The image format (e.g., "png", "jpg"). Default is "png".</param>
/// <param name="format">The image format (e.g., "png"). Default is "png".</param>
/// <returns>The contents of the downloaded file as a string.</returns>
public async Task<string> DownloadToFile(UrlboxOptions options, string filename, string format = "png", bool sign = true)
public async Task<string> DownloadToFile(UrlboxOptions options, string filename, bool sign = true)
{
string urlboxUrl = GenerateRenderLink(options, format, sign);
string urlboxUrl = GenerateRenderLink(options, sign);
return await DownloadToFile(urlboxUrl, filename);
}

Expand All @@ -398,7 +398,8 @@ public async Task<string> DownloadToFile(UrlboxOptions options, string filename,
/// <returns>A render link Url to render a PNG screenshot.</returns>
public string GeneratePNGUrl(UrlboxOptions options, bool sign = true)
{
return GenerateRenderLink(options, "png", sign);
options.Format = Format.Png;
return GenerateRenderLink(options, sign);
}

/// <summary>
Expand All @@ -408,7 +409,8 @@ public string GeneratePNGUrl(UrlboxOptions options, bool sign = true)
/// <returns>A render link Url to render a JPEG screenshot.</returns>
public string GenerateJPEGUrl(UrlboxOptions options, bool sign = true)
{
return GenerateRenderLink(options, "jpg", sign);
options.Format = Format.Jpeg;
return GenerateRenderLink(options, sign);
}

/// <summary>
Expand All @@ -418,7 +420,8 @@ public string GenerateJPEGUrl(UrlboxOptions options, bool sign = true)
/// <returns>A render link Url to render a PDF file.</returns>
public string GeneratePDFUrl(UrlboxOptions options, bool sign = true)
{
return GenerateRenderLink(options, "pdf", sign);
options.Format = Format.Pdf;
return GenerateRenderLink(options, sign);
}

/// <summary>
Expand All @@ -427,9 +430,9 @@ public string GeneratePDFUrl(UrlboxOptions options, bool sign = true)
/// <param name="options">The options for generating the screenshot or PDF.</param>
/// <param name="format">The format of the output, e.g., "png", "jpg", "pdf".</param>
/// <returns>A render link URL to render the content.</returns>
public string GenerateRenderLink(UrlboxOptions options, string format = "png", bool sign = true)
public string GenerateRenderLink(UrlboxOptions options, bool sign = true)
{
return renderLinkFactory.GenerateRenderLink(baseUrl, options, format, sign);
return renderLinkFactory.GenerateRenderLink(baseUrl, options, sign);
}

/// <summary>
Expand All @@ -438,9 +441,9 @@ public string GenerateRenderLink(UrlboxOptions options, string format = "png", b
/// <param name="options">The options for generating the screenshot or PDF.</param>
/// <param name="format">The format of the output, e.g., "png", "jpg", "pdf".</param>
/// <returns>A render link URL to render the content.</returns>
public string GenerateSignedRenderLink(UrlboxOptions options, string format = "png")
public string GenerateSignedRenderLink(UrlboxOptions options)
{
return renderLinkFactory.GenerateRenderLink(baseUrl, options, format, true);
return renderLinkFactory.GenerateRenderLink(baseUrl, options, sign: true);
}

// ** Status and Validation Methods **
Expand Down

0 comments on commit ec2b65d

Please sign in to comment.