-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathLenientClient.swift
74 lines (60 loc) · 2.37 KB
/
LenientClient.swift
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
//
// LenientClient.swift
// CareGuide
//
// Created by Dave Carlson on 1/14/19.
// Copyright © 2019 Clinical Cloud Solutions, LLC. All rights reserved.
//
import Foundation
import SMART
/**
A client that defaults to lenient validation, where JSON validation errors are tolerated when receiving a response,
i.e. don't throw upon instantiation, use what's provided.
*/
public class LenientClient: Client {
/// Which options to apply.
open var options: FHIRRequestOption = .lenient
public convenience init(baseURL: URL, settings: OAuth2JSON) {
var sett = settings
if let redirect = settings["redirect"] as? String {
sett["redirect_uris"] = [redirect]
}
if nil == settings["title"] {
sett["title"] = "SMART"
}
let srv = LenientServer(baseURL: baseURL, auth: sett)
self.init(server: srv)
}
/**
Request a JSON resource at the given path from the client's server.
- parameter path: The path relative to the server's base URL to request
- parameter callback: The callback to execute once the request finishes
*/
override public func getJSON(at path: String, callback: @escaping ((_ response: FHIRServerJSONResponse) -> Void)) {
let handler = FHIRJSONRequestHandler(.GET)
handler.options = options
server.performRequest(against: path, handler: handler, callback: { response in
callback(response as! FHIRServerJSONResponse)
})
}
}
/**
A server that defaults to lenient validation, where JSON validation errors are tolerated when receiving a response,
i.e. don't throw upon instantiation, use what's provided.
*/
public class LenientServer: Server {
/// Which options to apply.
open var options: FHIRRequestOption = .lenient
/**
The server can return the appropriate request handler for the type and resource combination.
Request handlers are responsible for constructing an URLRequest that correctly performs the desired REST interaction.
- parameter method: The request method (GET, PUT, POST or DELETE)
- parameter resource: The resource to be involved in the request, if any
- returns: An appropriate `FHIRRequestHandler`, for example a _FHIRJSONRequestHandler_ if sending and receiving JSON
*/
override public func handlerForRequest(withMethod method: FHIRRequestMethod, resource: Resource?) -> FHIRRequestHandler? {
let handler = FHIRJSONRequestHandler(method, resource: resource)
handler.options = options
return handler
}
}