Client

open class Client

A client instance handles authentication and connection to a SMART on FHIR resource server.

Create an instance of this class, then hold on to it for all your interactions with the SMART server:

import SMART

let smart = Client(
    baseURL: "https://fhir-api-dstu2.smarthealthit.org",
    settings: [
        //"client_id": "my_mobile_app",       // if you have one; otherwise uses dyn reg
        "redirect": "smartapp://callback",    // must be registered in Info.plist
    ]
)

There are many other options that you can pass to settings, take a look at init(baseURL:settings:). Also see our programming guide for more information.

  • The server this client connects to.

    Declaration

    Swift

    public final let server: Server
  • Set the authorize type you want, e.g. to use a built in web view for authentication and patient selection.

    Declaration

    Swift

    open var authProperties = SMARTAuthProperties()
  • Designated initializer.

    Declaration

    Swift

    public init(server: Server)

    Parameters

    server

    The server instance this client manages

  • Use this initializer with the appropriate server/auth settings. You can use:

    • client_id: If you have a client-id; otherwise, if the server supports OAuth2 dynamic client registration, will register itself
    • redirect: After-auth redirect URL (string). Must be registered on the server and in your app’s Info.plist (URL handler)
    • redirect_uris: Array of redirect URL (strings); will be created if you supply redirect
    • scope: Authorization scope, defaults to user/\*.* openid profile plus launch scope, if needed
    • authorize_uri: Optional; if present will NOT use the authorization endpoints defined in the server’s metadata. Know what you do!
    • token_uri: Optional; if present will NOT use the authorization endpoints defined in the server’s metadata. Know what you do!
    • authorize_type: Optional; inferred to be authorization_code or implicit. Can also be client_credentials for a 2-legged OAuth2 flow.
    • client_name: OPTIONAL, if you use dynamic client registration, this is the name of your app
    • logo_uri: OPTIONAL, if you use dynamic client registration, a URL to the icon of your app

    The settings are forwarded to the OAuth2 framework, so you can use any of the settings supported during authorization if you know what you’re doing: init(settings:) from http://p2.github.io/OAuth2/Classes/OAuth2.html .

    Declaration

    Swift

    public convenience init(baseURL: URL, settings: OAuth2JSON)

    Parameters

    baseURL

    The server’s base URL

    settings

    Client settings, mostly concerning authorization

  • Executes the callback immediately if the server is ready to perform requests. Otherwise performs necessary setup operations and requests, like retrieving the conformance statement.

    Declaration

    Swift

    open func ready(callback: @escaping (Error?) -> ())

    Parameters

    callback

    The callback to call if the server is ready or an error has occurred

  • Call this to start the authorization process. Implicitly calls ready, so no need to call it yourself.

    If you use the OS browser you will need to intercept the OAuth redirect in your app delegate and call didRedirect yourself. See the instructions for more detail.

    Declaration

    Swift

    open func authorize(callback: @escaping (_ patient: Patient?, _ error: Error?) -> ())

    Parameters

    callback

    The callback that is called when authorization finishes, with a patient resource (if launch/patient was specified or an error

  • Will return true while the client is waiting for the authorization callback.

    Declaration

    Swift

    open var awaitingAuthCallback: Bool
  • Call this with the redirect URL when intercepting the redirect callback in the app delegate.

    Declaration

    Swift

    open func didRedirect(to url: URL) -> Bool

    Parameters

    url

    The URL that was redirected to

  • Stops any request currently in progress.

    Declaration

    Swift

    open func abort()
  • Resets state and authorization data.

    Declaration

    Swift

    open func reset()
  • Throws away local client registration data.

    Declaration

    Swift

    open func forgetClientRegistration()
  • Request a JSON resource at the given path from the client’s server.

    Declaration

    Swift

    open func getJSON(at path: String, callback: @escaping ((_ response: FHIRServerJSONResponse) -> Void))

    Parameters

    path

    The path relative to the server’s base URL to request

    callback

    The callback to execute once the request finishes

  • Plain NSData request against the given full URL.

    If the server needs authentication and the URL is not in the receiver’s baseURL, this is probably going to fail. You usually use this method if a resource has attachments that live on the same server, e.g. Patient.photo.url.

    Declaration

    Swift

    open func getData(from url: URL, accept: String, callback: @escaping ((_ response: FHIRServerResponse) -> Void))

    Parameters

    url

    The URL to read data from

    accept

    The accept header to send along

    callback

    Callback called once the response comes back