Skip to content

Commit

Permalink
More Swift 3 adaptations
Browse files Browse the repository at this point in the history
  • Loading branch information
p2 committed Sep 16, 2016
1 parent 0712334 commit 903b62b
Show file tree
Hide file tree
Showing 115 changed files with 661 additions and 616 deletions.
4 changes: 2 additions & 2 deletions Sources/Client/DomainResource+Containment.swift
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ extension DomainResource {
- parameter display: The string that will become the reference's `display`
- returns: A `Reference` instance pointing to the contained resource (as "#id")
*/
public func contain(resource: Resource, withDisplay display: String? = nil) throws -> Reference {
open func contain(resource: Resource, withDisplay display: String? = nil) throws -> Reference {
guard resource !== self else {
throw FHIRError.resourceCannotContainItself
}
Expand Down Expand Up @@ -98,7 +98,7 @@ extension DomainResource {
- parameter display: The string that will become the reference's `display`
- returns: A `Reference`, ready for use
*/
public func reference(resource: Resource, withDisplay display: String? = nil) throws -> Reference {
open func reference(resource: Resource, withDisplay display: String? = nil) throws -> Reference {
let ref = Reference(json: nil, owner: self)
ref.display = display

Expand Down
61 changes: 34 additions & 27 deletions Sources/Client/FHIROpenServer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ It knows its base URL, can fetch and hold on to the conformance statement and pe
These methods are of interest to you when you create a subclass:
- `handlerForRequest(ofType:resource:)`: what kind of handler your server wants to use. Returns `FHIRServerJSONRequestHandler`.
- `configurableRequest(forURL:)`: the SMART framework returns a request that already has an Authorization headers set, if needed.
- `handlerForRequest(withMethod:resource:)`: what kind of handler your server wants to use. Returns `FHIRServerJSONRequestHandler`.
- `configurableRequest(for:)`: the SMART framework returns a request that already has an Authorization headers set, if needed.
*/
open class FHIROpenServer: FHIRServer {

Expand Down Expand Up @@ -50,8 +50,10 @@ open class FHIROpenServer: FHIRServer {
This method simply creates an absolute URL from the receiver's `baseURL` and the given path.
A chance for subclasses to mess with URL generation if needed.
- parameter for: The path in the absolute URL
*/
open func absoluteURLForPath(_ path: String, handler: FHIRServerRequestHandler) -> URL? {
open func absoluteURL(for path: String, handler: FHIRServerRequestHandler) -> URL? {
return URL(string: path, relativeTo: baseURL)
}

Expand All @@ -60,13 +62,19 @@ open class FHIROpenServer: FHIRServer {

/**
Perform a request of given type against the given path with the (optional) given resource and headers.
- parameter method: The HTTP method type of the request
- parameter path: The relative path on the server to be interacting against
- parameter resource: The resource to be involved in the request, if any
- parameter additonalHeaders: The headers to set on the request
- parameter callback: A callback, likely called asynchronously, returning a response instance
*/
open func performRequest(ofType type: FHIRRequestType, path: String, resource: Resource?, additionalHeaders: FHIRRequestHeaders? = nil, callback: @escaping ((_ response: FHIRServerResponse) -> Void)) {
if let handler = handlerForRequest(type, resource: resource, headers: additionalHeaders) {
performRequest(againstPath: path, handler: handler, callback: callback)
open func performRequest(_ method: FHIRRequestMethod, path: String, resource: Resource?, additionalHeaders: FHIRRequestHeaders? = nil, callback: @escaping ((FHIRServerResponse) -> Void)) {
if let handler = handlerForRequest(withMethod: method, resource: resource, headers: additionalHeaders) {
performRequest(against: path, handler: handler, callback: callback)
}
else {
let res = FHIRServerRequestHandler.noneAvailable(forType: type)
let res = FHIRServerRequestHandler.noneAvailable(for: method)
callback(res)
}
}
Expand All @@ -79,12 +87,12 @@ open class FHIROpenServer: FHIRServer {
Request handlers are responsible for constructing an URLRequest that correctly performs the desired REST interaction.
- parameter ofType: The type of the request (GET, PUT, POST or DELETE)
- 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 `FHIRServerRequestHandler`, for example a _FHIRServerJSONRequestHandler_ if sending and receiving JSON
- returns: An appropriate `FHIRServerRequestHandler`, for example a _FHIRServerJSONRequestHandler_ if sending and receiving JSON
*/
open func handlerForRequest(_ ofType: FHIRRequestType, resource: Resource?, headers: FHIRRequestHeaders? = nil) -> FHIRServerRequestHandler? {
let handler = FHIRServerJSONRequestHandler(ofType, resource: resource)
open func handlerForRequest(withMethod method: FHIRRequestMethod, resource: Resource?, headers: FHIRRequestHeaders? = nil) -> FHIRServerRequestHandler? {
let handler = FHIRServerJSONRequestHandler(method, resource: resource)
if let headers = headers {
handler.add(headers: headers)
}
Expand All @@ -94,38 +102,37 @@ open class FHIROpenServer: FHIRServer {
/**
Pre-prepare a mutable URLRequest that the handler subsequently prepares and performs.
- parameter forURL: The url to use for the request
- parameter url: The url to use for the request
*/
open func configurableRequest(forURL url: URL) -> URLRequest {
open func configurableRequest(for url: URL) -> URLRequest {
return URLRequest(url: url)
}


/**
Method to execute a request against a given relative URL with a given request/response handler.
- parameter againstPath: The path, relative to the server's base; may include URL query and URL fragment (!)
- parameter handler: The RequestHandler that prepares the request and processes the response
- parameter callback: The callback to execute; NOT guaranteed to be performed on the main thread!
- parameter path: The path, relative to the server's base; may include URL query and URL fragment (!)
- parameter handler: The RequestHandler that prepares the request and processes the response
- parameter callback: The callback to execute; NOT guaranteed to be performed on the main thread!
*/
open func performRequest<R: FHIRServerRequestHandler>(againstPath path: String, handler: R, callback: @escaping ((_ response: FHIRServerResponse) -> Void)) {
guard let url = absoluteURLForPath(path, handler: handler) else {
open func performRequest<R: FHIRServerRequestHandler>(against path: String, handler: R, callback: @escaping ((FHIRServerResponse) -> Void)) {
guard let url = absoluteURL(for: path, handler: handler) else {
let res = handler.notSent("Failed to parse path «\(path)» relative to server base URL")
callback(res)
return
}
performRequest(withURL: url, handler: handler, callback: callback)
performRequest(on: url, handler: handler, callback: callback)
}

/**
Method to execute a request against a given absolute URL with a given request/response handler.
- parameter withURL: The full URL; may include query parts and fragment (!)
- parameter url: The full URL; may include query parts and fragment (!)
- parameter handler: The RequestHandler that prepares the request and processes the response
- parameter callback: The callback to execute; NOT guaranteed to be performed on the main thread!
*/
open func performRequest<R: FHIRServerRequestHandler>(withURL url: URL, handler: R, callback: @escaping ((_ response: FHIRServerResponse) -> Void)) {
var request = configurableRequest(forURL: url)
open func performRequest<R: FHIRServerRequestHandler>(on url: URL, handler: R, callback: @escaping ((FHIRServerResponse) -> Void)) {
var request = configurableRequest(for: url)
do {
try handler.prepare(request: &request)
self.performPreparedRequest(request, handler: handler, callback: callback)
Expand All @@ -145,7 +152,7 @@ open class FHIROpenServer: FHIRServer {
- parameter handler: The RequestHandler that prepares the request and processes the response
- parameter callback: The callback to execute; NOT guaranteed to be performed on the main thread!
*/
open func performPreparedRequest<R: FHIRServerRequestHandler>(_ request: URLRequest, handler: R, callback: @escaping ((_ response: FHIRServerResponse) -> Void)) {
open func performPreparedRequest<R: FHIRServerRequestHandler>(_ request: URLRequest, handler: R, callback: @escaping ((FHIRServerResponse) -> Void)) {
performPreparedRequest(request, withSession: URLSession(), handler: handler, callback: callback)
}

Expand All @@ -157,9 +164,9 @@ open class FHIROpenServer: FHIRServer {
- parameter handler: The RequestHandler that prepares the request and processes the response
- parameter callback: The callback to execute; NOT guaranteed to be performed on the main thread!
*/
open func performPreparedRequest<R: FHIRServerRequestHandler>(_ request: URLRequest, withSession session: URLSession, handler: R, callback: @escaping ((_ response: FHIRServerResponse) -> Void)) {
open func performPreparedRequest<R: FHIRServerRequestHandler>(_ request: URLRequest, withSession session: URLSession, handler: R, callback: @escaping ((FHIRServerResponse) -> Void)) {
let task = session.dataTask(with: request) { data, response, error in
let res = handler.response(response, data: data, error: error as NSError?)
let res = handler.response(with: response, data: data, error: error)
callback(res)
}
task.resume()
Expand Down Expand Up @@ -293,7 +300,7 @@ open class FHIROpenServer: FHIRServer {
- parameter operation: The operation instance to perform
- parameter callback: The callback to call when the request ends (success or failure)
*/
open func perform(_ operation: FHIROperation, callback: @escaping ((_ response: FHIRServerResponse) -> Void)) {
open func perform(_ operation: FHIROperation, callback: @escaping ((FHIRServerResponse) -> Void)) {
self.operation(operation.name) { definition in
if let def = definition {
do {
Expand Down
12 changes: 6 additions & 6 deletions Sources/Client/FHIROperation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,16 @@ import Models


/**
The context an operation is to be performed against.
*/
The context an operation is to be performed against.
*/
public enum FHIROperationContext {
case none, system, resourceType, instance
}


/**
Named operations to be performed against a FHIR REST endpoint.
*/
Named operations to be performed against a FHIR REST endpoint.
*/
open class FHIROperation: CustomStringConvertible {

/// The name of the operation.
Expand Down Expand Up @@ -116,7 +116,7 @@ open class FHIROperation: CustomStringConvertible {
if "in" == param.use {

// have the parameter, validate it
if let _ = inParams?[param.name!] {
if nil != inParams?[param.name!] {
leftover.removeValue(forKey: param.name!)

// TODO: actually validate!
Expand Down Expand Up @@ -165,7 +165,7 @@ open class FHIROperation: CustomStringConvertible {
*/
open func perform(onServer server: FHIRServer, callback: @escaping ((_ response: FHIRServerResponse) -> Void)) throws {
let path = try serverPath()
server.performRequest(ofType: .GET, path: path, resource: nil, additionalHeaders: nil, callback: callback)
server.performRequest(.GET, path: path, resource: nil, additionalHeaders: nil, callback: callback)
}


Expand Down
17 changes: 11 additions & 6 deletions Sources/Client/FHIRSearch.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,17 @@
import Foundation
#if !NO_MODEL_IMPORT
import Models
public typealias FHIRSearchBundleErrorCallback = ((Models.Bundle?, FHIRError?) -> Void)
#else
public typealias FHIRSearchBundleErrorCallback = ((Bundle?, FHIRError?) -> Void)
#endif


/**
Instances of this class can perform searches on a server.
TODO: needs a refresh!!
Searches are instantiated from MongoDB-like query constructs, like:
let srch = Patient.search(["address": "Boston", "gender": "male", "given": ["$exact": "Willis"]])
Expand Down Expand Up @@ -101,9 +106,9 @@ open class FHIRSearch
Calling this method will always restart search, not fetch subsequent pages.
- parameter server: The FHIRServer instance on which to perform the search
- parameter callback: The callback, receives the response Bundle or an NSError message describing what went wrong
- parameter callback: The callback, receives the response Bundle or an Error message describing what went wrong
*/
open func perform(_ server: FHIRServer, callback: @escaping ((_ bundle: Bundle?, _ error: FHIRError?) -> Void)) {
open func perform(_ server: FHIRServer, callback: @escaping FHIRSearchBundleErrorCallback) {
if nil == profileType {
callback(nil, FHIRError.searchResourceTypeNotDefined)
return
Expand All @@ -118,9 +123,9 @@ open class FHIRSearch
with no bundle and no error.
- parameter server: The FHIRServer instance on which to perform the search
- parameter callback: The callback, receives the response Bundle or an NSError message describing what went wrong
- parameter callback: The callback, receives the response Bundle or an Error message describing what went wrong
*/
open func nextPage(_ server: FHIRServer, callback: @escaping ((_ bundle: Bundle?, _ error: FHIRError?) -> Void)) {
open func nextPage(_ server: FHIRServer, callback: @escaping FHIRSearchBundleErrorCallback) {
if let next = nextPageURL?.absoluteString {
performSearch(server, queryPath: next, callback: callback)
}
Expand All @@ -129,14 +134,14 @@ open class FHIRSearch
}
}

func performSearch(_ server: FHIRServer, queryPath: String, callback: @escaping ((_ bundle: Bundle?, _ error: FHIRError?) -> Void)) {
func performSearch(_ server: FHIRServer, queryPath: String, callback: @escaping FHIRSearchBundleErrorCallback) {
if busy {
callback(nil, nil)
return
}

busy = true
server.performRequest(ofType: .GET, path: queryPath, resource: nil, additionalHeaders: nil) { response in
server.performRequest(.GET, path: queryPath, resource: nil, additionalHeaders: nil) { response in
self.busy = false

if let error = response.error {
Expand Down
Loading

0 comments on commit 903b62b

Please sign in to comment.