Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introducing Upload Strategies #2171

Merged
merged 17 commits into from
Sep 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
/*
* Copyright 2023 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.google.android.fhir.sync.upload

import com.google.android.fhir.sync.upload.patch.PatchGeneratorMode
import com.google.android.fhir.sync.upload.request.UploadRequestGeneratorMode
import org.hl7.fhir.r4.model.Bundle
import org.hl7.fhir.r4.model.codesystems.HttpVerb

anchita-g marked this conversation as resolved.
Show resolved Hide resolved
/**
* Strategy to define how to upload the [LocalChange]s to the FHIR server.
*
* Each strategy comprises of deciding appropriate modes for [LocalChangeFetcher],
* [PatchGeneratorMode], [UploadRequestGeneratorMode]. The strategies mentioned here are exhaustive
* as the different modes for the components mentioned above can only be used together in some
* specific ways.
*/
sealed class UploadStrategy
private constructor(
internal val localChangesFetchMode: LocalChangesFetchMode,
internal val patchGeneratorMode: PatchGeneratorMode,
internal val requestGeneratorMode: UploadRequestGeneratorMode,
) {
object SingleChangePut :
UploadStrategy(
LocalChangesFetchMode.EarliestChange,
PatchGeneratorMode.PerChange,
UploadRequestGeneratorMode.UrlRequest(HttpVerb.PUT, HttpVerb.PATCH),
)

object SingleChangePost :
UploadStrategy(
LocalChangesFetchMode.EarliestChange,
PatchGeneratorMode.PerChange,
UploadRequestGeneratorMode.UrlRequest(HttpVerb.POST, HttpVerb.PATCH),
)

object SingleResourcePut :
UploadStrategy(
LocalChangesFetchMode.PerResource,
PatchGeneratorMode.PerResource,
UploadRequestGeneratorMode.UrlRequest(HttpVerb.PUT, HttpVerb.PATCH),
)

object SingleResourcePost :
UploadStrategy(
LocalChangesFetchMode.PerResource,
PatchGeneratorMode.PerResource,
UploadRequestGeneratorMode.UrlRequest(HttpVerb.POST, HttpVerb.PATCH),
)

object AllChangesBundlePut :
UploadStrategy(
LocalChangesFetchMode.AllChanges,
PatchGeneratorMode.PerChange,
UploadRequestGeneratorMode.BundleRequest(Bundle.HTTPVerb.PUT, Bundle.HTTPVerb.PATCH),
)

object AllChangesBundlePost :
UploadStrategy(
LocalChangesFetchMode.AllChanges,
PatchGeneratorMode.PerChange,
UploadRequestGeneratorMode.BundleRequest(Bundle.HTTPVerb.POST, Bundle.HTTPVerb.PATCH),
)

object AllChangesSquashedBundlePut :
UploadStrategy(
LocalChangesFetchMode.AllChanges,
PatchGeneratorMode.PerResource,
UploadRequestGeneratorMode.BundleRequest(Bundle.HTTPVerb.PUT, Bundle.HTTPVerb.PATCH),
)

object AllChangesSquashedBundlePost :
UploadStrategy(
LocalChangesFetchMode.AllChanges,
PatchGeneratorMode.PerResource,
UploadRequestGeneratorMode.BundleRequest(Bundle.HTTPVerb.POST, Bundle.HTTPVerb.PATCH),
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,22 @@ internal interface PatchGenerator {
*/
fun generate(localChanges: List<LocalChange>): List<Patch>
}

internal object PatchGeneratorFactory {
fun byMode(
mode: PatchGeneratorMode,
): PatchGenerator =
when (mode) {
is PatchGeneratorMode.PerChange -> PerChangePatchGenerator
is PatchGeneratorMode.PerResource -> PerResourcePatchGenerator
}
}

/**
* Mode to decide the type of [PatchGenerator] that needs to be used to upload the [LocalChange]s
*/
internal sealed class PatchGeneratorMode {
object PerResource : PatchGeneratorMode()

object PerChange : PatchGeneratorMode()
}
Loading