Skip to content

Commit

Permalink
Refactor guard clauses and add inline documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
patchoulish committed Nov 8, 2024
1 parent baf22b4 commit 1df887e
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 45 deletions.
Original file line number Diff line number Diff line change
@@ -1,20 +1,25 @@
using System;

using Microsoft;
using Microsoft.Extensions;
using Microsoft.Extensions.DependencyInjection;

namespace Firecrawl
{
/// <summary>
///
/// Represents the options when registering an instance of the <see cref="FirecrawlService"/>
/// class with an <see cref="IServiceCollection"/>.
/// </summary>
public sealed class FirecrawlServiceOptions
{
/// <summary>
///
/// Gets or sets the base URL to use.
/// </summary>
public Uri BaseUrl { get; set; } =
FirecrawlService.DefaultBaseUrl;

/// <summary>
///
/// Gets or sets the API key to use.
/// </summary>
public string ApiKey { get; set; }
}
Expand Down
16 changes: 9 additions & 7 deletions source/Firecrawl.Extensions.DependencyInjection/Guard.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,26 @@
namespace Firecrawl
{
/// <summary>
///
/// Contains guard clauses for throwing exceptions.
/// </summary>
internal static class Guard
{
/// <summary>
///
/// Throws an exception if the specified argument is null.
/// </summary>
/// <param name="argument"></param>
/// <param name="paramName"></param>
/// <exception cref="ArgumentNullException"></exception>
/// <param name="argument">The argument specified.</param>
/// <param name="argumentName">The name of the argument specified.</param>
/// <exception cref="ArgumentNullException">
/// Thrown if the specified argument is null.
/// </exception>
public static void NotNull(
object argument,
string paramName)
string argumentName)
{
if (argument == null)
{
throw new ArgumentNullException(
paramName);
argumentName);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,25 @@
namespace Firecrawl
{
/// <summary>
///
/// Contains extension methods for
/// <see cref="IServiceCollection"/>.
/// </summary>
public static class ServiceCollectionExtensions
{
/// <summary>
///
/// The name to use for the named client.
/// </summary>
private const string FirecrawlHttpClientName =
"firecrawl";

/// <summary>
///
/// Registers an instance of the <see cref="FirecrawlService"/>
/// class with an <see cref="IServiceCollection"/>
/// using a named <see cref="HttpClient"/>.
/// </summary>
/// <param name="services"></param>
/// <param name="optionsCallback"></param>
/// <returns></returns>
/// <param name="services">The service collection to register with.</param>
/// <param name="optionsCallback">The callback to configure the options for the instance with.</param>
/// <returns>An <see cref="IHttpClientBuilder"/> that can be used to configure the named client.</returns>
public static IHttpClientBuilder AddFirecrawlHttpClient(
this IServiceCollection services,
Action<FirecrawlServiceOptions> optionsCallback = default)
Expand Down
70 changes: 41 additions & 29 deletions source/Firecrawl/Guard.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,89 +3,101 @@
namespace Firecrawl
{
/// <summary>
///
/// Contains guard clauses for throwing exceptions.
/// </summary>
internal static class Guard
{
/// <summary>
///
/// Throws an exception if the specified argument is null.
/// </summary>
/// <param name="argument"></param>
/// <param name="paramName"></param>
/// <exception cref="ArgumentNullException"></exception>
/// <param name="argument">The argument specified.</param>
/// <param name="argumentName">The name of the argument specified.</param>
/// <exception cref="ArgumentNullException">
/// Thrown if the specified argument is null.
/// </exception>
public static void NotNull(
object argument,
string paramName)
string argumentName)
{
if (argument == null)
{
throw new ArgumentNullException(
paramName);
argumentName);
}
}

/// <summary>
///
/// Throws an exception if the specified argument is null or empty.
/// </summary>
/// <param name="argument"></param>
/// <param name="paramName"></param>
/// <exception cref="ArgumentNullException"></exception>
/// <exception cref="ArgumentException"></exception>
/// <param name="argument">The argument specified.</param>
/// <param name="argumentName">The name of the argument specified.</param>
/// <exception cref="ArgumentNullException">
/// Thrown if the specified argument is null.
/// </exception>
/// <exception cref="ArgumentException">
/// Thrown if the specified argument is empty.
/// </exception>
public static void NotNullOrEmpty(
string argument,
string paramName)
string argumentName)
{
if (argument == null)
{
throw new ArgumentNullException(
paramName);
argumentName);
}

if (String.IsNullOrEmpty(argument))
{
throw new ArgumentException(
paramName);
argumentName);
}
}

/// <summary>
///
/// Throws an exception if the specified argument is null, empty, or whitespace.
/// </summary>
/// <param name="argument"></param>
/// <param name="paramName"></param>
/// <exception cref="ArgumentNullException"></exception>
/// <exception cref="ArgumentException"></exception>
/// <param name="argument">The argument specified.</param>
/// <param name="argumentName">The name of the argument specified.</param>
/// <exception cref="ArgumentNullException">
/// Thrown if the specified argument is null.
/// </exception>
/// <exception cref="ArgumentException">
/// Thrown if the specified argument is empty or whitespace.
/// </exception>
public static void NotNullOrWhiteSpace(
string argument,
string paramName)
string argumentName)
{
if (argument == null)
{
throw new ArgumentNullException(
paramName);
argumentName);
}

if (String.IsNullOrWhiteSpace(argument))
{
throw new ArgumentException(
paramName);
argumentName);
}
}

/// <summary>
///
/// Throws an exception if the specified argument is negative or zero.
/// </summary>
/// <param name="argument"></param>
/// <param name="paramName"></param>
/// <exception cref="ArgumentOutOfRangeException"></exception>
/// <param name="argument">The argument specified.</param>
/// <param name="argumentName">The name of the argument specified.</param>
/// <exception cref="ArgumentOutOfRangeException">
/// Thrown if the specified argument is negative or zero.
/// </exception>
public static void NotNegativeOrZero(
int argument,
string paramName)
string argumentName)
{
if (argument <= 0)
{
throw new ArgumentOutOfRangeException(
paramName);
argumentName);
}
}
}
Expand Down

0 comments on commit 1df887e

Please sign in to comment.