Skip to content
This repository has been archived by the owner on Sep 13, 2024. It is now read-only.

Commit

Permalink
Merge pull request #21 from libgraviton/feature/file-endpoint-default…
Browse files Browse the repository at this point in the history
…-headers

Feature/file endpoint default headers
  • Loading branch information
wegenmic authored Jul 7, 2017
2 parents 8e35220 + f15ebe3 commit c2b2f60
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ public String getBaseUrl() {
*/
public Request.Builder request() {
return new Request.Builder(executor)
.setHeaders(getDefaultHeaders());
.setHeaders(getDefaultHeaders().build());
}

public Request.Builder head(String url) {
Expand Down Expand Up @@ -233,10 +233,9 @@ public ObjectMapper getObjectMapper() {
}

// TODO make it configurable
protected HeaderBag getDefaultHeaders() {
protected HeaderBag.Builder getDefaultHeaders() {
return new HeaderBag.Builder()
.set("Content-Type", "application/json")
.set("Accept", "application/json")
.build();
.set("Accept", "application/json");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,12 @@ public Request.Builder getFile(String url) {
LOG.debug("Requesting file");

// without the 'Accept' - 'application/json' header, we get the file instead of the metadata
HeaderBag headers = new HeaderBag.Builder()
.set("Content-Type", "application/json")
.build();
HeaderBag.Builder headers = gravitonApi.getDefaultHeaders()
.unset("Accept");

return gravitonApi.request()
.setUrl(url)
.setHeaders(headers)
.setHeaders(headers.build())
.get();
}

Expand Down Expand Up @@ -68,20 +67,28 @@ public Request.Builder post(byte[] data, GravitonBase resource) throws Serializa
Part dataPart = new Part(data, "upload");
Part metadataPart = new Part(gravitonApi.serializeResource(resource), "metadata");

HeaderBag.Builder headers = gravitonApi.getDefaultHeaders()
.unset("Accept")
.unset("Content-Type");

return gravitonApi.request()
.setUrl(gravitonApi.getEndpointManager().getEndpoint(resource.getClass().getName()).getUrl())
.setHeaders(new HeaderBag.Builder().build())
.setHeaders(headers.build())
.post(dataPart, metadataPart);
}

public Request.Builder put(byte[] data, GravitonBase resource) throws SerializationException {
Part dataPart = new Part(data, "upload");
Part metadataPart = new Part(gravitonApi.serializeResource(resource), "metadata");

HeaderBag.Builder headers = gravitonApi.getDefaultHeaders()
.unset("Accept")
.unset("Content-Type");

return gravitonApi.request()
.setUrl(gravitonApi.getEndpointManager().getEndpoint(resource.getClass().getName()).getItemUrl())
.addParam("id", gravitonApi.extractId(resource))
.setHeaders(new HeaderBag.Builder().build())
.setHeaders(headers.build())
.put(dataPart, metadataPart);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Arrays;

/**
* This is the base class used for Graviton API calls.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import org.slf4j.LoggerFactory;

import java.io.*;
import java.util.Iterator;
import java.util.Map;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@ public void setupService() throws Exception {
when(gravitonApi.getEndpointManager()).thenReturn(endpointManager);
when(gravitonApi.extractId(any(GravitonBase.class))).thenCallRealMethod();
when(gravitonApi.serializeResource(any(SimpleClass.class))).thenReturn("{ \"id\":\"111\"}");
HeaderBag headers = new HeaderBag.Builder()
HeaderBag.Builder headers = new HeaderBag.Builder()
.set("whatever", "something")
.build();
.set("Accept", "almost-everything");

when(gravitonApi.getDefaultHeaders()).thenReturn(headers);
resource = new SimpleClass();
Expand All @@ -56,7 +56,7 @@ public void setupService() throws Exception {
public void testGetFile() throws Exception {
Request request = gravitonFileEndpoint.getFile(url).build();
assertEquals(0, request.getHeaders().get("Accept").all().size());
assertEquals(1, request.getHeaders().get("Content-Type").all().size());
assertEquals(1, request.getHeaders().get("whatever").all().size());
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
import org.junit.Test;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.mock;

public class NoopResponseTest {
Expand Down

0 comments on commit c2b2f60

Please sign in to comment.