Skip to content

Commit

Permalink
Small improvements & README file (#49)
Browse files Browse the repository at this point in the history
* return custom response instead of the real call response; add README file

* Update build.gradle

* Update build.gradle

* Update build.gradle

---------

Co-authored-by: Eugene Tkachenko <[email protected]>
  • Loading branch information
Sunhumann and eugene-tkachenko authored Jul 23, 2024
1 parent 174aae8 commit 187a1cb
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 11 deletions.
6 changes: 3 additions & 3 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ dependencies {
releaseImplementation project(':okhttp-requests-modifier-no-op')
debugImplementation project(':okhttp-requests-modifier')

// releaseImplementation ('io.nerdythings:requests-modifier-no-op:1.1.0')
// debugImplementation ('io.nerdythings:requests-modifier:1.1.0')
// implementation ('io.nerdythings:okhttpprofiler:1.1.0')
// releaseImplementation ('io.nerdythings:okhttp-requests-modifier-no-op:1.0.0')
// debugImplementation ('io.nerdythings:okhttp-requests-modifier:1.0.0')
// implementation ('io.nerdythings:okhttp-profiler:1.1.1')
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.withContext
import okhttp3.Protocol
import okhttp3.Request
import okhttp3.Response
import okhttp3.ResponseBody.Companion.toResponseBody
import java.io.File
Expand Down Expand Up @@ -142,12 +144,15 @@ class DataModifier(private val context: Context) {
fetchAllCustomResponses()
}

fun modifyResponse(response: Response): Response =
getCustomResponses(response.request.url.toString()).firstOrNull { it.isEnabled }
fun modifyResponse(request: Request, makeRequest: () -> Response) =
getCustomResponses(request.url.toString()).firstOrNull { it.isEnabled }
?.let { customResponse ->
response.newBuilder().apply {
code(customResponse.code)
body(customResponse.response.toResponseBody(response.body?.contentType()))
}.build()
} ?: response
Response.Builder()
.request(request)
.protocol(Protocol.HTTP_2)
.message("success")
.code(customResponse.code)
.body(customResponse.response.toResponseBody())
.build()
} ?: makeRequest()
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ class OkHttpRequestModifierInterceptor(context: Context) : Interceptor {
@Throws(IOException::class)
override fun intercept(chain: Chain): Response {
try {
return dataModifier.modifyResponse(chain.proceed(chain.request()))
return dataModifier.modifyResponse(chain.request()) {
chain.proceed(chain.request())
}
} catch (e: Exception) {
throw e
}
Expand Down
Binary file added request_modifiers_activity.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added request_modifiers_add_new_modifier.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
63 changes: 63 additions & 0 deletions requests-modifier/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
Request Modifier is a new Android library designed to provide developers with an easy way to
customize HTTP responses.
By adding this library into your project, you gain the ability to modify response bodies and
response codes dynamically.

| <img src="https://github.com/itkacher/OkHttpProfiler/blob/master/request_modifiers_activity.png?raw=true"> | <img src="https://github.com/itkacher/OkHttpProfiler/blob/master/request_modifiers_add_new_modifier.png?raw=true"> |
|------------------------------------------------------------------------------------------------------------|--------------------------------------------------------------------------------------------------------------------|

## Installation

For installation, you need to include the library to your app build.gradle file

releaseImplementation 'io.nerdythings:okhttp-requests-modifier-no-op:1.1.0'
debugImplementation 'io.nerdythings:okhttp-requests-modifier:1.0.0'

and add Interceptor to okHttpClient in code

##### For OkHttp

val builder = OkHttpClient.Builder()
if (BuildConfig.DEBUG) {
builder.addInterceptor(OkHttpRequestModifierInterceptor(applicationContext))
}
val client = builder.build()

##### For Retrofit

val builder = OkHttpClient.Builder()
if (BuildConfig.DEBUG) {
builder.addInterceptor(OkHttpRequestModifierInterceptor(applicationContext))
}
val client = builder.build()
val retrofit = Retrofit.Builder()
......
.client(client)
.build()

##### For security reasons we recommend to enable OkHttpProfilerInterceptor only for DEBUG BUILDS!

Also Proguard will cut it out in the release build.

## Customization Capabilities

With the Request Modifier, you can adjust the response body and response code for your HTTP
requests.
Additionally, the library supports the use of regular expressions (regex) to match and modify
multiple requests at once, offering a powerful and flexible way to handle different scenarios.

Important: if you override request, it won't make a real call, it'll immediately return your custom
data.

## Usage

To define custom responses, follow these steps:

1. Add the library to your project and set up the custom interceptor as shown above.
2. Access the built-in Activity from your debug menu, which provides a user-friendly interface for
managing your requests.
3. Through this Activity, you can easily add and modify your requests, specifying the desired
response bodies and response codes.

This intuitive UI makes it straightforward for developers to define and manage custom responses
without diving deep into the code, enhancing productivity and debugging efficiency.

0 comments on commit 187a1cb

Please sign in to comment.