Skip to content
Open
Changes from all commits
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
59 changes: 51 additions & 8 deletions src/main/java/com/compass/vinyl/interceptor/VinylInterceptor.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,62 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.List;

import javax.xml.bind.DatatypeConverter;

public class VinylInterceptor {

public static class OkHttpInterceptor implements Interceptor {

private static final boolean HEAD_PEEK = true ;

public static String checkSum(String string) {
try {
MessageDigest md = MessageDigest.getInstance("MD5");
byte[] thedigest = md.digest(string.getBytes());
return DatatypeConverter.printHexBinary(thedigest);
} catch (NoSuchAlgorithmException e){
return "0";
}
}

private static Response peekAndGet( Scenario recordedScenario, Chain chain ) throws IOException {
Request original = chain.request();
String responseJSON = (String) recordedScenario.getOutput().getValue();
Response cachedResponse = new Response.Builder()
.request(original)
.protocol(Protocol.HTTP_1_1)
.code(201)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why 201?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copied from original code : CREATED 201
:-)

.message("")
.body(ResponseBody.create(responseJSON, okhttp3.MediaType.parse(MEDIA_TYPE_JSON)))
.build();
if ( HEAD_PEEK ){

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could do this without compromising on the time taken to load the response. Since we use observables in android, we could sent the cached response as is and at the same time send a parallel request in a co-routine to check the validity of this response (using the head message like below). This way the UI will load faster and at the same time, load the changed response (if server has updated response).

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Murtaza-arif , @sarthak-baiswar - who wants to take this up? @nmondal has created a partial poc with this pull request.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But for every GET api, we need to create a HEAD API as well?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is just the idea. We have to see if apiv3 supports adding custom headers, if not it will be handled as an additional attribute in sirius response. The idea is correct - the implementation is left to you guys on what integrates better with compass.

// create another one from the original
Request headRequest = original.newBuilder().head().build();
Response headResponse = chain.proceed(headRequest);
String checkSum = headResponse.header( COMPASS_HEAD_CHECKSUM , "");
if ( checkSum.isEmpty() ){
return cachedResponse;
}
String cachedCheckSum = checkSum(responseJSON);
// obvious... yes?
if ( cachedCheckSum.equals( checkSum ) ){
return cachedResponse;
}
// now, here, we go for the actual call
Response response = chain.proceed(original);
return response;
}
return cachedResponse;
}

private static final String MEDIA_TYPE_JSON = "application/json; charset=utf-8";

private static final String REQUEST = "requestBody";
Expand All @@ -29,6 +77,8 @@ public static class OkHttpInterceptor implements Interceptor {

private static final String COMPASS_RESPONSE_FILTER = "X-Compass-Response-Filter";

private static final String COMPASS_HEAD_CHECKSUM = "X-Compass-CHECKSUM";

private static final Logger LOG = LoggerFactory.getLogger(OkHttpInterceptor.class);

private final Vinyl vinyl;
Expand Down Expand Up @@ -67,14 +117,7 @@ public Response intercept(@NotNull Chain chain) throws IOException {
vinyl.record(new Scenario(url, method, inputs, output));
}
else {
String responseJSON = (String) recordedScenario.getOutput().getValue();
response = new Response.Builder()
.request(request)
.protocol(Protocol.HTTP_1_1)
.code(201)
.message("")
.body(ResponseBody.create(responseJSON, okhttp3.MediaType.parse(MEDIA_TYPE_JSON)))
.build();
response = peekAndGet(recordedScenario,chain);
}
return response;
}
Expand Down