com.tradeshift.sdk.assignments.TradeshiftAssignmentsClient Maven / Gradle / Ivy
The newest version!
/*
* Copyright (c) 2018 Tradeshift Inc. and/or its affiliates. All rights reserved.
*/
package com.tradeshift.sdk.assignments;
import static java.net.HttpURLConnection.HTTP_NO_CONTENT;
import static org.asynchttpclient.util.HttpConstants.Methods.DELETE;
import static org.asynchttpclient.util.HttpConstants.Methods.POST;
import static org.asynchttpclient.util.HttpConstants.Methods.PUT;
import java.util.Objects;
import java.util.concurrent.CompletionStage;
import javax.ws.rs.core.HttpHeaders;
import org.asynchttpclient.RequestBuilder;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import com.tradeshift.sdk.assignments.domain.AssignmentClosed;
import com.tradeshift.sdk.assignments.domain.AssignmentFlagUpdated;
import com.tradeshift.sdk.assignments.domain.AssignmentLocked;
import com.tradeshift.sdk.assignments.domain.AssignmentOpened;
import com.tradeshift.sdk.assignments.domain.AssignmentSaved;
import com.tradeshift.sdk.assignments.domain.AutoAssignmentDeleted;
import com.tradeshift.sdk.assignments.domain.CloseAssignment;
import com.tradeshift.sdk.assignments.domain.DeleteAutoAssignment;
import com.tradeshift.sdk.assignments.domain.LockAssignment;
import com.tradeshift.sdk.assignments.domain.OpenAssignment;
import com.tradeshift.sdk.assignments.domain.SaveAssignment;
import com.tradeshift.sdk.assignments.domain.SetHoldFlag;
import com.tradeshift.sdk.assignments.domain.SetImportantFlag;
import com.tradeshift.sdk.assignments.json.JsonAssignment;
import com.tradeshift.sdk.core.TradeshiftClient;
import com.tradeshift.sdk.core.TradeshiftClientConfiguration;
public class TradeshiftAssignmentsClient extends TradeshiftClient implements TradeshiftAssignments {
private final ObjectMapper objectMapper;
public TradeshiftAssignmentsClient(TradeshiftClientConfiguration config) {
this(config, new ObjectMapper());
}
public TradeshiftAssignmentsClient(TradeshiftClientConfiguration config, ObjectMapper objectMapper) {
super(config);
this.objectMapper = objectMapper;
this.objectMapper.registerModule(new JavaTimeModule());
}
@Override
public CompletionStage saveAssignment(SaveAssignment cmd) {
Objects.requireNonNull(cmd);
String url = String.format("%s/assignments", endpoint);
JsonAssignment json = JsonAssignment.of(cmd.getAssignment());
return execute(authenticated(cmd, new RequestBuilder(POST)
.setHeader(HttpHeaders.CONTENT_TYPE, cmd.getMediaType())
.setUrl(url)
.setBody(serialize(json))))
.thenApply(response -> {
if (response.getStatusCode() == 201) {
return new AssignmentSaved(cmd.getUserContext(), cmd.getAssignment().getId());
}
throw parseHttpStatusCode(cmd.getUserContext(), response);
});
}
@Override
public CompletionStage openAssignment(OpenAssignment cmd) {
Objects.requireNonNull(cmd);
String url = String.format("%s/assignments/%s/open", endpoint, cmd.getAssignmentId());
return execute(authenticated(cmd, new RequestBuilder(PUT).setUrl(url)))
.thenApply(response -> {
if (response.getStatusCode() == 204) {
return new AssignmentOpened(cmd.getUserContext(), cmd.getAssignmentId());
}
throw parseHttpStatusCode(cmd.getUserContext(), response);
});
}
@Override
public CompletionStage lockAssignment(LockAssignment cmd) {
Objects.requireNonNull(cmd);
String url = String.format("%s/assignments/%s/lock", endpoint, cmd.getAssignmentId());
return execute(authenticated(cmd, new RequestBuilder(PUT).setUrl(url)))
.thenApply(response -> {
if (response.getStatusCode() == 204) {
return new AssignmentLocked(cmd.getUserContext(), cmd.getAssignmentId());
}
throw parseHttpStatusCode(cmd.getUserContext(), response);
});
}
@Override
public CompletionStage closeAssignment(CloseAssignment cmd) {
Objects.requireNonNull(cmd);
String url = String.format("%s/assignments/%s/close", endpoint, cmd.getAssignmentId());
return execute(authenticated(cmd, new RequestBuilder(PUT).setUrl(url)))
.thenApply(response -> {
if (response.getStatusCode() == 204) {
return new AssignmentClosed(cmd.getUserContext(), cmd.getAssignmentId());
}
throw parseHttpStatusCode(cmd.getUserContext(), response);
});
}
@Override
public CompletionStage setImportantFlagForAssignment(SetImportantFlag cmd) {
Objects.requireNonNull(cmd);
String url = String.format("%s/assignments/%s/important/%s", endpoint, cmd.getAssignmentId(), cmd.isValue());
return execute(authenticated(cmd, new RequestBuilder(PUT).setUrl(url)))
.thenApply(response -> {
if (response.getStatusCode() == 204) {
return new AssignmentFlagUpdated(cmd.getUserContext(), cmd.getAssignmentId());
}
throw parseHttpStatusCode(cmd.getUserContext(), response);
});
}
@Override
public CompletionStage setHoldFlagForAssignment(SetHoldFlag cmd) {
Objects.requireNonNull(cmd);
String url = String.format("%s/assignments/%s/hold/%s", endpoint, cmd.getAssignmentId(), cmd.isValue());
return execute(authenticated(cmd, new RequestBuilder(PUT).setUrl(url)))
.thenApply(response -> {
if (response.getStatusCode() == 204) {
return new AssignmentFlagUpdated(cmd.getUserContext(), cmd.getAssignmentId());
}
throw parseHttpStatusCode(cmd.getUserContext(), response);
});
}
@Override
public CompletionStage deleteAutoAssignment(DeleteAutoAssignment cmd) {
Objects.requireNonNull(cmd);
final String url = String.format("%s/assignments/autoassignments/assignees/%s", endpoint, cmd.getAssigneeId());
return execute(authenticated(cmd, new RequestBuilder(DELETE).setUrl(url)))
.thenApply(r -> {
if (r.getStatusCode() == HTTP_NO_CONTENT) {
return new AutoAssignmentDeleted(cmd.getUserContext(), cmd.getAssigneeId());
}
throw parseHttpStatusCode(cmd.getUserContext(), r);
});
}
private String serialize(Object o) {
try {
return objectMapper.writeValueAsString(o);
} catch (JsonProcessingException e) {
throw new RuntimeException("Could not serialize Json", e);
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy