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

Allow DownloadWorkManager to have Bundle based requests along with url based ones. #1887

Merged
merged 17 commits into from
Apr 4, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Review comments: Cleaned up remotefhirservice interface
  • Loading branch information
aditya-07 committed Mar 29, 2023
commit 26974eac7c1573addf68f4de95380b2e51241189
11 changes: 7 additions & 4 deletions engine/src/main/java/com/google/android/fhir/FhirServices.kt
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import com.google.android.fhir.index.ResourceIndexer
import com.google.android.fhir.index.SearchParamDefinitionsProviderImpl
import com.google.android.fhir.sync.DataSource
import com.google.android.fhir.sync.remote.RemoteFhirService
import com.google.android.fhir.sync.remote.RetrofitBasedRemoteDataSource
import org.hl7.fhir.r4.model.SearchParameter
import timber.log.Timber

Expand Down Expand Up @@ -81,10 +82,12 @@ internal data class FhirServices(
val engine = FhirEngineImpl(database = db, context = context)
val remoteDataSource =
serverConfiguration?.let {
RemoteFhirService.builder(it.baseUrl, it.networkConfiguration)
.setAuthenticator(it.authenticator)
.setHttpLogger(it.httpLogger)
.build()
RetrofitBasedRemoteDataSource(
RemoteFhirService.builder(it.baseUrl, it.networkConfiguration)
.setAuthenticator(it.authenticator)
.setHttpLogger(it.httpLogger)
.build()
)
}
return FhirServices(
fhirEngine = engine,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ package com.google.android.fhir.sync.remote

import com.google.android.fhir.NetworkConfiguration
import com.google.android.fhir.sync.Authenticator
import com.google.android.fhir.sync.DataSource
import java.util.concurrent.TimeUnit
import okhttp3.Interceptor
import okhttp3.OkHttpClient
Expand All @@ -32,13 +31,11 @@ import retrofit2.http.POST
import retrofit2.http.Url

/** Interface to make http requests to the FHIR server. */
internal interface RemoteFhirService : DataSource {
internal interface RemoteFhirService {
aditya-07 marked this conversation as resolved.
Show resolved Hide resolved

@GET override suspend fun download(@Url path: String): Resource
@GET suspend fun get(@Url path: String): Resource

@POST(".") override suspend fun download(@Body bundle: Bundle): Resource

@POST(".") override suspend fun upload(@Body bundle: Bundle): Resource
@POST(".") suspend fun post(@Body bundle: Bundle): Resource

class Builder(
private val baseUrl: String,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* Copyright 2022 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
*
* https://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.remote

import com.google.android.fhir.sync.DataSource
import org.hl7.fhir.r4.model.Bundle

internal class RetrofitBasedRemoteDataSource(private val remoteFhirService: RemoteFhirService) :
aditya-07 marked this conversation as resolved.
Show resolved Hide resolved
aditya-07 marked this conversation as resolved.
Show resolved Hide resolved
DataSource {
override suspend fun download(path: String) = remoteFhirService.get(path)

override suspend fun download(bundle: Bundle) = remoteFhirService.post(bundle)

override suspend fun upload(bundle: Bundle) = remoteFhirService.post(bundle)
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import com.google.android.fhir.sync.DataSource
import com.google.android.fhir.sync.DownloadWorkManager
import com.google.android.fhir.sync.Request
import com.google.common.truth.Truth.assertThat
import java.net.SocketTimeoutException
import java.time.OffsetDateTime
import java.util.Date
import java.util.LinkedList
Expand Down Expand Up @@ -200,11 +201,11 @@ class TestingUtils constructor(private val iParser: IParser) {
}

override suspend fun download(bundle: Bundle): Resource {
throw Exception("Posting Download Bundle failed...")
throw SocketTimeoutException("Posting Download Bundle failed...")
}

override suspend fun upload(bundle: Bundle): Resource {
throw Exception("Posting Upload Bundle failed...")
throw SocketTimeoutException("Posting Upload Bundle failed...")
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,14 @@ import org.junit.runner.RunWith
import org.robolectric.RobolectricTestRunner

@RunWith(RobolectricTestRunner::class)
class RemoteFhirServiceTest {
class RetrofitBasedRemoteDataSourceTest {

private val mockWebServer = MockWebServer()

private val apiService by lazy {
RemoteFhirService.Builder(mockWebServer.url("/").toString(), NetworkConfiguration()).build()
private val retrofitBasedRemoteDataSource by lazy {
RetrofitBasedRemoteDataSource(
RemoteFhirService.Builder(mockWebServer.url("/").toString(), NetworkConfiguration()).build()
)
}

private val parser = FhirContext.forR4Cached().newJsonParser()
Expand Down Expand Up @@ -76,7 +78,7 @@ class RemoteFhirServiceTest {
}
mockWebServer.enqueue(mockResponse)

val result = apiService.download("Patient/patient-001")
val result = retrofitBasedRemoteDataSource.download("Patient/patient-001")

// No exception should occur
assertThat(result).isInstanceOf(Patient::class.java)
Expand Down Expand Up @@ -104,7 +106,7 @@ class RemoteFhirServiceTest {
type = Bundle.BundleType.TRANSACTION
}

val result = apiService.upload(request)
val result = retrofitBasedRemoteDataSource.upload(request)

// No exception has occurred
assertThat(result).isInstanceOf(Bundle::class.java)
Expand Down Expand Up @@ -132,7 +134,7 @@ class RemoteFhirServiceTest {
type = Bundle.BundleType.TRANSACTION
}

val result = apiService.upload(request)
val result = retrofitBasedRemoteDataSource.upload(request)

assertThat(result).isInstanceOf(Bundle::class.java)
assertThat((result as Bundle).type).isEqualTo(Bundle.BundleType.TRANSACTIONRESPONSE)
Expand Down