Skip to content

Commit ab17760

Browse files
Update README.md
1 parent 56411b9 commit ab17760

1 file changed

Lines changed: 95 additions & 4 deletions

File tree

README.md

Lines changed: 95 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,99 @@ dependencies {
2323
}
2424
```
2525

26-
## Todo
26+
## How to use
2727

28-
- Add example usage and demo gif
29-
- Use builder design pattern to simplify constructor
30-
- Split codes to more classes
28+
### 1. Create json file of your api response lets say `picsum_list.json` and put it in `assets/mock_json` directory
29+
30+
```json
31+
[
32+
{
33+
"id": "1016",
34+
"author": "Andrew Ridley",
35+
"width": 3844,
36+
"height": 2563,
37+
"url": "https://unsplash.com/photos/_h7aBovKia4",
38+
"download_url": "https://picsum.photos/id/1018/3914/2935"
39+
},
40+
{
41+
"id": "1018",
42+
"author": "Andrew Ridley",
43+
"width": 3914,
44+
"height": 2935,
45+
"url": "https://unsplash.com/photos/Kt5hRENuotI",
46+
"download_url": "https://picsum.photos/id/1018/3914/2935"
47+
},
48+
{
49+
"id": "1019",
50+
"author": "Patrick Fore",
51+
"width": 5472,
52+
"height": 3648,
53+
"url": "https://unsplash.com/photos/V6s1cmE39XM",
54+
"download_url": "https://picsum.photos/id/1019/5472/3648"
55+
}
56+
]
57+
```
58+
59+
### 2. Define your api interface and annotate endpoint with `@mock("picsum_list.json")` so that Mockfit can generate config for you
60+
```kotlin
61+
interface Api {
62+
63+
@Mock("picsum_list.json")
64+
@GET("list")
65+
fun getListOfPicsums(
66+
@Query("page") page: Int,
67+
@Query("limit") limit: Int
68+
): Call<List<Picsum>>
69+
}
70+
```
71+
72+
### 3. Add `MockfitInterceptor` to retrofit configuration
73+
```kotlin
74+
class RemoteDataSource(private val context: Context) {
75+
76+
fun api(): Api {
77+
val mockFitInterceptor = provideMockFitInterceptor(context)
78+
val okHttpClient = provideOkHttpClient(mockFitInterceptor)
79+
val retrofit = provideRetrofit(okHttpClient)
80+
return retrofit.create(Api::class.java)
81+
}
82+
83+
private fun provideRetrofit(okHttpClient: OkHttpClient): Retrofit = Retrofit.Builder()
84+
.baseUrl(BASE_URL)
85+
.addConverterFactory(GsonConverterFactory.create())
86+
.client(okHttpClient)
87+
.build()
88+
89+
private fun provideMockFitInterceptor(context: Context) = MockFitInterceptor(
90+
bodyFactory = { input -> context.resources.assets.open(input) }, // read asset file
91+
logger = { tag, message -> Log.d(tag, message) }, // pass logger to log events in logcat
92+
baseUrl = BASE_URL, // base url of your api
93+
requestPathToJsonMap = REQUEST_TO_JSON, // autogenerated constant, just press build button
94+
mockFilesPath = MOCK_FILES_PATH, // path to json files
95+
mockFitEnable = true, // master setting to enable or disable mocking
96+
apiEnableMock = true, // enable or disable mock when there are includes and excludes configs
97+
apiIncludeIntoMock = arrayOf(), // include endpoint if `apiEnableMock` is false
98+
apiExcludeFromMock = arrayOf(), // exclude endpoint if `apiEnableMock` is true
99+
apiResponseLatency = 500L // latency of retrieving data
100+
)
101+
102+
private fun provideOkHttpClient(mockFitInterceptor: MockFitInterceptor) = OkHttpClient.Builder()
103+
.addInterceptor(mockFitInterceptor)
104+
.connectTimeout(CONNECT_TIMEOUT, TimeUnit.SECONDS)
105+
.writeTimeout(WRITE_TIMEOUT, TimeUnit.SECONDS)
106+
.readTimeout(READ_TIMEOUT, TimeUnit.SECONDS)
107+
.build()
108+
109+
companion object {
110+
private const val BASE_URL = "https://picsum.photos/v2/"
111+
private const val MOCK_FILES_PATH = "mock_json" // located at assets/mock_json/
112+
private const val CONNECT_TIMEOUT = 20L
113+
private const val WRITE_TIMEOUT = 20L
114+
private const val READ_TIMEOUT = 30L
115+
}
116+
}
117+
```
118+
119+
Now you can see your mock data in UI:
120+
121+
<img src="https://user-images.githubusercontent.com/29440700/115327793-ce226080-a1a4-11eb-814b-5f58afbb5af6.png" width="256" height="455">

0 commit comments

Comments
 (0)