toFhirHttpRequest method

FhirHttpRequest toFhirHttpRequest({
  1. Map<String, String>? headers,
})

FhirHttpRequest In order to send a these via the atProtocol, we need to change them into something that can be easily serialized or deserialized.

Implementation

FhirHttpRequest toFhirHttpRequest({Map<String, String>? headers}) => map(
      read: (FhirReadRequest request) => FhirHttpRequest.fromFhirRequest(
        type: RestfulRequest.get_,
        url: uri(parameters: parameters),
        headers: headers,
        accept: accept,
        mimeType: mimeType,
      ),
      vRead: (FhirVReadRequest request) => FhirHttpRequest.fromFhirRequest(
        type: RestfulRequest.get_,
        url: uri(parameters: parameters),
        headers: headers,
        accept: accept,
        mimeType: mimeType,
      ),
      update: (FhirUpdateRequest request) => FhirHttpRequest.fromFhirRequest(
        type: RestfulRequest.put_,
        url: uri(parameters: parameters),
        headers: headers,
        accept: accept,
        resource: (this as FhirUpdateRequest).resource.toJson(),
        mimeType: mimeType,
      ),
      patch: (FhirPatchRequest request) => FhirHttpRequest.fromFhirRequest(
        type: RestfulRequest.patch_,
        url: uri(parameters: parameters),
        headers: headers,
        accept: accept,
        resource: (this as FhirPatchRequest).resource.toJson(),
        mimeType: mimeType,
      ),
      delete: (FhirDeleteRequest request) => FhirHttpRequest.fromFhirRequest(
        type: RestfulRequest.delete_,
        url: uri(parameters: parameters),
        headers: headers,
        accept: accept,
        mimeType: mimeType,
      ),
      create: (FhirCreateRequest request) => FhirHttpRequest.fromFhirRequest(
        type: RestfulRequest.post_,
        url: uri(parameters: parameters),
        headers: headers,
        accept: accept,
        resource: (this as FhirCreateRequest).resource.toJson(),
        mimeType: mimeType,
      ),
      search: (FhirSearchRequest request) => FhirHttpRequest.fromFhirRequest(
        type: (this as FhirSearchRequest).usePost
            ? RestfulRequest.post_
            : RestfulRequest.get_,
        url: (this as FhirSearchRequest).usePost
            ? url
            : uri(parameters: parameters),
        headers: headers,
        accept: accept,
        formData: (this as FhirSearchRequest).usePost
            ? (this as FhirSearchRequest).formData(parameters: parameters)
            : null,
        mimeType: mimeType,
      ),
      searchAll: (FhirSearchAllRequest request) =>
          FhirHttpRequest.fromFhirRequest(
        type: RestfulRequest.get_,
        url: uri(parameters: parameters),
        headers: headers,
        accept: accept,
        mimeType: mimeType,
      ),
      capabilities: (FhirCapabilitiesRequest request) =>
          FhirHttpRequest.fromFhirRequest(
        type: RestfulRequest.get_,
        url: uri(parameters: parameters),
        headers: headers,
        accept: accept,
        mimeType: mimeType,
      ),
      transaction: (FhirTransactionRequest request) {
        if ((this as FhirTransactionRequest).bundle.type.toString() !=
            'transaction') {
          throw const FormatException(
              'A Transaction request was made, but no Bundle was included.');
        }
        if ((this as FhirTransactionRequest).bundle.entry != null) {
          for (final BundleEntry entry
              in (this as FhirTransactionRequest).bundle.entry!) {
            if (entry.request == null) {
              throw const FormatException(
                  'Each bundle entry requires a request, but at least one of '
                  'the entries in this bundle is missing a request.');
            } else if (entry.request?.method == null) {
              throw const FormatException(
                  'Each bundle entry request needs a method type specified, but'
                  ' at least one entry in this bundle is missing a method');
            }
          }
        }
        return FhirHttpRequest.fromFhirRequest(
          type: RestfulRequest.post_,
          url: uri(),
          headers: headers,
          accept: accept,
          resource: (this as FhirTransactionRequest).bundle.toJson(),
          mimeType: mimeType,
        );
      },
      batch: (FhirBatchRequest request) {
        if ((this as FhirBatchRequest).bundle.type.toString() != 'batch') {
          throw const FormatException(
              'A Batch request was made, but the included Bundle is not a'
              ' batch type.');
        }
        if ((this as FhirBatchRequest).bundle.entry != null) {
          for (final BundleEntry entry
              in (this as FhirBatchRequest).bundle.entry!) {
            if (entry.request == null) {
              throw const FormatException(
                  'Each bundle entry requires a request, but at least one of '
                  'the entries in this bundle is missing a request.');
            } else if (entry.request?.method == null) {
              throw const FormatException(
                  'Each bundle entry request needs a method type specified, but'
                  ' at least one entry in this bundle is missing a method');
            }
          }
        }
        return FhirHttpRequest.fromFhirRequest(
          type: RestfulRequest.post_,
          url: uri(),
          headers: headers,
          accept: accept,
          resource: (this as FhirBatchRequest).bundle.toJson(),
          mimeType: mimeType,
        );
      },
      history: (FhirHistoryRequest request) {
        final List<String> parameterList = <String>[];
        final List<String> hxList = _hxParameters(
            (this as FhirHistoryRequest).count,
            (this as FhirHistoryRequest).since,
            (this as FhirHistoryRequest).at,
            (this as FhirHistoryRequest).reference);

        if (hxList.isNotEmpty) {
          parameterList.addAll(hxList);
        }
        if (parameters.isNotEmpty) {
          parameterList.addAll(parameters);
        }

        return FhirHttpRequest.fromFhirRequest(
          type: RestfulRequest.get_,
          url: uri(parameters: parameterList),
          headers: headers,
          accept: accept,
          mimeType: mimeType,
        );
      },
      historyType: (FhirHistoryTypeRequest request) {
        final List<String> parameterList = <String>[];
        final List<String> hxList = _hxParameters(
            (this as FhirHistoryTypeRequest).count,
            (this as FhirHistoryTypeRequest).since,
            (this as FhirHistoryTypeRequest).at,
            (this as FhirHistoryTypeRequest).reference);

        if (hxList.isNotEmpty) {
          parameterList.addAll(hxList);
        }
        if (parameters.isNotEmpty) {
          parameterList.addAll(parameters);
        }

        return FhirHttpRequest.fromFhirRequest(
          type: RestfulRequest.get_,
          url: uri(parameters: parameterList),
          headers: headers,
          accept: accept,
          mimeType: mimeType,
        );
      },
      historyAll: (FhirHistoryAllRequest request) {
        final List<String> parameterList = <String>[];
        final List<String> hxList = _hxParameters(
            (this as FhirHistoryAllRequest).count,
            (this as FhirHistoryAllRequest).since,
            (this as FhirHistoryAllRequest).at,
            (this as FhirHistoryAllRequest).reference);

        if (hxList.isNotEmpty) {
          parameterList.addAll(hxList);
        }
        if (parameters.isNotEmpty) {
          parameterList.addAll(parameters);
        }

        return FhirHttpRequest.fromFhirRequest(
          type: RestfulRequest.get_,
          url: uri(parameters: parameterList),
          headers: headers,
          accept: accept,
          mimeType: mimeType,
        );
      },
      operation: (FhirOperationRequest request) {
        return FhirHttpRequest.fromFhirRequest(
          type: (this as FhirOperationRequest).usePost ||
                  (this as FhirOperationRequest).fhirParameter != null
              ? RestfulRequest.post_
              : RestfulRequest.get_,
          url: (this as FhirOperationRequest).usePost ||
                  (this as FhirOperationRequest).fhirParameter != null
              ? url
              : uri(parameters: parameters),
          headers: headers,
          accept: accept,
          resource: ((this as FhirOperationRequest).usePost &&
                      !(this as FhirOperationRequest).useFormData) ||
                  (this as FhirOperationRequest).fhirParameter != null
              ? (this as FhirOperationRequest).fhirParameter!.toJson()
              : null,
          formData: (this as FhirOperationRequest).usePost &&
                  (this as FhirOperationRequest).useFormData
              ? (this as FhirOperationRequest)
                  .formData(parameters: parameters)
              : null,
          mimeType: mimeType,
        );
      },
    );