Skip to content
Merged
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import org.springframework.http.ResponseEntity;
import org.springframework.http.client.ClientHttpRequestFactory;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.http.converter.ByteArrayHttpMessageConverter;
import org.springframework.http.converter.json.JacksonJsonHttpMessageConverter;
import org.springframework.web.client.RestTemplate;
import tools.jackson.databind.json.JsonMapper;
Expand All @@ -32,7 +33,7 @@ public class RequestContext {
.build();
this.requestFactory = new HttpComponentsClientHttpRequestFactory(httpClient);
restTemplate = new RestTemplate(requestFactory);
restTemplate.setMessageConverters(List.of(new JacksonJsonHttpMessageConverter(getJsonMapper())));
restTemplate.setMessageConverters(List.of(new JacksonJsonHttpMessageConverter(getJsonMapper()), new ByteArrayHttpMessageConverter()));
}


Expand Down Expand Up @@ -71,6 +72,25 @@ public synchronized <R, E> R execute(RestUriBuilder uriBuilder, HttpMethod metho
return response.getBody();
}

public synchronized byte[] downloadFile(RestUriBuilder uriBuilder) {

HttpHeaders headers = new HttpHeaders();
headers.setAccept(List.of(MediaType.APPLICATION_PDF, MediaType.ALL));
headers.add("Authorization", "Bearer " + apiBuilder.getApiToken());

checkThrottlePeriod();

HttpEntity<Void> requestEntity = new HttpEntity<>(headers);
ResponseEntity<byte[]> response = restTemplate.exchange(uriBuilder.build(), HttpMethod.GET, requestEntity, byte[].class);
lastCall = System.currentTimeMillis();
if (apiBuilder.throttleProviderPresent()) {
apiBuilder.getThrottleProvider()
.apiCalled();
}

return response.getBody();
}

public synchronized void delete(RestUriBuilder uriBuilder) {

HttpHeaders headers = new HttpHeaders();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ public Invoice get(String id) {
return new Get(context).get(id);
}

public byte[] downloadFile(String id) {
return new DownloadFile(context).download(id);
}

public Create create() {
return new Create(context);
}
Expand All @@ -35,6 +39,19 @@ public Invoice get(String id) {
}
}

protected static class DownloadFile extends ExecutableRequestChain {

public DownloadFile(RequestContext context) {
super(context, "/invoices");
}

@SneakyThrows
public byte[] download(String id) {
getUriBuilder().appendPath("/" + id + "/file");
return getContext().downloadFile(getUriBuilder());
}
}

public static class Create extends ExecutableRequestChain {
private static final ParameterizedTypeReference<Invoice> TYPE_REFERENCE = new ParameterizedTypeReference<Invoice>() {
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,26 @@ void createInvoice() {
true)));
}

@Test
void downloadInvoiceFile() {
String invoiceId = "e9066f04-8cc7-4616-93f8-ac9571ec5e11";
byte[] pdfContent = "%PDF-1.4 fake pdf content".getBytes();

stubFor(
get(urlPathEqualTo("/v1/invoices/" + invoiceId + "/file"))
.willReturn(
aResponse()
.withHeader("Content-Type", "application/pdf")
.withHeader(
"Content-Disposition",
"attachment; filename=\"RE1019.pdf\"")
.withBody(pdfContent)));

byte[] result = lexofficeApi.invoice().downloadFile(invoiceId);

assertThat(result).isEqualTo(pdfContent);
}

@Test
void createInvoiceFinalized() {
stubFor(
Expand Down