Skip to content

Commit

Permalink
Refactor client logic
Browse files Browse the repository at this point in the history
Closes #5
  • Loading branch information
patchoulish committed Nov 19, 2024
1 parent f908ed7 commit d20a53d
Show file tree
Hide file tree
Showing 6 changed files with 245 additions and 268 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using System;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;

using Microsoft;
using Microsoft.Extensions;
Expand Down Expand Up @@ -45,6 +44,7 @@ public static IHttpClientBuilder AddFirecrawlHttpClient(
options);

return services
.AddTransient<FirecrawlDelegatingHandler>()
.AddHttpClient<IFirecrawlService, FirecrawlService>(
FirecrawlHttpClientName,
(httpClient) =>
Expand All @@ -54,11 +54,11 @@ public static IHttpClientBuilder AddFirecrawlHttpClient(
options.BaseUrl;

// Configure the default request headers for the client.
httpClient.DefaultRequestHeaders.Authorization =
new AuthenticationHeaderValue(
"Bearer",
options.ApiKey);
});
httpClient.DefaultRequestHeaders.Add(
$"Authorization",
$"Bearer {options.ApiKey}");
})
.AddHttpMessageHandler<FirecrawlDelegatingHandler>();
}
}
}
65 changes: 65 additions & 0 deletions source/Firecrawl/FirecrawlDelegatingHandler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
using System;
using System.Net;
using System.Net.Http;
using System.Net.Http.Json;
using System.Threading;
using System.Threading.Tasks;

namespace Firecrawl
{
/// <summary>
/// Represents a <see cref="DelegatingHandler"/> that treats responses as
/// having been sent by Firecrawl.
/// </summary>
public sealed class FirecrawlDelegatingHandler :
DelegatingHandler
{
/// <summary>
/// Creates a new instance of the
/// <see cref="FirecrawlDelegatingHandler"/> class.
/// </summary>
public FirecrawlDelegatingHandler() :
base()
{ }

/// <inheritdoc/>
protected override async Task<HttpResponseMessage> SendAsync(
HttpRequestMessage request,
CancellationToken cancellationToken)
{
var httpClientResponse =
default(HttpResponseMessage);

try
{
httpClientResponse =
await base.SendAsync(
request,
cancellationToken);
}
catch (Exception ex)
{
throw new FirecrawlException(
"The operation was not successful.",
ex);
}

if (httpClientResponse.StatusCode != HttpStatusCode.OK)
{
var error =
await httpClientResponse.Content
.ReadFromJsonAsync<FirecrawlError>(
FirecrawlService.JsonSerializerOptions,
cancellationToken);

throw new FirecrawlException(
"The operation was not successful.",
default,
httpClientResponse.StatusCode,
error);
}

return httpClientResponse;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ namespace Firecrawl
/// <summary>
///
/// </summary>
public class FirecrawlErrorResult :
FirecrawlResult
public class FirecrawlError
{
/// <summary>
///
Expand Down
134 changes: 115 additions & 19 deletions source/Firecrawl/FirecrawlException.cs
Original file line number Diff line number Diff line change
@@ -1,67 +1,163 @@
using System;
using System.Net;
using System.Net.Http;

namespace Firecrawl
{
/// <summary>
///
/// </summary>
public class FirecrawlException :
Exception
HttpRequestException
{
#if NETSTANDARD

/// <summary>
///
/// Gets the HTTP status code for this exception, if any.
/// </summary>
public HttpStatusCode? StatusCode { get; private init; }

#endif

/// <summary>
/// Gets the Firecrawl error for this exception, if any.
/// </summary>
public FirecrawlError Error { get; private init; }

/// <summary>
///
/// Initializes a new instance of the <see cref="FirecrawlException"/>
/// class.
/// </summary>
public FirecrawlException() :
this(
default)
{ }

/// <summary>
/// Initializes a new instance of the <see cref="FirecrawlException"/>
/// class with a specific message that describes the current exception.
/// </summary>
/// <param name="message">
/// A message that describes the current exception.
/// </param>
public FirecrawlException(
string message) :
this(
message,
default)
{ }

/// <summary>
///
/// Initializes a new instance of the <see cref="FirecrawlException"/>
/// class with a specific message that describes the current exception
/// and an inner exception.
/// </summary>
/// <param name="statusCode"></param>
/// <param name="message">
/// A message that describes the current exception.
/// </param>
/// <param name="innerException">
/// The inner exception.
/// </param>
public FirecrawlException(
HttpStatusCode statusCode) :
string message,
Exception innerException) :
this(
statusCode,
message,
innerException,
default)
{ }

/// <summary>
///
/// Initializes a new instance of the <see cref="FirecrawlException"/>
/// class with a specific message that describes the current exception,
/// an inner exception, and an HTTP status code.
/// </summary>
/// <param name="statusCode"></param>
/// <param name="message"></param>
/// <param name="message">
/// A message that describes the current exception.
/// </param>
/// <param name="innerException">
/// The inner exception.
/// </param>
/// <param name="statusCode">
/// The HTTP status code.
/// </param>
public FirecrawlException(
HttpStatusCode statusCode,
string message) :
string message,
Exception innerException,
HttpStatusCode? statusCode) :
this(
statusCode,
message,
innerException,
statusCode,
default)
{ }

#if NETSTANDARD

/// <summary>
///
/// Initializes a new instance of the <see cref="FirecrawlException"/>
/// class with a specific message that describes the current exception,
/// an inner exception, an HTTP status code, and Firecrawl error.
/// </summary>
/// <param name="statusCode"></param>
/// <param name="message"></param>
/// <param name="innerException"></param>
/// <param name="message">
/// A message that describes the current exception.
/// </param>
/// <param name="innerException">
/// The inner exception.
/// </param>
/// <param name="statusCode">
/// The HTTP status code.
/// </param>
/// <param name="error">
/// The Firecrawl error.
/// </param>
public FirecrawlException(
HttpStatusCode statusCode,
string message,
Exception innerException) :
Exception innerException,
HttpStatusCode? statusCode,
FirecrawlError error) :
base(
message,
innerException)
{
StatusCode = statusCode;
Error = error;
}

#endif

#if NET

/// <summary>
/// Initializes a new instance of the <see cref="FirecrawlException"/>
/// class with a specific message that describes the current exception,
/// an inner exception, an HTTP status code, and Firecrawl error.
/// </summary>
/// <param name="message">
/// A message that describes the current exception.
/// </param>
/// <param name="innerException">
/// The inner exception.
/// </param>
/// <param name="statusCode">
/// The HTTP status code.
/// </param>
/// <param name="error">
/// The Firecrawl error.
/// </param>
public FirecrawlException(
string message,
Exception innerException,
HttpStatusCode? statusCode,
FirecrawlError error) :
base(
message,
innerException,
statusCode)
{
Error = error;
}

#endif
}
}
2 changes: 1 addition & 1 deletion source/Firecrawl/FirecrawlResult.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ namespace Firecrawl
/// </summary>
public class FirecrawlResult
{

// This space intentionally left blank.
}
}
Loading

0 comments on commit d20a53d

Please sign in to comment.