Skip to content

Commit

Permalink
support :endpoint-override map
Browse files Browse the repository at this point in the history
Closes #59
Closes #61
Closes #64
  • Loading branch information
dchelimsky committed Mar 26, 2019
1 parent 6e27345 commit 387cf3e
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 21 deletions.
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## DEV

* support endpoint-override map [#59](https://github.com/cognitect-labs/aws-api/issues/59), [#61](https://github.com/cognitect-labs/aws-api/issues/61), [#64](https://github.com/cognitect-labs/aws-api/issues/64)
* only parse json response body when output-shape is specified [#66](https://github.com/cognitect-labs/aws-api/issues/66)

## 0.8.273 / 2019-03-01
Expand Down
22 changes: 16 additions & 6 deletions src/cognitect/aws/client.clj
Original file line number Diff line number Diff line change
Expand Up @@ -48,18 +48,28 @@
{:cognitect.anomalies/category :cognitect.anomalies/fault
::throwable t})))

(defn with-endpoint [req {:keys [protocol
hostname
port
path]
:as endpoint}]
(cond-> (-> req
(assoc-in [:headers "host"] hostname)
(assoc :server-name hostname))
protocol (assoc :scheme protocol)
port (assoc :server-port port)
path (assoc :uri path)))

(defn send-request
"Send the request to AWS and return a channel which delivers the response."
[client op-map]
(let [result-meta (atom {})]
(try
(let [{:keys [service region credentials endpoint http-client]} (-get-info client)
{:keys [hostname]} endpoint
http-request (sign-http-request service region (credentials/fetch credentials)
(-> (build-http-request service op-map)
(assoc-in [:headers "host"] hostname)
(assoc :server-name hostname)
((partial interceptors/modify-http-request service op-map))))]
http-request (sign-http-request service region (credentials/fetch credentials)
(-> (build-http-request service op-map)
(with-endpoint endpoint)
((partial interceptors/modify-http-request service op-map))))]
(swap! result-meta assoc :http-request http-request)
(http/submit http-client
(update http-request :body util/->bbuf)
Expand Down
46 changes: 31 additions & 15 deletions src/cognitect/aws/client/api.clj
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
(ns cognitect.aws.client.api
"API functions for using a client to interact with AWS services."
(:require [clojure.core.async :as a]
[clojure.tools.logging :as log]
[clojure.string :as str]
[cognitect.http-client :as http]
[cognitect.aws.client :as client]
Expand All @@ -30,10 +31,17 @@
cognitect.aws.credentials/CredentialsProvider
protocol, defaults to
cognitect.aws.credentials/default-credentials-provider
:endpoint-override - optional, overrides the configured endpoint. If the endpoint
includes an AWS region, be sure use the same region for
the client (either via out of process configuration or the :region key
passed to this fn).
:endpoint-override - optional, map to override parts of the endpoint. Supported keys:
:protocol - :http or :https
:hostname - string
:port - int
:path - string
If the hostname includes an AWS region, be sure use the same
region for the client (either via out of process configuration
or the :region key supplied to this fn).
Also supports a string representing just the hostname, though
support for a string is deprectated and may be removed in the
future.
:region-provider - optional, implementation of aws-clojure.region/RegionProvider
protocol, defaults to cognitect.aws.region/default-region-provider
:retriable? - optional, fn of http-response (see cognitect.http-client/submit).
Expand All @@ -48,23 +56,31 @@
Defaults to cognitect.aws.retry/default-backoff.
Alpha. Subject to change."
[{:keys [api region region-provider retriable? backoff credentials-provider endpoint-override] :as config}]
(let [service (service/service-description (name api))
region (keyword
(or region
(region/fetch
(or region-provider
(region/default-region-provider)))))]
[{:keys [api region region-provider retriable? backoff credentials-provider endpoint endpoint-override]
:or {endpoint-override {}}
:as config}]
(when (string? endpoint-override)
(log/warn
(format
"DEPRECATION NOTICE: :endpoint-override string is deprecated.\nUse {:endpoint-override {:hostname \"%s\"}} instead."
endpoint-override)))
(let [service (service/service-description (name api))
region (keyword
(or region
(region/fetch
(or region-provider
(region/default-region-provider)))))]
(require (symbol (str "cognitect.aws.protocols." (get-in service [:metadata :protocol]))))
(with-meta
(client/->Client
(atom {})
{:service service
:region region
:endpoint (or (cond-> (endpoint/resolve (-> service :metadata :endpointPrefix keyword) region)
endpoint-override
(assoc :hostname endpoint-override))
(throw (ex-info "No known endpoint." {:service api :region region})))
:endpoint (if-let [ep (endpoint/resolve (-> service :metadata :endpointPrefix keyword) region)]
(merge ep (if (string? endpoint-override)
{:hostname endpoint-override}
endpoint-override))
(throw (ex-info "No known endpoint." {:service api :region region})))
:retriable? (or retriable? retry/default-retriable?)
:backoff (or backoff retry/default-backoff)
:http-client (http/create {:trust-all true}) ;; FIX :trust-all
Expand Down

0 comments on commit 387cf3e

Please sign in to comment.