Make POSTing cards work

This commit is contained in:
Malanius 2018-10-28 11:32:44 +01:00
parent da353afd21
commit 3f5c1d4fc3
5 changed files with 41 additions and 24 deletions

View file

@ -2,6 +2,7 @@ package cz.brmlab.wm;
import cz.brmlab.wm.utils.Exceptions.BrmException; import cz.brmlab.wm.utils.Exceptions.BrmException;
import cz.brmlab.wm.wekan.WekanConfiguration; import cz.brmlab.wm.wekan.WekanConfiguration;
import cz.brmlab.wm.wekan.pojo.card.PostCardResponse;
import cz.brmlab.wm.wekan.rest.CardPost; import cz.brmlab.wm.wekan.rest.CardPost;
import cz.brmlab.wm.wekan.rest.LoginPost; import cz.brmlab.wm.wekan.rest.LoginPost;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
@ -32,7 +33,7 @@ public class Application implements CommandLineRunner {
loginPost.login(); loginPost.login();
CardPost cardPost = new CardPost(loginPost.getToken(), wekanConfiguration); CardPost cardPost = new CardPost(loginPost.getToken(), wekanConfiguration);
cardPost.postCard("Test from spring", "Test card from awesome spring app.\nAnd next line"); PostCardResponse postCardResponse = cardPost.postCard("Test from spring", "Test card from awesome spring app.\nAnd next line");
} catch (BrmException ex) { } catch (BrmException ex) {
log.error("Error {} encountered, shutting down!", ex.getExitCode()); log.error("Error {} encountered, shutting down!", ex.getExitCode());

View file

@ -4,7 +4,8 @@ import lombok.Getter;
public enum ExitCode { public enum ExitCode {
CONFIGURATION_MISSING(10, "Missing configuration property: "); CONFIGURATION_MISSING(10, "Missing configuration property: "),
POST_ERROR(20, "Failed POST request, RC: ");
@Getter @Getter
private String reason; private String reason;

View file

@ -3,7 +3,7 @@ package cz.brmlab.wm.wekan.pojo.card;
import lombok.Data; import lombok.Data;
@Data @Data
public class CardRequest { public class PostCardRequest {
private String title; private String title;
private String description; private String description;

View file

@ -3,8 +3,8 @@ package cz.brmlab.wm.wekan.pojo.card;
import lombok.Data; import lombok.Data;
@Data @Data
public class CardResponse { public class PostCardResponse {
private String id; private String _id;
} }

View file

@ -1,44 +1,59 @@
package cz.brmlab.wm.wekan.rest; package cz.brmlab.wm.wekan.rest;
import cz.brmlab.wm.utils.Exceptions.BrmException;
import cz.brmlab.wm.utils.Exceptions.ExitCode;
import cz.brmlab.wm.wekan.WekanConfiguration; import cz.brmlab.wm.wekan.WekanConfiguration;
import cz.brmlab.wm.wekan.pojo.card.CardRequest; import cz.brmlab.wm.wekan.pojo.card.PostCardRequest;
import cz.brmlab.wm.wekan.pojo.card.CardResponse; import cz.brmlab.wm.wekan.pojo.card.PostCardResponse;
import cz.brmlab.wm.wekan.pojo.login.LoginToken; import cz.brmlab.wm.wekan.pojo.login.LoginToken;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.http.*;
import org.springframework.web.client.HttpStatusCodeException;
import org.springframework.web.client.RestTemplate; import org.springframework.web.client.RestTemplate;
import java.util.Collections;
@Slf4j @Slf4j
public class CardPost { public class CardPost {
private final LoginToken token; private final LoginToken token;
private final WekanConfiguration configuration; private static final String POST_CARD_URL_TEMPLATE = "${host}/api/boards/${boardId}/lists/${listId}/cards";
private final String postCardUrl;
public CardPost(LoginToken token, WekanConfiguration configuration) { public CardPost(LoginToken token, WekanConfiguration configuration) {
log.trace("{}() - start.", this.getClass().getSimpleName()); log.trace("{}() - start.", this.getClass().getSimpleName());
this.token = token; this.token = token;
this.configuration = configuration; postCardUrl = POST_CARD_URL_TEMPLATE.replace("${host}", configuration.getWekanUrl())
.replace("${boardId}", configuration.getWekanBoard())
.replace("${listId}", configuration.getWekanList());
log.debug("Card post URL: {}", postCardUrl);
} }
public CardResponse postCard(String title, String description) { public PostCardResponse postCard(String title, String description) throws BrmException {
log.trace("postCard({}, {}) - start.", title, description); log.trace("postCard({}, {}) - start.", title, description);
CardRequest cardRequest = new CardRequest(); PostCardRequest postCardRequest = new PostCardRequest();
cardRequest.setAuthorId(token.getId()); postCardRequest.setAuthorId(token.getId());
cardRequest.setTitle(title); postCardRequest.setTitle(title);
cardRequest.setDescription(description); postCardRequest.setDescription(description);
String cardUrl = configuration.getWekanUrl()
+ "/api/boards"
+ configuration.getWekanBoard()
+ "/lists/"
+ configuration.getWekanList() + "cards";
RestTemplate restTemplate = new RestTemplate(); RestTemplate restTemplate = new RestTemplate();
log.debug("Sending card: {}", cardRequest); HttpHeaders headers = new HttpHeaders();
CardResponse cardResponse = restTemplate.postForObject(cardUrl, cardRequest, CardResponse.class); headers.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON));
headers.set("Authorization", "Bearer " + token.getToken());
HttpEntity<PostCardRequest> entity = new HttpEntity<>(postCardRequest, headers);
log.debug("Sending card: {}", postCardRequest);
try {
ResponseEntity<PostCardResponse> responseEntity = restTemplate.exchange(postCardUrl, HttpMethod.POST, entity, PostCardResponse.class);
PostCardResponse postCardResponse = responseEntity.getBody();
log.info("Card {} successfully sent - cardId: {}", title, postCardResponse != null ? postCardResponse.get_id() : null);
return postCardResponse;
} catch (HttpStatusCodeException ex) {
log.error("Unable to POST a card, status code: {}", ex.getStatusCode());
throw new BrmException(ExitCode.POST_ERROR.getReason() + ex.getStatusCode(), ExitCode.POST_ERROR);
}
log.info("Card {} successfully sent - cardId: {}", title, cardResponse.getId());
return cardResponse;
} }
} }