Skip to content
Pascal Pfiffner edited this page May 3, 2016 · 6 revisions

The embedded Swift-FHIR library contains classes for all base FHIR profiles. All classes are documented in the technical documentation, their inheritance graph usually is:

Instantiation

You have several options to instantiate FHIR elements and resources. For now only JSON serialization is supported.

These code examples assume a valid JSON file named Contract.json to be present in the App bundle. Error checking is omitted for readability purposes.

Hardcoded

let url = NSBundle.mainBundle().URLForResource("Contract", withExtension: "json")!
let data = NSData(contentsOfURL: url)!
let json = NSJSONSerialization.JSONObjectWithData(data, options: nil, error: nil) as! FHIRJSON

let contract = Contract(json: json)

Factory

let url = NSBundle.mainBundle().URLForResource("Contract", withExtension: "json")!
let data = NSData(contentsOfURL: url)!
let json = NSJSONSerialization.JSONObjectWithData(data, options: nil, error: nil) as! FHIRJSON

let contract = FHIRElement.instantiateFrom(json, owner: nil) as? Contract
// equal to
let type = json["resourceType"] as! String
let contract = FHIRElement.factory(type, json: json, owner: nil)

NSBundle Extension

This uses the factory methods shown above.

let contract = try NSBundle.mainBundle().fhir_bundledResource("Contract")

REST API

See the read interaction below.

Usage

The FHIR REST API defines a set of logical interactions. These are all (to be) implemented as a class extension on Resource, at instance- or class-level, or are handled by the Server instance the client is hanging on to.

Currently, all server interactions use the JSON format by using the Server's performRequest* methods.

Instance Level

read

Patient.read({patient-id}, server: smart.server) { resource, error in
    // check error
    // `resource` is a "Patient" instance on success
}

vread

TBD

update

let encounter = Encounter(...)      // must have _server assigned
encounter.update() { error in
    // check error
}

TODO: PATCH operation

delete

let encounter = Encounter(...)      // must have _server assigned
encounter.delete() { error in
    // check error
}

history

TBD

Type Level

create

The create method will issue a create call, issuing a POST with the resource's contents to the server. It will add the Prefer header with return=minimal and will ignore body content if the server still returns it. The resource's id and meta will be updated with data found in response headers.

let order = MedicationOrder(...)
order.create(server: smart.server) { error in
    // check error
}

The createAndReturn method also issues a create call, a POST with the resource's contents. It will add the Prefer header with return=representation and will, should the server ignore that request, issue a read to retrieve resource data before calling the callback.

let order = MedicationOrder(...)
order.createAndReturn(server: smart.server) { error in
    // check error
    // resource has been populated with data fresh from the server
}

search

See the search page for detailed instructions. A basic example:

MedicationPrescription.search(["patient": id])
.perform(smart.server) { bundle, error in
    // check error
    // `bundle` is a Bundle instance
}

history

TBD

System Level

conformance

The Server instance handles working with the conformance statement. You don't usually need to work with conformance yourself, but you still can if you desire to do so. Take a look at the Server and Conformance class documentation for details.

transaction

TBD

history

TBD

search

See the search page.