Class EquinixClient
- All Implemented Interfaces:
Closeable, AutoCloseable
EquinixClient and the domain clients delegate to an instance
of this class.
Token sources
A client is built over exactly one token source: OAuth2 client credentials
(EquinixCredentialsProvider; the facade wires the token mint through
setAuthenticator(Runnable)), or a BearerTokenProvider that supplies ready-made tokens
(for example an STS-issued access-policy token). Either way the token is obtained lazily on the
first authenticated request and again when it expires, single-flight.
401 recovery
When an authenticated request is answered with HTTP 401 the cached token is discarded, a new
one is obtained once (single-flight: concurrent requests rejected with the same token cause one
token request) and the request is replayed once. A second 401 is thrown. There is no replay for
the token request itself, for endpoints declared "unauthenticated", or when no token
source is wired. Replaying is safe for every method because a 401 means the request was rejected
before it was processed.
- Author:
- ianjones
-
Constructor Summary
ConstructorsConstructorDescriptionEquinixClient(BearerTokenProvider bearerTokenProvider, boolean isSandBoxed) Creates a transport client that signs requests with tokens from the given provider instead of minting OAuth2 client-credentials tokens.EquinixClient(EquinixCredentials equinixCredentials, boolean isSandBoxed) Creates a transport client over fixed OAuth2 client credentials.EquinixClient(EquinixCredentialsProvider credentialsProvider, boolean isSandBoxed) Creates a transport client that resolves its credentials through the given provider. -
Method Summary
Modifier and TypeMethodDescriptionvoidappendApiParams(String fileName) Merges a domain's apiParams resource into the shared catalogue.voidForces (re)authentication from the wired token source (the client-credentials authenticator or theBearerTokenProvider), single-flight undertokenLock.voidclose()getEndPointFor(String functionalArea) Resolves the host a functional area's requests are sent to.Returns the currently published OAuth token, ornullif the client has not authenticated yet.<T> EquinixResponse<T> invoke(EquinixRequest<T> equinixRequest) Resolves, signs and dispatches a request, with the retry policy, the circuit breaker and the one-shot 401 recovery described on this class.booleanvoidsetAuthenticator(Runnable authenticator) Wires the callback used to mint and publish an OAuth token for lazy authentication and re-authentication on expiry or after a 401.voidsetCircuitBreaker(CircuitBreaker circuitBreaker) Enables (or, withnull, disables) the opt-in circuit breaker consulted for every request attempt.voidReplaces the clock used to judge token expiry andRetry-Afterdates (defaultClock.systemUTC()).voidsetDomainEndPoint(String functionalArea, String endPoint) Routes one functional area (for example"IAM"or"STS") to its own host, overriding both the catalogue's per-domain host andsetEndPoint(String).voidsetEndPoint(String endPoint) Replaces the client-wide endpoint (a gateway, proxy or WireMock); from then on it wins over the catalogue's per-domain hosts unless adomain overrideis set.voidsetOAuthToken(OAuthToken oAuthToken) Publishes a (re)authenticated OAuth token for use by subsequent requests.voidsetRetryPolicy(RetryPolicy retryPolicy) Overrides the automatic retry behavior for transient failures (429/5xx, transient IO).
-
Constructor Details
-
EquinixClient
Creates a transport client over fixed OAuth2 client credentials.- Parameters:
equinixCredentials- the client credentialsisSandBoxed-truefor the sandbox environment;falsefor production
-
EquinixClient
Creates a transport client that resolves its credentials through the given provider. The provider is consulted on each authentication (first call and re-auth on expiry), so a custom provider can rotate the underlying credentials without the client being rebuilt.- Parameters:
credentialsProvider- supplies the OAuth2 credentials to authenticate withisSandBoxed-truefor the sandbox environment;falsefor production
-
EquinixClient
Creates a transport client that signs requests with tokens from the given provider instead of minting OAuth2 client-credentials tokens. No token-mint authenticator is needed or used: the provider is asked for a token on the first authenticated call, before a known expiry, and once after an HTTP 401.- Parameters:
bearerTokenProvider- supplies the bearer tokensisSandBoxed-truefor the sandbox environment;falsefor production
-
-
Method Details
-
setClock
Replaces the clock used to judge token expiry andRetry-Afterdates (defaultClock.systemUTC()). Intended for deterministic tests and simulations.- Parameters:
clock- the clock; must not benull
-
setEndPoint
Replaces the client-wide endpoint (a gateway, proxy or WireMock); from then on it wins over the catalogue's per-domain hosts unless adomain overrideis set.- Parameters:
endPoint- the host, with or without scheme- Throws:
IllegalArgumentException- if the endpoint is not a valid URI
-
setDomainEndPoint
Routes one functional area (for example"IAM"or"STS") to its own host, overriding both the catalogue's per-domain host andsetEndPoint(String).- Parameters:
functionalArea- the functional-area key from the apiParams catalogueendPoint- the host (with or without scheme);nullremoves the override
-
getEndPointFor
Resolves the host a functional area's requests are sent to. Precedence:- an explicit
setDomainEndPoint(String, String)override; - the client-wide endpoint, when a caller replaced it with
setEndPoint(String); - the functional area's own
hostName/sandboxHostNamein its apiParams resource (IAM is served byaccess.eqix.equinix.comand STS bysts.eqix.equinix.com;api.equinix.comanswers 404 for both); - the client-wide default endpoint.
- Parameters:
functionalArea- the functional-area key, may benull- Returns:
- the endpoint for that area, never
null
- an explicit
-
setRetryPolicy
Overrides the automatic retry behavior for transient failures (429/5xx, transient IO).- Parameters:
retryPolicy- the policy to apply;RetryPolicy.none()disables retries
-
setCircuitBreaker
Enables (or, withnull, disables) the opt-in circuit breaker consulted for every request attempt. SeeCircuitBreakerfor the open/half-open/closed semantics. Disabled by default.- Parameters:
circuitBreaker- the breaker to enforce, ornullto disable
-
appendApiParams
Merges a domain's apiParams resource into the shared catalogue. Thread-safe: the merge is copy-on-write underapiParamsLockand published atomically through thevolatileclientResourceFilefield (read via the Lombok-generatedgetClientResourceFile()), so request threads reading the tree concurrently (Jackson'sObjectNodeis not safe for concurrent read/write) always see either the old or the fully-merged catalogue.- Parameters:
fileName- the classpath resource name (e.g.json/apiParams_Fabric.json)- Throws:
EquinixClientException- if the resource is missing, malformed, or does not declare afunctionalAreasobject
-
invoke
Resolves, signs and dispatches a request, with the retry policy, the circuit breaker and the one-shot 401 recovery described on this class.- Type Parameters:
T- the response payload type- Parameters:
equinixRequest- the request to send- Returns:
- the successful response
- Throws:
EquinixClientException- if the request cannot be built or sentEquinixServiceException- if the API answers with an error
-
isSandBoxed
public boolean isSandBoxed()- Returns:
- a boolean.
-
getOAuthToken
Returns the currently published OAuth token, ornullif the client has not authenticated yet.This is a lock-free
volatileread returning the latest published instance. For an atomic validity-then-use decision under concurrency, prefer the internal single-flight path insetStandardHeaders(EquinixRequest)which captures a consistent snapshot undertokenLock.- Returns:
- the current
OAuthToken, may benull.
-
setOAuthToken
Publishes a (re)authenticated OAuth token for use by subsequent requests.Publication is serialized through
tokenLockso that a token obtained by a single-flight refresh becomes visible atomically with respect to other threads inspecting validity. Once stored, thevolatilefield guarantees the new instance is visible to all threads.- Parameters:
oAuthToken- the freshly obtained token, may benullto clear.
-
setAuthenticator
Wires the callback used to mint and publish an OAuth token for lazy authentication and re-authentication on expiry or after a 401. The facade client supplies this; the core client invokes it when a request needs a token and none valid is published. Ignored when this client was built over aBearerTokenProvider, which then is the only token source.- Parameters:
authenticator- the token-acquisition callback (it must publish viasetOAuthToken(OAuthToken))
-
authenticate
public void authenticate()Forces (re)authentication from the wired token source (the client-credentials authenticator or theBearerTokenProvider), single-flight undertokenLock. This is the explicitauthenticate()entry point. Theauthenticatingguard means the token request it issues (which itself flows throughsetStandardHeaders(EquinixRequest)) does not recurse into another authentication. No-op when no token source is wired. -
close
- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable- Throws:
IOException
-