getResource<T> method

Future<Resource> getResource<T>(
  1. String baseUri,
  2. String path
)

Low level call to get a resource from the FHIR server with raw URL path Note: it is recommended to use the higher level calls like search etc.

Implementation

Future<Resource> getResource<T>(String baseUri, String path) async {
  try {
    final uri = '$baseUri$path';

    final response = await this.get(
      Uri.parse(uri),
      headers: {'Content-Type': 'application/json'},
    );

    try {
      final jsonValue = jsonValueDecode(response.body);

      if (jsonValue is JsonObject) {
        return Resource.fromJson(jsonValue);
      } else {
        return OperationOutcome<T>.error(
          status: NarrativeStatus.empty,
          details:
              'Expected a JSON object, but got a ${jsonValue.runtimeType}',
        );
      }

      // ignore: avoid_catches_without_on_clauses
    } catch (e) {
      return OperationOutcome<T>.error(
        status:
            NarrativeStatus.empty,
        details: e.toString(),
      );
    }
    // ignore: avoid_catches_without_on_clauses
  } catch (e) {
    return OperationOutcome<T>.error(
      status: NarrativeStatus.empty,
      details: e.toString(),
    );
  }
}