Skip to main content

Overview

This example demonstrates how to use coreauth.Manager.HttpRequest and coreauth.Manager.NewHttpRequest to execute arbitrary HTTP requests with provider credentials automatically injected. This is useful when you need to make custom API calls to a provider while leveraging the authentication system.

Use Case

Use this pattern when you need to:
  • Make custom API calls to a provider beyond standard completion requests
  • Execute HTTP requests with automatic credential injection
  • Test authentication and header injection logic
  • Build custom integrations that need provider credentials

Complete Source Code

Key Concepts

1. Two Approaches for HTTP Requests

The example demonstrates two different approaches:

Approach 1: NewHttpRequest + Custom Client

Use core.NewHttpRequest() to build a prepared request with credentials injected, then execute it with your own HTTP client:
This approach gives you full control over the HTTP client and transport.

Approach 2: HttpRequest (All-in-One)

Use core.HttpRequest() to both inject credentials and execute the request:
This approach is simpler and handles everything in one call.

2. Executor Integration

The EchoExecutor implements the minimal executor interface with:
  • Identifier() - Returns the provider key “echo”
  • PrepareRequest() - Injects the Authorization header from auth.Attributes["api_key"]
  • HttpRequest() - Executes the HTTP request with credentials
The other executor methods return errors since this example focuses only on HTTP request execution.

3. Authentication

Authentication is provided through the Auth struct:
The api_key attribute is extracted by PrepareRequest() and injected as a Bearer token.

4. Request Lifecycle

  1. Create an Auth object with credentials
  2. Register an executor with the Manager
  3. Build or create an HTTP request
  4. Call NewHttpRequest() or HttpRequest() on the manager
  5. The manager finds the appropriate executor and calls its PrepareRequest() method
  6. Credentials are injected into the request headers
  7. The request is executed and the response is returned

How to Run

  1. Run the example:
  1. The example will make two requests to httpbin.org:
    • A GET request using NewHttpRequest + manual execution
    • A POST request using HttpRequest (automatic execution)
  2. Both responses will show the injected Authorization: Bearer demo-api-key header.

Expected Output

Use Cases

Custom API Endpoints

Make requests to provider-specific endpoints that aren’t part of the standard completion API:

File Uploads

Upload files to providers with automatic authentication:

Testing Authentication

Test that your executor’s authentication logic works correctly: