// Copyright (c) Microsoft. All rights reserved.
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Threading;
using System.Threading.Tasks;
namespace Microsoft.Agents.AI.Workflows.Declarative;
///
/// Defines the contract for executing HTTP requests emitted by HttpRequestAction within declarative workflows.
///
///
/// This interface allows the HTTP request dispatch to be abstracted, enabling different implementations
/// for local development, hosted workflows, authenticated scenarios, and testing.
///
public interface IHttpRequestHandler
{
///
/// Sends an HTTP request and returns the response.
///
/// The HTTP request to send.
/// A token to observe cancellation.
/// The describing the HTTP response.
Task SendAsync(
HttpRequestInfo request,
CancellationToken cancellationToken = default);
}
///
/// Describes an HTTP request to be sent by an .
///
[SuppressMessage("Design", "CA1056:URI-like properties should not be strings", Justification = "URL is carried as a string to preserve the declarative expression result and to avoid forcing handler implementations to construct a Uri eagerly.")]
public sealed class HttpRequestInfo
{
///
/// Gets the HTTP method to use (GET, POST, PUT, PATCH, DELETE).
///
public string Method { get; init; } = "GET";
///
/// Gets the absolute URL to send the request to.
///
public string Url { get; init; } = string.Empty;
///
/// Gets the headers to include on the request, excluding the Content-Type header (which is supplied via ).
///
public IReadOnlyDictionary? Headers { get; init; }
///
/// Gets the Content-Type of the request body, or if no body is sent.
///
public string? BodyContentType { get; init; }
///
/// Gets the serialized request body, or if no body is sent.
///
public string? Body { get; init; }
///
/// Gets the maximum amount of time to wait for the request to complete, or to use the handler default.
///
public TimeSpan? Timeout { get; init; }
///
/// Gets the query parameters to append to the request URL, with values already formatted as strings.
///
public IReadOnlyDictionary? QueryParameters { get; init; }
///
/// Gets the name of the declared remote connection, or if no connection is declared.
/// This maps to the Foundry project connection Id and is only used when running in foundry service.
///
public string? ConnectionName { get; init; }
}
///
/// Represents the result of an HTTP request executed by an .
///
public sealed class HttpRequestResult
{
///
/// Gets the HTTP status code returned by the server.
///
public int StatusCode { get; init; }
///
/// Gets a value indicating whether the status code is in the range 200-299.
///
public bool IsSuccessStatusCode { get; init; }
///
/// Gets the response body, or if no body was returned.
///
public string? Body { get; init; }
///
/// Gets the response headers keyed by header name. Each header may have multiple values.
///
public IReadOnlyDictionary>? Headers { get; init; }
}