/
api.go
440 lines (327 loc) · 10.1 KB
/
api.go
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
package v1
import (
"compress/gzip"
"encoding/json"
"fmt"
"io"
"net/http"
"time"
"github.com/go-chi/chi/v5"
"github.com/CMSgov/bcda-app/bcda/api"
"github.com/CMSgov/bcda-app/bcda/auth"
"github.com/CMSgov/bcda-app/bcda/constants"
"github.com/CMSgov/bcda-app/bcda/health"
"github.com/CMSgov/bcda-app/bcda/responseutils"
"github.com/CMSgov/bcda-app/bcda/service"
"github.com/CMSgov/bcda-app/bcda/servicemux"
"github.com/CMSgov/bcda-app/conf"
"github.com/CMSgov/bcda-app/log"
)
var h *api.Handler
func init() {
resources, ok := service.GetDataTypes([]string{
"Patient",
"Coverage",
"ExplanationOfBenefit",
"Observation",
}...)
if ok {
h = api.NewHandler(resources, "/v1/fhir", "v1")
} else {
panic("Failed to configure resource DataTypes")
}
}
/*
swagger:route GET /api/v1/alr/$export alrData alrRequest
# Start FHIR STU3 data export for all supported resource types
Initiates a job to collect Assignment List Report data for your ACO. Supported resource types are Patient, Coverage, Group, Risk Assessment, Observation, and Covid Episode.
Produces:
- application/fhir+json
Security:
bearer_token:
Responses:
202: BulkRequestResponse
400: badRequestResponse
401: invalidCredentials
429: tooManyRequestsResponse
500: errorResponse
*/
func ALRRequest(w http.ResponseWriter, r *http.Request) {
h.ALRRequest(w, r)
}
/*
swagger:route GET /api/v1/Patient/$export bulkData bulkPatientRequest
# Start FHIR STU3 data export for all supported resource types
Initiates a job to collect data from the Blue Button API for your ACO. Supported resource types are Patient, Coverage, and ExplanationOfBenefit.
Produces:
- application/fhir+json
Security:
bearer_token:
Responses:
202: BulkRequestResponse
400: badRequestResponse
401: invalidCredentials
429: tooManyRequestsResponse
500: errorResponse
*/
func BulkPatientRequest(w http.ResponseWriter, r *http.Request) {
h.BulkPatientRequest(w, r)
}
/*
swagger:route GET /api/v1/Group/{groupId}/$export bulkData bulkGroupRequest
Start FHIR STU3 data export (for the specified group identifier) for all supported resource types
Initiates a job to collect data from the Blue Button API for your ACO. The supported Group identifiers are `all` and `runout`.
The `all` identifier returns data for the group of all patients attributed to the requesting ACO. If used when specifying `_since`: all claims data which has been updated since the specified date will be returned for beneficiaries which have been attributed to the ACO since before the specified date; and all historical claims data will be returned for beneficiaries which have been newly attributed to the ACO since the specified date.
The `runout` identifier returns claims runouts data.
Produces:
- application/fhir+json
Security:
bearer_token:
Responses:
202: BulkRequestResponse
400: badRequestResponse
401: invalidCredentials
429: tooManyRequestsResponse
500: errorResponse
*/
func BulkGroupRequest(w http.ResponseWriter, r *http.Request) {
h.BulkGroupRequest(w, r)
}
/*
swagger:route GET /api/v1/jobs/{jobId} job jobStatus
# Get job status
Returns the current status of an export job.
Produces:
- application/fhir+json
Schemes: http, https
Security:
bearer_token:
Responses:
202: jobStatusResponse
200: completedJobResponse
400: badRequestResponse
401: invalidCredentials
404: notFoundResponse
410: goneResponse
500: errorResponse
*/
func JobStatus(w http.ResponseWriter, r *http.Request) {
h.JobStatus(w, r)
}
/*
swagger:route GET /api/v1/jobs job jobsStatus
# Get jobs statuses
Returns the current statuses of export jobs. Supported status types are Completed, Archived, Expired, Failed, FailedExpired,
Pending, In Progress, Cancelled, and CancelledExpired. If no status(s) is provided, all jobs will be returned.
Note on job status to fhir task resource status mapping:
Due to the fhir task status field having a smaller set of values, the following statuses will be set to different fhir values in the response
Archived, Expired -> Completed
FailedExpired -> Failed
Pending -> In Progress
CancelledExpired -> Cancelled
Though the status name has been remapped the response will still only contain jobs pertaining to the provided job status in the request.
Produces:
- application/fhir+json
Schemes: http, https
Security:
bearer_token:
Responses:
200: jobsStatusResponse
400: badRequestResponse
401: invalidCredentials
404: notFoundResponse
410: goneResponse
500: errorResponse
*/
func JobsStatus(w http.ResponseWriter, r *http.Request) {
h.JobsStatus(w, r)
}
type gzipResponseWriter struct {
io.Writer
http.ResponseWriter
}
func (w gzipResponseWriter) Write(b []byte) (int, error) {
int, err := w.Writer.Write(b)
if err != nil {
log.API.Errorf("Error encountered in writing bytes with gzip writer: %s", err.Error())
}
return int, err
}
/*
swagger:route DELETE /api/v1/jobs/{jobId} job deleteJob
# Cancel a job
Cancels a currently running job.
Produces:
- application/fhir+json
Schemes: http, https
Security:
bearer_token:
Responses:
202: deleteJobResponse
400: badRequestResponse
401: invalidCredentials
404: notFoundResponse
410: goneResponse
500: errorResponse
*/
func DeleteJob(w http.ResponseWriter, r *http.Request) {
h.DeleteJob(w, r)
}
/*
swagger:route GET /api/v1/attribution_status attributionStatus attributionStatus
# Get attribution status
Returns the status of the latest ingestion for attribution and claims runout files. The response will contain the Type to identify which ingestion and a Timestamp for the last time it was updated.
Produces:
- application/json
Schemes: http, https
Security:
bearer_token:
Responses:
200: AttributionFileStatusResponse
404: notFoundResponse
*/
func AttributionStatus(w http.ResponseWriter, r *http.Request) {
h.AttributionStatus(w, r)
}
/*
swagger:route GET /data/{jobId}/{filename} job serveData
# Get data file
Returns the NDJSON file of data generated by an export job. Will be in the format <UUID>.ndjson. Get the full value from the job status response
Produces:
- application/fhir+json
Schemes: http, https
Security:
bearer_token:
Responses:
200: FileNDJSON
400: badRequestResponse
401: invalidCredentials
404: notFoundResponse
500: errorResponse
*/
func ServeData(w http.ResponseWriter, r *http.Request) {
dataDir := conf.GetEnv("FHIR_PAYLOAD_DIR")
fileName := chi.URLParam(r, "fileName")
jobID := chi.URLParam(r, "jobID")
w.Header().Set(constants.ContentType, "application/fhir+ndjson")
var useGZIP bool
for _, header := range r.Header.Values("Accept-Encoding") {
if header == "gzip" {
useGZIP = true
break
}
}
if useGZIP {
w.Header().Set("Content-Encoding", "gzip")
gz := gzip.NewWriter(w)
defer gz.Close()
gzw := gzipResponseWriter{Writer: gz, ResponseWriter: w}
http.ServeFile(gzw, r, fmt.Sprintf("%s/%s/%s", dataDir, jobID, fileName))
} else {
log.API.Warnf("API request to serve data is being made without gzip for file %s for jobId %s", fileName, jobID)
http.ServeFile(w, r, fmt.Sprintf("%s/%s/%s", dataDir, jobID, fileName))
}
}
/*
swagger:route GET /api/v1/metadata metadata metadata
# Get metadata
Returns metadata about the API.
Produces:
- application/fhir+json
Schemes: http, https
Responses:
200: MetadataResponse
*/
func Metadata(w http.ResponseWriter, r *http.Request) {
dt := time.Now()
scheme := "http"
if servicemux.IsHTTPS(r) {
scheme = "https"
}
host := fmt.Sprintf("%s://%s", scheme, r.Host)
statement := responseutils.CreateCapabilityStatement(dt, constants.Version, host)
responseutils.WriteCapabilityStatement(r.Context(), statement, w)
}
/*
swagger:route GET /_version metadata getVersion
# Get API version
Returns the version of the API that is currently running. Note that this endpoint is **not** prefixed with the base path (e.g. /api/v1).
Produces:
- application/json
Schemes: http, https
Responses:
200: VersionResponse
*/
func GetVersion(w http.ResponseWriter, r *http.Request) {
respMap := make(map[string]string)
respMap["version"] = constants.Version
respBytes, err := json.Marshal(respMap)
if err != nil {
log.API.Error(err)
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
return
}
w.Header().Set(constants.ContentType, constants.JsonContentType)
_, err = w.Write(respBytes)
if err != nil {
log.API.Error(err)
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
return
}
}
func HealthCheck(w http.ResponseWriter, r *http.Request) {
m := make(map[string]string)
dbStatus, dbOK := health.IsDatabaseOK()
ssasStatus, ssasOK := health.IsSsasOK()
m["database"] = dbStatus
m["ssas"] = ssasStatus
if !dbOK || !ssasOK {
w.WriteHeader(http.StatusBadGateway)
} else {
w.WriteHeader(http.StatusOK)
}
respJSON, err := json.Marshal(m)
if err != nil {
log.API.Error(err)
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
}
w.Header().Set(constants.ContentType, constants.JsonContentType)
_, err = w.Write(respJSON)
if err != nil {
log.API.Error(err)
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
}
}
/*
swagger:route GET /_auth metadata getAuthInfo
# Get details about auth
Returns the auth provider that is currently being used. Note that this endpoint is **not** prefixed with the base path (e.g. /api/v1).
Produces:
- application/json
Schemes: http, https
Responses:
200: AuthResponse
*/
func GetAuthInfo(w http.ResponseWriter, r *http.Request) {
respMap := make(map[string]string)
respMap["auth_provider"] = auth.GetProviderName()
version, err := auth.GetProvider().GetVersion()
if err == nil {
respMap["version"] = version
} else {
respMap["error message"] = err.Error()
}
respBytes, err := json.Marshal(respMap)
if err != nil {
logger := log.GetCtxLogger(r.Context())
logger.Error(err)
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
}
w.Header().Set(constants.ContentType, constants.JsonContentType)
_, err = w.Write(respBytes)
if err != nil {
logger := log.GetCtxLogger(r.Context())
logger.Error(err)
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
}
}