Skip to content

Commit

Permalink
pass around shapes instead of entire service (#43)
Browse files Browse the repository at this point in the history
  • Loading branch information
dchelimsky committed Jul 12, 2023
1 parent d86dcce commit 5900e35
Show file tree
Hide file tree
Showing 8 changed files with 201 additions and 196 deletions.
18 changes: 9 additions & 9 deletions src/cognitect/aws/protocols/ec2.clj
Original file line number Diff line number Diff line change
Expand Up @@ -18,29 +18,29 @@
default))

(defmulti serialize
(fn [_service shape _args _serialized _prefix] (:type shape)))
(fn [_shapes shape _args _serialized _prefix] (:type shape)))

(defmethod serialize :default
[service shape args serialized prefix]
(query/serialize service shape args serialized prefix))
[shapes shape args serialized prefix]
(query/serialize shapes shape args serialized prefix))

(defmethod serialize "structure"
[service shape args serialized prefix]
[shapes shape args serialized prefix]
(let [args (util/with-defaults shape args)]
(reduce (fn [serialized k]
(let [member-shape (shape/resolve service (shape/structure-member-shape-ref shape k))
(let [member-shape (shape/resolve shapes (shape/structure-member-shape-ref shape k))
member-name (serialized-name member-shape (name k))]
(if (contains? args k)
(serialize service member-shape (k args) serialized (conj prefix member-name))
(serialize shapes member-shape (k args) serialized (conj prefix member-name))
serialized)))
serialized
(keys (:members shape)))))

(defmethod serialize "list"
[service shape args serialized prefix]
(let [member-shape (shape/resolve service (shape/list-member-shape-ref shape))]
[shapes shape args serialized prefix]
(let [member-shape (shape/resolve shapes (shape/list-member-shape-ref shape))]
(reduce (fn [serialized [i member]]
(serialize service member-shape member serialized (conj prefix (str i))))
(serialize shapes member-shape member serialized (conj prefix (str i))))
serialized
(map-indexed (fn [i member] [(inc i) member]) args))))

Expand Down
25 changes: 13 additions & 12 deletions src/cognitect/aws/protocols/json.clj
Original file line number Diff line number Diff line change
Expand Up @@ -4,40 +4,41 @@
(ns ^:skip-wiki cognitect.aws.protocols.json
"Impl, don't call directly."
(:require [cognitect.aws.protocols :as aws.protocols]
[cognitect.aws.service :as service]
[cognitect.aws.shape :as shape]
[cognitect.aws.util :as util]))

(set! *warn-on-reflection* true)

(defmulti serialize
(fn [_service shape _data] (:type shape)))
(fn [_shapes shape _data] (:type shape)))

(defmethod serialize :default
[service shape data]
(shape/json-serialize service shape data))
[shapes shape data]
(shape/json-serialize shapes shape data))

(defmethod serialize "structure"
[service shape data]
[shapes shape data]
(->> (util/with-defaults shape data)
(shape/json-serialize service shape)))
(shape/json-serialize shapes shape)))

(defmethod aws.protocols/build-http-request "json"
[service {:keys [op request]}]
(let [operation (get-in service [:operations op])
input-shape (shape/resolve service (:input operation))]
shapes (:shapes service)
input-shape (shape/resolve shapes (:input operation))]
{:request-method :post
:scheme :https
:server-port 443
:uri "/"
:headers (aws.protocols/headers service operation)
:body (serialize service input-shape (or request {}))}))
:body (serialize shapes input-shape (or request {}))}))

(defmethod aws.protocols/parse-http-response "json"
[service {:keys [op]} {:keys [body]}]
(let [operation (get-in service [:operations op])
output-shape (shape/resolve service (:output operation))
body-str (util/bbuf->str body)]
(let [operation (get-in service [:operations op])
shapes (:shapes service)
output-shape (shape/resolve shapes (:output operation))
body-str (util/bbuf->str body)]
(if output-shape
(shape/json-parse service output-shape body-str)
(shape/json-parse shapes output-shape body-str)
{})))
49 changes: 25 additions & 24 deletions src/cognitect/aws/protocols/query.clj
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
"Impl, don't call directly."
(:require [clojure.string :as str]
[cognitect.aws.protocols :as aws.protocols]
[cognitect.aws.service :as service]
[cognitect.aws.shape :as shape]
[cognitect.aws.util :as util]))

Expand All @@ -20,74 +19,75 @@
(or (:locationName shape)
default))

(defmulti serialize
(fn [_service shape _args _serialized _prefix] (:type shape)))

(defn prefix-assoc
[serialized prefix val]
(assoc serialized (str/join "." prefix) val))

(defmulti serialize
(fn [_shapes shape _args _serialized _prefix] (:type shape)))

(defmethod serialize :default
[_service _shape args serialized prefix]
[_shapes _shape args serialized prefix]
(prefix-assoc serialized prefix (str args)))

(defmethod serialize "structure"
[service shape args serialized prefix]
[shapes shape args serialized prefix]
(let [args (util/with-defaults shape args)]
(reduce (fn [serialized k]
(let [member-shape (shape/resolve service (shape/structure-member-shape-ref shape k))
(let [member-shape (shape/resolve shapes (shape/structure-member-shape-ref shape k))
member-name (serialized-name member-shape (name k))]
(if (contains? args k)
(serialize service member-shape (k args) serialized (conj prefix member-name))
(serialize shapes member-shape (k args) serialized (conj prefix member-name))
serialized)))
serialized
(keys (:members shape)))))

(defmethod serialize "list"
[service shape args serialized prefix]
[shapes shape args serialized prefix]
(if (empty? args)
(prefix-assoc serialized prefix "")
(let [member-shape (shape/resolve service (shape/list-member-shape-ref shape))
(let [member-shape (shape/resolve shapes (shape/list-member-shape-ref shape))
list-prefix (if (:flattened shape)
(conj (vec (butlast prefix)) (serialized-name member-shape (last prefix)))
(conj prefix (serialized-name member-shape "member")))]
(reduce (fn [serialized [i member]]
(serialize service member-shape member serialized (conj list-prefix (str i))))
(serialize shapes member-shape member serialized (conj list-prefix (str i))))
serialized
(map-indexed (fn [i member] [(inc i) member]) args)))))

(defmethod serialize "map"
[service shape args serialized prefix]
[shapes shape args serialized prefix]
(let [map-prefix (if (:flattened shape) prefix (conj prefix "entry"))
key-shape (shape/resolve service (shape/map-key-shape-ref shape))
key-shape (shape/resolve shapes (shape/map-key-shape-ref shape))
key-suffix (serialized-name key-shape "key")
value-shape (shape/resolve service (shape/map-value-shape-ref shape))
value-shape (shape/resolve shapes (shape/map-value-shape-ref shape))
value-suffix (serialized-name value-shape "value")]
(reduce (fn [serialized [i k v]]
(as-> serialized $
(serialize service key-shape (name k) $ (conj map-prefix (str i) key-suffix))
(serialize service value-shape v $ (conj map-prefix (str i) value-suffix))))
(serialize shapes key-shape (name k) $ (conj map-prefix (str i) key-suffix))
(serialize shapes value-shape v $ (conj map-prefix (str i) value-suffix))))
serialized
(map-indexed (fn [i [k v]] [(inc i) k v]) args))))

(defmethod serialize "blob"
[_service _shape args serialized prefix]
[_shapes _shape args serialized prefix]
(prefix-assoc serialized prefix (util/base64-encode args)))

(defmethod serialize "timestamp"
[_service shape args serialized prefix]
[_shapes shape args serialized prefix]
(prefix-assoc serialized prefix (shape/format-date shape
args
(partial util/format-date util/iso8601-date-format))))

(defmethod serialize "boolean"
[_service _shape args serialized prefix]
[_shapes _shape args serialized prefix]
(prefix-assoc serialized prefix (if args "true" "false")))

(defn build-query-http-request
[{:keys [op request]} service serialize]
(let [operation (get-in service [:operations op])
input-shape (shape/resolve service (:input operation))
shapes (:shapes service)
input-shape (shape/resolve shapes (:input operation))
params {"Action" (name op)
"Version" (get-in service [:metadata :apiVersion])}]
{:request-method :post
Expand All @@ -96,17 +96,18 @@
:uri "/"
:headers (aws.protocols/headers service operation)
:body (util/query-string
(serialize service input-shape request params []))}))
(serialize shapes input-shape request params []))}))

(defmethod aws.protocols/build-http-request "query"
[service op-map]
(build-query-http-request op-map service serialize))

(defn build-query-http-response
[service {:keys [op]} {:keys [body]}]
(let [operation (get-in service [:operations op])]
(if-let [output-shape (shape/resolve service (:output operation))]
(shape/xml-parse service output-shape (util/bbuf->str body))
(let [operation (get-in service [:operations op])
shapes (:shapes service)]
(if-let [output-shape (shape/resolve shapes (:output operation))]
(shape/xml-parse shapes output-shape (util/bbuf->str body))
(util/xml->map (util/xml-read (util/bbuf->str body))))))

(defmethod aws.protocols/parse-http-response "query"
Expand Down
Loading

0 comments on commit 5900e35

Please sign in to comment.