ApiConnectionManager is a generic, connection-agnostic HTTP auth manager. You instantiate it with a connection ID that matches a record in the UTB Integrations Hub, and it takes care of token negotiation, caching, and injecting the correct Authorization header into your requests.
Constructor
The ID of the connection as configured in UTB Builder → Configuración API. For CEP and CertificadosFlow the canonical ID is
cep_integrator.The constructor does not perform any network calls or database reads. Configuration is loaded lazily the first time a method that needs it is called.
Methods
get_api_base(): string
Returns the base URL configured for this connection, with a trailing slash stripped.
get_auth_headers(): array
Returns an array of HTTP headers required to authenticate a request. The content depends on the connection’s auth_type:
auth_type | Header emitted |
|---|---|
none | (empty array) |
basic_auth | Authorization: Basic base64(client_id:client_secret) |
bearer_token | Authorization: Bearer {bearer_token} |
oauth2_client_credentials | Authorization: Bearer {access_token} (token fetched and cached automatically) |
oauth2_client_credentials, the method calls the token endpoint at {api_base}/oauth/token using the Client Credentials grant, then caches the token in a WordPress Transient for expires_in - 60 seconds (minimum 60 s). Subsequent calls within the TTL return the cached token without a network request.
get_auth_request_args(array $additional_args = []): array
Builds a complete wp_remote_get()/wp_remote_post() args array with authentication headers pre-filled and a default Accept: application/json header. Additional args are deep-merged so you can override timeout, add body parameters, etc.
Optional array of additional
wp_remote_* args. Custom headers are merged with (not replaced by) the auth headers.Supported auth types
none
No authorization header is sent. Useful for public APIs or when authentication is handled at the network level.
basic_auth
The connection’s client_id and client_secret fields are base64-encoded and sent as HTTP Basic Auth:
bearer_token
The connection’s bearer_token field is sent as-is:
oauth2_client_credentials
The manager fetches a token from {api_base}/oauth/token using the OAuth 2.0 Client Credentials grant:
oauth_token_{connection_id}. The TTL is expires_in - 60 seconds (to account for clock skew), with a floor of 60 seconds.
Configuration storage
Connections are stored in the plugin’s global configuration (retrieved viaUTB_PB_DB_Schema::get_config()) under the api_connections array key. Each connection object must have:
| Field | Required | Description |
|---|---|---|
id | Yes | Unique connection identifier |
api_base | Yes | Base URL for the API |
auth_type | Yes | One of none, basic_auth, bearer_token, oauth2_client_credentials |
client_id | OAuth2 / Basic | Client ID or username |
client_secret | OAuth2 / Basic | Client secret or password |
bearer_token | Bearer | Static token |
api_connections is absent but the legacy oauth block is present, new ApiConnectionManager('cep_integrator') automatically maps the old CEP OAuth fields (cep_api_base, cep_client_id, cep_client_secret) to the new structure.
Usage in CEPFlow and CertificadosFlow
Both built-in flows use connection ID'cep_integrator':
Error handling
ApiConnectionManager throws \Exception in three situations:
| Situation | Message pattern |
|---|---|
| Connection ID not found in DB | La conexión API '{id}' no está configurada en el sistema. |
| OAuth token fetch fails (network) | Error comunicándose con el servidor OAuth para la conexión {id}. |
| OAuth server returns non-200 | Error de autenticación OAuth en {id}: HTTP {code} |
Unknown auth_type | Tipo de autenticación desconocido para la conexión {id}: {type} |