Class AlpacaHttpConfig

java.lang.Object
markets.alpaca.client.http.AlpacaHttpConfig

public final class AlpacaHttpConfig extends Object
Factory for OkHttp client instances with Alpaca-recommended defaults.

defaultClient() returns a shared singleton — OkHttp instances hold a thread pool and a connection pool and should be shared across the application when the same transport policy is appropriate. Pass the result directly to AlpacaClientFactory, or use defaultBuilder() to create a customised instance for a specific API, credential set, timeout profile, proxy, retry interceptor, logging policy, or connection pool.

  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final Duration
     
    static final Duration
     
    static final Duration
     
  • Method Summary

    Modifier and Type
    Method
    Description
    static okhttp3.OkHttpClient.Builder
    Returns a pre-configured builder so callers can add their own interceptors, certificates, retry/backoff policies, or other customisations before calling build().
    static okhttp3.OkHttpClient
    Returns the shared singleton OkHttpClient with Alpaca defaults (no logging).
    static okhttp3.OkHttpClient
    loggingClient(okhttp3.logging.HttpLoggingInterceptor.Level level)
    Returns an OkHttpClient with HTTP-level request/response logging at the given level, writing to the platform logger (java.util.logging).
    static okhttp3.OkHttpClient
    loggingClient(okhttp3.logging.HttpLoggingInterceptor.Level level, okhttp3.logging.HttpLoggingInterceptor.Logger logger)
    Returns an OkHttpClient with HTTP-level request/response logging at the given level, writing to a caller-supplied HttpLoggingInterceptor.Logger.
    static okhttp3.OkHttpClient
    Returns an OkHttp client with default timeouts and the default Alpaca retry policy.
    static okhttp3.OkHttpClient
    Returns an OkHttp client with default timeouts and the supplied Alpaca retry policy.
    static okhttp3.OkHttpClient
    withAgentInformation(okhttp3.OkHttpClient httpClient)
    Returns an HTTP client that identifies this SDK and its version in the User-Agent header using APCA-JAVA/<sdk-version> Java/<runtime-version>.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • DEFAULT_CONNECT_TIMEOUT

      public static final Duration DEFAULT_CONNECT_TIMEOUT
    • DEFAULT_READ_TIMEOUT

      public static final Duration DEFAULT_READ_TIMEOUT
    • DEFAULT_WRITE_TIMEOUT

      public static final Duration DEFAULT_WRITE_TIMEOUT
  • Method Details

    • defaultClient

      public static okhttp3.OkHttpClient defaultClient()
      Returns the shared singleton OkHttpClient with Alpaca defaults (no logging). Use defaultBuilder() if you need a distinct instance with different timeouts, interceptors, retry/backoff behavior, logging, proxy settings, or connection-pool sizing. Use retryingClient(AlpacaRetryPolicy) for opt-in application-level retries of idempotent requests.
    • defaultBuilder

      public static okhttp3.OkHttpClient.Builder defaultBuilder()
      Returns a pre-configured builder so callers can add their own interceptors, certificates, retry/backoff policies, or other customisations before calling build().

      OkHttp's transport retry support is enabled by default, but application-level retries for responses such as HTTP 429 or 5xx are opt-in. Add AlpacaRetryInterceptor when you want conservative retries for idempotent requests.

    • withAgentInformation

      public static okhttp3.OkHttpClient withAgentInformation(okhttp3.OkHttpClient httpClient)
      Returns an HTTP client that identifies this SDK and its version in the User-Agent header using APCA-JAVA/<sdk-version> Java/<runtime-version>.

      If the supplied client already has the Alpaca agent interceptor, the same instance is returned. Otherwise, a derived client is returned without modifying the supplied instance. The SDK's canonical agent value replaces any existing User-Agent header.

    • retryingClient

      public static okhttp3.OkHttpClient retryingClient()
      Returns an OkHttp client with default timeouts and the default Alpaca retry policy.
    • retryingClient

      public static okhttp3.OkHttpClient retryingClient(AlpacaRetryPolicy policy)
      Returns an OkHttp client with default timeouts and the supplied Alpaca retry policy.
    • loggingClient

      public static okhttp3.OkHttpClient loggingClient(okhttp3.logging.HttpLoggingInterceptor.Level level)
      Returns an OkHttpClient with HTTP-level request/response logging at the given level, writing to the platform logger (java.util.logging).

      HttpLoggingInterceptor.Level.BASIC logs request line, response code, and size — safe for all environments. Higher levels log headers and bodies:

      • HttpLoggingInterceptor.Level.HEADERS — logs all headers. The APCA-API-KEY-ID, APCA-API-SECRET-KEY, and Authorization headers are redacted automatically by this method, but any other sensitive headers you add will not be.
      • HttpLoggingInterceptor.Level.BODY — logs headers and bodies; same redaction applies. Avoid in production: response bodies may contain PII.
      Parameters:
      level - the desired logging verbosity
      See Also:
    • loggingClient

      public static okhttp3.OkHttpClient loggingClient(okhttp3.logging.HttpLoggingInterceptor.Level level, okhttp3.logging.HttpLoggingInterceptor.Logger logger)
      Returns an OkHttpClient with HTTP-level request/response logging at the given level, writing to a caller-supplied HttpLoggingInterceptor.Logger.

      Use this overload to route OkHttp log lines into your own logging framework (SLF4J, Logback, Log4j, Android Logcat, etc.):

      
       // SLF4J example
       Logger slf4j = LoggerFactory.getLogger(AlpacaHttpConfig.class);
       var client = AlpacaHttpConfig.loggingClient(Level.BASIC, slf4j::debug);
       

      The same header-redaction rules as loggingClient(HttpLoggingInterceptor.Level) apply.

      Parameters:
      level - the desired logging verbosity
      logger - destination for log lines produced by the interceptor