Barricade allows apps to get responses to API requests by intercepting them using an OkHttp Network Interceptor. The responses are fetched from local assets based on the configuration.
Barricade also supports multiple responses per request, unlike other local server implementations and presents the user with a UI for modifying which response to return for a request at runtime. It also provides an option to change the response and latency of the API call programatically
The source code of Barricade, and the sample app is available on GitHub.
compile 'com.mutualmobile:barricade:(insert latest version)'
annotationProcessor 'com.mutualmobile:barricade-compiler:(insert latest version)'
Barricade supports Android 4.1+
Barricade
is the runtime class which is used to initialize and configure the library
dynamically.
BarricadeConfig
is the class which is generated at compile time based on the
@Barricade annotations and data provided inside it.
BarricadeActivity
is an activity class which provides a UI to select the default
response for each API endpoint.
Below are the steps to configure Barricade in an Android application -
Install Barricade in the onCreate()
method of your Application
class.
@Override
public void onCreate() {
super.onCreate();
new Barricade.Builder(this, new BarricadeConfig()).enableShakeToStart(this).install();
...
}
Note: Setting enableShakeToStart to true starts BarricadeActivity when device is shaked
twice.
Add your API response files to assets/barricade/<endpoint_name>
for
different types of responses (success, invalid, error etc).
In the Retrofit service API interface, annotate the methods with @Barricade. The @Barricade
annotation accepts 2 parameters:
@Response
annotations corresponding to various
responses barricade can return
@GET("/users/{user}/repos")
@Barricade(endpoint = "repos", responses = {
@Response(fileName = "get_repos_success", isDefault = true),
@Response(fileName = "get_repos_error", statusCode = 500)
}) Single<ReposResponse> getUserRepositories(@Path("user") String userId);
...
Each @Response
annotation corresponds to a unique response for the given
endpoint. It takes 4 parameters:
assets/barricade/endpoint/
Add BarricadeInterceptor
to OkHttpClient
OkHttpClient okHttpClient =
new OkHttpClient.Builder().addInterceptor(new BarricadeInterceptor())
...
.build();
Barricade can be enabled or disabled at runtime.
To enable - Barricade.getInstance().setEnabled(true);
To disable - Barricade.getInstance().setEnabled(false);
Change response for endpoints
Barricade allows changing the response time and the default response for an endpoint at runtime.
If enableShakeToStart was set to true during Barricade initialization, the following process can used to configure Barricade -
You can also change the above settings programmatically which can be helpful for testing -
Barricade.getInstance()
.setDelay(100)
.setResponse(BarricadeConfig.Endpoints.REPOS, BarricadeConfig.Responses.Repos.GetReposSuccess);
Note: Changes made using setDelay and setResponse are persisted in SharedPreferences.
If you would like to contribute code you can do so through GitHub by forking the repository and sending a pull request.
When submitting code, please make every effort to follow existing conventions and style in order to keep the code as readable as possible.
Copyright 2016 Mutual Mobile 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.