From 387cf3ea85be01fab42eeea79693d20bb1858ef2 Mon Sep 17 00:00:00 2001 From: David Chelimsky Date: Sun, 24 Mar 2019 13:29:16 -0500 Subject: [PATCH] support :endpoint-override map Closes #59 Closes #61 Closes #64 --- CHANGES.md | 1 + src/cognitect/aws/client.clj | 22 ++++++++++----- src/cognitect/aws/client/api.clj | 46 +++++++++++++++++++++----------- 3 files changed, 48 insertions(+), 21 deletions(-) diff --git a/CHANGES.md b/CHANGES.md index a3dcdb70..1d445b40 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -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 diff --git a/src/cognitect/aws/client.clj b/src/cognitect/aws/client.clj index 0575ec04..0124e4d7 100644 --- a/src/cognitect/aws/client.clj +++ b/src/cognitect/aws/client.clj @@ -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) diff --git a/src/cognitect/aws/client/api.clj b/src/cognitect/aws/client/api.clj index a34c3404..9734f7d3 100644 --- a/src/cognitect/aws/client/api.clj +++ b/src/cognitect/aws/client/api.clj @@ -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] @@ -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). @@ -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