Skip to content

Commit

Permalink
Rename classes
Browse files Browse the repository at this point in the history
  • Loading branch information
hichamboushaba committed Dec 3, 2024
1 parent d71445d commit 07950ea
Show file tree
Hide file tree
Showing 10 changed files with 63 additions and 63 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package com.woocommerce.android.apifaker

import com.woocommerce.android.apifaker.db.EndpointDao
import com.woocommerce.android.apifaker.models.EndpointType
import com.woocommerce.android.apifaker.models.FakeResponse
import com.woocommerce.android.apifaker.models.ApiType
import com.woocommerce.android.apifaker.models.Response
import com.woocommerce.android.apifaker.util.JSONObjectProvider
import okhttp3.Request
import okio.Buffer
Expand All @@ -15,7 +15,7 @@ internal class EndpointProcessor @Inject constructor(
private val endpointDao: EndpointDao,
private val jsonObjectProvider: JSONObjectProvider
) {
fun fakeRequestIfNeeded(request: Request): FakeResponse? {
fun fakeRequestIfNeeded(request: Request): Response? {
// TODO match against method and query parameters too
val endpointData = when {
request.url.host == WPCOM_HOST -> request.extractDataFromWPComEndpoint()
Expand All @@ -24,7 +24,7 @@ internal class EndpointProcessor @Inject constructor(
}

return with(endpointData) {
endpointDao.queryEndpoint(endpointType, path, body.orEmpty())
endpointDao.queryEndpoint(apiType, path, body.orEmpty())
}?.response
}

Expand All @@ -45,13 +45,13 @@ internal class EndpointProcessor @Inject constructor(
}

EndpointData(
endpointType = EndpointType.WPApi,
apiType = ApiType.WPApi,
path = path,
body = body
)
} else {
EndpointData(
endpointType = EndpointType.WPCom,
apiType = ApiType.WPCom,
path = url.encodedPath.substringAfter("/rest"),
body = originalBody
)
Expand All @@ -60,15 +60,15 @@ internal class EndpointProcessor @Inject constructor(

private fun Request.extractDataFromWPApiEndpoint(): EndpointData {
return EndpointData(
endpointType = EndpointType.WPApi,
apiType = ApiType.WPApi,
path = url.encodedPath.substringAfter("/wp-json"),
body = readBody()
)
}

private fun Request.extractDataFromCustomEndpoint(): EndpointData {
return EndpointData(
endpointType = EndpointType.Custom(host = url.host),
apiType = ApiType.Custom(host = url.host),
path = url.encodedPath,
body = readBody()
)
Expand All @@ -85,7 +85,7 @@ internal class EndpointProcessor @Inject constructor(
}

private data class EndpointData(
val endpointType: EndpointType,
val apiType: ApiType,
val path: String,
val body: String?
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ import androidx.room.Database
import androidx.room.Room
import androidx.room.RoomDatabase
import androidx.room.TypeConverters
import com.woocommerce.android.apifaker.models.Endpoint
import com.woocommerce.android.apifaker.models.FakeResponse
import com.woocommerce.android.apifaker.models.Request
import com.woocommerce.android.apifaker.models.Response

@Database(
entities = [
Endpoint::class, FakeResponse::class
Request::class, Response::class
],
version = 1
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,35 +5,35 @@ import androidx.room.Insert
import androidx.room.OnConflictStrategy
import androidx.room.Query
import androidx.room.Transaction
import com.woocommerce.android.apifaker.models.Endpoint
import com.woocommerce.android.apifaker.models.EndpointType
import com.woocommerce.android.apifaker.models.EndpointWithResponse
import com.woocommerce.android.apifaker.models.FakeResponse
import com.woocommerce.android.apifaker.models.Request
import com.woocommerce.android.apifaker.models.ApiType
import com.woocommerce.android.apifaker.models.MockedEndpoint
import com.woocommerce.android.apifaker.models.Response
import kotlinx.coroutines.flow.Flow

@Dao
internal interface EndpointDao {
@Transaction
@Query("Select * FROM Endpoint")
fun observeEndpoints(): Flow<List<EndpointWithResponse>>
@Query("Select * FROM Request")
fun observeEndpoints(): Flow<List<MockedEndpoint>>

@Transaction
@Query("Select * FROM Endpoint WHERE type = :type AND :path LIKE path AND :body LIKE COALESCE(body, '%')")
fun queryEndpoint(type: EndpointType, path: String, body: String): EndpointWithResponse?
@Query("Select * FROM Request WHERE type = :type AND :path LIKE path AND :body LIKE COALESCE(body, '%')")
fun queryEndpoint(type: ApiType, path: String, body: String): MockedEndpoint?

@Transaction
@Query("Select * FROM Endpoint WHERE id = :id")
suspend fun getEndpoint(id: Long): EndpointWithResponse?
@Query("Select * FROM Request WHERE id = :id")
suspend fun getEndpoint(id: Long): MockedEndpoint?

@Insert(onConflict = OnConflictStrategy.REPLACE)
suspend fun insertEndpoint(endpoint: Endpoint): Long
suspend fun insertRequest(request: Request): Long

@Insert(onConflict = OnConflictStrategy.REPLACE)
suspend fun insertResponse(response: FakeResponse)
suspend fun insertResponse(response: Response)

@Transaction
suspend fun insertEndpointWithResponse(endpoint: Endpoint, response: FakeResponse) {
val id = insertEndpoint(endpoint)
suspend fun insertEndpoint(request: Request, response: Response) {
val id = insertRequest(request)
insertResponse(response.copy(endpointId = id))
}
}
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
package com.woocommerce.android.apifaker.db

import androidx.room.TypeConverter
import com.woocommerce.android.apifaker.models.EndpointType
import com.woocommerce.android.apifaker.models.ApiType

internal class EndpointTypeConverter {
@TypeConverter
fun fromEndpointType(endpointType: EndpointType?): String? {
if (endpointType == null) return null
return endpointType::class.simpleName +
if (endpointType is EndpointType.Custom) ":${endpointType.host}" else ""
fun fromEndpointType(apiType: ApiType?): String? {
if (apiType == null) return null
return apiType::class.simpleName +
if (apiType is ApiType.Custom) ":${apiType.host}" else ""
}

@TypeConverter
fun toEndpointType(value: String?): EndpointType? {
fun toEndpointType(value: String?): ApiType? {
if (value == null) return null
val parts = value.split(":")
return when (parts[0]) {
EndpointType.WPApi::class.simpleName -> EndpointType.WPApi
EndpointType.WPCom::class.simpleName -> EndpointType.WPCom
else -> EndpointType.Custom(parts[1])
ApiType.WPApi::class.simpleName -> ApiType.WPApi
ApiType.WPCom::class.simpleName -> ApiType.WPCom
else -> ApiType.Custom(parts[1])
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.woocommerce.android.apifaker.models

internal sealed interface ApiType {
companion object {
fun defaultValues(): List<ApiType> = listOf(WPApi, WPCom, Custom(""))
}

data object WPApi : ApiType
data object WPCom : ApiType

data class Custom(val host: String) : ApiType
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ package com.woocommerce.android.apifaker.models
import androidx.room.Embedded
import androidx.room.Relation

internal data class EndpointWithResponse(
@Embedded val endpoint: Endpoint,
internal data class MockedEndpoint(
@Embedded val request: Request,
@Relation(
parentColumn = "id",
entityColumn = "endpointId"
)
val response: FakeResponse
val response: Response
)
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import androidx.room.Entity
import androidx.room.PrimaryKey

@Entity
internal data class Endpoint(
internal data class Request(
@PrimaryKey(autoGenerate = true) val id: Long = 0,
val type: EndpointType,
val type: ApiType,
val path: String,
val body: String?
)
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ import androidx.room.PrimaryKey
@Entity(
foreignKeys = [
ForeignKey(
entity = Endpoint::class,
entity = Request::class,
parentColumns = ["id"],
childColumns = ["endpointId"]
)
]
)
internal data class FakeResponse(
internal data class Response(
@PrimaryKey val endpointId: Long = 0,
val statusCode: Int,
val body: String?,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.woocommerce.android.apifaker

import com.woocommerce.android.apifaker.db.EndpointDao
import com.woocommerce.android.apifaker.models.EndpointType
import com.woocommerce.android.apifaker.models.ApiType
import com.woocommerce.android.apifaker.util.JSONObjectProvider
import okhttp3.Request
import okhttp3.RequestBody.Companion.toRequestBody
Expand Down Expand Up @@ -30,7 +30,7 @@ class EndpointProcessorTest {
endpointProcessor.fakeRequestIfNeeded(request)

verify(endpointDaoMock).queryEndpoint(
type = EndpointType.WPCom,
type = ApiType.WPCom,
path = "/v1.1/me",
body = ""
)
Expand All @@ -47,7 +47,7 @@ class EndpointProcessorTest {
endpointProcessor.fakeRequestIfNeeded(request)

verify(endpointDaoMock).queryEndpoint(
type = EndpointType.WPCom,
type = ApiType.WPCom,
path = "/v1.1/me",
body = body
)
Expand All @@ -63,7 +63,7 @@ class EndpointProcessorTest {
endpointProcessor.fakeRequestIfNeeded(request)

verify(endpointDaoMock).queryEndpoint(
type = EndpointType.WPApi,
type = ApiType.WPApi,
path = "/wc/v3/products",
body = ""
)
Expand All @@ -90,7 +90,7 @@ class EndpointProcessorTest {
endpointProcessor.fakeRequestIfNeeded(request)

verify(endpointDaoMock).queryEndpoint(
type = EndpointType.WPApi,
type = ApiType.WPApi,
path = "/wc/v3/products",
body = "test body"
)
Expand All @@ -106,7 +106,7 @@ class EndpointProcessorTest {
endpointProcessor.fakeRequestIfNeeded(request)

verify(endpointDaoMock).queryEndpoint(
type = EndpointType.WPApi,
type = ApiType.WPApi,
path = "/wc/v3/products",
body = ""
)
Expand All @@ -123,7 +123,7 @@ class EndpointProcessorTest {
endpointProcessor.fakeRequestIfNeeded(request)

verify(endpointDaoMock).queryEndpoint(
type = EndpointType.WPApi,
type = ApiType.WPApi,
path = "/wc/v3/products",
body = body
)
Expand All @@ -139,7 +139,7 @@ class EndpointProcessorTest {
endpointProcessor.fakeRequestIfNeeded(request)

verify(endpointDaoMock).queryEndpoint(
type = EndpointType.Custom("test-site.com"),
type = ApiType.Custom("test-site.com"),
path = "/an/endpoint",
body = ""
)
Expand All @@ -156,7 +156,7 @@ class EndpointProcessorTest {
endpointProcessor.fakeRequestIfNeeded(request)

verify(endpointDaoMock).queryEndpoint(
type = EndpointType.Custom("test-site.com"),
type = ApiType.Custom("test-site.com"),
path = "/an/endpoint",
body = body
)
Expand Down

0 comments on commit 07950ea

Please sign in to comment.