-
Notifications
You must be signed in to change notification settings - Fork 102
Expand file tree
/
Copy pathmetadata_db2.clj
More file actions
189 lines (168 loc) · 7.84 KB
/
metadata_db2.clj
File metadata and controls
189 lines (168 loc) · 7.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
(ns cmr.transmit.metadata-db2
"This contains functions for interacting with the metadata db API. It uses the newer transmit namespace
style that concepts, and access control use"
(:require
[cheshire.core :as json]
[clj-http.client :as client]
[cmr.common.api.context :as ch]
[cmr.common.services.errors :as errors]
[cmr.transmit.config :as config]
[cmr.transmit.connection :as conn]
[cmr.transmit.http-helper :as h]
[ring.util.codec :as codec]))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; URL functions
(defn- providers-url
[conn]
(format "%s/providers" (conn/root-url conn)))
(defn- provider-url
[conn provider-id]
(format "%s/%s" (providers-url conn) provider-id))
(defn- concepts-url
[conn]
(format "%s/concepts" (conn/root-url conn)))
(defn- concept-search-url
[conn concept-type]
(format "%s/concepts/search/%s" (conn/root-url conn) (name concept-type)))
(defn- latest-concept-url
[conn concept-id]
(str (concepts-url conn) "/" concept-id))
(defn- concept-revision-url
[conn concept-id revision-id]
(str (concepts-url conn) "/" concept-id "/" revision-id))
(defn- concept-id-url
[conn concept-type provider-id native-id]
(str (conn/root-url conn)
"/concept-id/" (name concept-type) "/" provider-id "/" (codec/url-encode native-id)))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Helper functions
(defn finish-parse-concept
"Finishes the parsing of a concept. After a concept has been parsed from JSON some of its fields
may still be a String instead of a native clojure types."
[concept]
(when concept
(update-in concept [:concept-type] keyword)))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Request functions
(declare reset)
(h/defresetter reset :metadata-db)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Provider functions
(declare create-provider update-provider delete-provider)
(h/defcreator create-provider :metadata-db providers-url {:use-system-token? true})
(h/defupdater update-provider :metadata-db provider-url {:use-system-token? true})
(h/defdestroyer delete-provider :metadata-db provider-url {:use-system-token? true})
(defn get-providers
"Returns the list of providers configured in the metadata db. Valid options are
* :raw? - set to true to indicate the raw response should be returned. See
cmr.transmit.http-helper for more info. Default false.
* token - the user token to use. If not set the token in the context will
be used.
* http-options - Other http-options to be sent to clj-http."
([context]
(get-providers context nil))
([context {:keys [raw? http-options token]}]
(let [token (or token (:token context))
headers (when token {config/token-header token})]
(h/request context :metadata-db
{:url-fn providers-url
:method :get
:raw? raw?
:http-options (h/include-request-id context (merge {:accept :json
:headers headers}
http-options))}))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Concept functions
(declare save-concept)
(h/defcreator save-concept :metadata-db concepts-url {:use-system-token? true})
(defn get-concept-id
"Returns a concept id for the given concept type, provider, and native id"
([context concept-type provider-id native-id]
(get-concept-id context concept-type provider-id native-id nil))
([context concept-type provider-id native-id {:keys [raw? http-options]}]
(let [response (h/request context :metadata-db
{:url-fn #(concept-id-url % concept-type provider-id native-id)
:method :get
:raw? raw?
:use-system-token? true
:http-options (h/include-request-id context (merge {:accept :json} http-options))})]
(if raw?
response
(:concept-id response)))))
(defn find-concepts
"Searches metadata db for concepts matching the given parameters.
Valid options are:
* :raw? - set to true to indicate the raw response should be returned. See
cmr.transmit.http-helper for more info. Default false.
* http-options - Other http-options to be sent to clj-http."
([context params concept-type]
(find-concepts context params concept-type nil))
([context params concept-type {:keys [raw? http-options]}]
(-> context
(h/request :metadata-db
{:url-fn #(concept-search-url % concept-type)
:method :get
:raw? raw?
:use-system-token? true
:http-options (h/include-request-id context (merge {:accept :json} http-options params))})
finish-parse-concept)))
(defn get-concept
"Retrieve the concept with the given concept and revision-id. Valid options are
* :raw? - set to true to indicate the raw response should be returned. See
cmr.transmit.http-helper for more info. Default false.
* http-options - Other http-options to be sent to clj-http."
([context concept-id revision-id]
(get-concept context concept-id revision-id nil))
([context concept-id revision-id {:keys [raw? http-options]}]
(-> context
(h/request :metadata-db
{:url-fn #(concept-revision-url % concept-id revision-id)
:method :get
:raw? raw?
:use-system-token? true
:http-options (h/include-request-id context (merge {:accept :json} http-options))})
finish-parse-concept)))
(defn get-latest-concept
"Retrieve the latest verison of a concept. Valid options are
* :raw? - set to true to indicate the raw response should be returned. See
cmr.transmit.http-helper for more info. Default false.
* http-options - Other http-options to be sent to clj-http."
([context concept-id]
(get-latest-concept context concept-id nil))
([context concept-id {:keys [raw? http-options]}]
(-> context
(h/request :metadata-db
{:url-fn #(latest-concept-url % concept-id)
:method :get
:raw? raw?
:use-system-token? true
:http-options (h/include-request-id context (merge {:accept :json} http-options))})
finish-parse-concept)))
;; Defines health check function
(declare get-metadata-db-health)
(h/defhealther get-metadata-db-health :metadata-db {:timeout-secs 2})
(defn get-subscription-cache-content
"Retrieves the cache contents of the ingest subscription cache."
([context coll-concept-id]
(get-subscription-cache-content context coll-concept-id nil))
([context coll-concept-id {:keys [raw? http-options]}]
(let [
conn (config/context->app-connection context :metadata-db)
request-url (str (conn/root-url conn) "/subscription/cache-content")
params (merge
(config/conn-params conn)
{:accept :json
:query-params {:collection-concept-id coll-concept-id}
:headers (merge
(ch/context->http-headers context)
{:client-id config/cmr-client-id})
:throw-exceptions false
:http-options (h/include-request-id context {})})
response (client/get request-url params)
{:keys [status body]} response
status (int status)]
(case status
200 (json/decode body true)
;; default
(errors/internal-error!
(format "Get subscription cache content failed for collection %s. status: %s body: %s" coll-concept-id status body))))))