mirror of
https://github.com/brmlab/wekan-mailer.git
synced 2025-06-08 09:54:10 +02:00
Make POSTing cards work
This commit is contained in:
parent
da353afd21
commit
3f5c1d4fc3
5 changed files with 41 additions and 24 deletions
|
@ -2,6 +2,7 @@ package cz.brmlab.wm;
|
|||
|
||||
import cz.brmlab.wm.utils.Exceptions.BrmException;
|
||||
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.LoginPost;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
@ -32,7 +33,7 @@ public class Application implements CommandLineRunner {
|
|||
loginPost.login();
|
||||
|
||||
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) {
|
||||
log.error("Error {} encountered, shutting down!", ex.getExitCode());
|
||||
|
|
|
@ -4,7 +4,8 @@ import lombok.Getter;
|
|||
|
||||
public enum ExitCode {
|
||||
|
||||
CONFIGURATION_MISSING(10, "Missing configuration property: ");
|
||||
CONFIGURATION_MISSING(10, "Missing configuration property: "),
|
||||
POST_ERROR(20, "Failed POST request, RC: ");
|
||||
|
||||
@Getter
|
||||
private String reason;
|
||||
|
|
|
@ -3,7 +3,7 @@ package cz.brmlab.wm.wekan.pojo.card;
|
|||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
public class CardRequest {
|
||||
public class PostCardRequest {
|
||||
|
||||
private String title;
|
||||
private String description;
|
|
@ -3,8 +3,8 @@ package cz.brmlab.wm.wekan.pojo.card;
|
|||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
public class CardResponse {
|
||||
public class PostCardResponse {
|
||||
|
||||
private String id;
|
||||
private String _id;
|
||||
|
||||
}
|
|
@ -1,44 +1,59 @@
|
|||
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.pojo.card.CardRequest;
|
||||
import cz.brmlab.wm.wekan.pojo.card.CardResponse;
|
||||
import cz.brmlab.wm.wekan.pojo.card.PostCardRequest;
|
||||
import cz.brmlab.wm.wekan.pojo.card.PostCardResponse;
|
||||
import cz.brmlab.wm.wekan.pojo.login.LoginToken;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.http.*;
|
||||
import org.springframework.web.client.HttpStatusCodeException;
|
||||
import org.springframework.web.client.RestTemplate;
|
||||
|
||||
import java.util.Collections;
|
||||
|
||||
@Slf4j
|
||||
public class CardPost {
|
||||
|
||||
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) {
|
||||
log.trace("{}() - start.", this.getClass().getSimpleName());
|
||||
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);
|
||||
|
||||
CardRequest cardRequest = new CardRequest();
|
||||
cardRequest.setAuthorId(token.getId());
|
||||
cardRequest.setTitle(title);
|
||||
cardRequest.setDescription(description);
|
||||
|
||||
String cardUrl = configuration.getWekanUrl()
|
||||
+ "/api/boards"
|
||||
+ configuration.getWekanBoard()
|
||||
+ "/lists/"
|
||||
+ configuration.getWekanList() + "cards";
|
||||
PostCardRequest postCardRequest = new PostCardRequest();
|
||||
postCardRequest.setAuthorId(token.getId());
|
||||
postCardRequest.setTitle(title);
|
||||
postCardRequest.setDescription(description);
|
||||
|
||||
RestTemplate restTemplate = new RestTemplate();
|
||||
log.debug("Sending card: {}", cardRequest);
|
||||
CardResponse cardResponse = restTemplate.postForObject(cardUrl, cardRequest, CardResponse.class);
|
||||
HttpHeaders headers = new HttpHeaders();
|
||||
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;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue