Skip to content

Commit

Permalink
Merge pull request #1409 from dimagi/dv/api_versioning
Browse files Browse the repository at this point in the history
Providing API version header from server to caller when available.
  • Loading branch information
OrangeAndGreen authored Jul 19, 2024
2 parents d09e72a + 3c220b1 commit 1f7e218
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public interface HttpResponseProcessor {
/**
* Http response was in the 200s
*/
void processSuccess(int responseCode, InputStream responseData);
void processSuccess(int responseCode, InputStream responseData, String apiVersion);

/**
* Http response was in the 400s.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@

public interface ResponseStreamAccessor {
InputStream getResponseStream() throws IOException;
String getApiVersion();
}
33 changes: 22 additions & 11 deletions src/main/java/org/commcare/core/network/ModernHttpRequester.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
*
* @author Phillip Mates ([email protected])
*/
public class ModernHttpRequester implements ResponseStreamAccessor {
public class ModernHttpRequester {
/**
* How long to wait when opening network connection in milliseconds
*/
Expand Down Expand Up @@ -87,7 +87,23 @@ public void makeRequestAndProcess() {
}
try {
response = makeRequest();
processResponse(responseProcessor, response.code(), this);
final ModernHttpRequester requester = this;
processResponse(responseProcessor, response.code(), new ResponseStreamAccessor() {
/**
* Only gets called if response processor is supplied
* @return Input Stream from cache
* @throws IOException if an io error happens while reading or writing to cache
*/
@Override
public InputStream getResponseStream() throws IOException {
return requester.getResponseStream(response);
}

@Override
public String getApiVersion() {
return requester.getApiVersion();
}
});
} catch (IOException e) {
e.printStackTrace();
responseProcessor.handleIOException(e);
Expand Down Expand Up @@ -153,7 +169,8 @@ public static void processResponse(HttpResponseProcessor responseProcessor,
responseProcessor.handleIOException(e);
return;
}
responseProcessor.processSuccess(responseCode, responseStream);
String apiVersion = streamAccessor.getApiVersion();
responseProcessor.processSuccess(responseCode, responseStream, apiVersion);
} finally {
StreamsUtil.closeStream(responseStream);
}
Expand Down Expand Up @@ -181,14 +198,8 @@ public InputStream getResponseStream(Response<ResponseBody> response) throws IOE
return cache.retrieveCache();
}

/**
* Only gets called if response processor is supplied
* @return Input Stream from cache
* @throws IOException if an io error happens while reading or writing to cache
*/
@Override
public InputStream getResponseStream() throws IOException {
return getResponseStream(response);
public String getApiVersion() {
return response != null ? response.headers().get("x-api-current-version") : null;
}

public static RequestBody getPostBody(Multimap<String, String> inputs) {
Expand Down

0 comments on commit 1f7e218

Please sign in to comment.