Skip to content

Instantly share code, notes, and snippets.

@sirkkalap
Created July 10, 2017 12:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sirkkalap/078e796924c0dab1d5be95b3a96db3ec to your computer and use it in GitHub Desktop.
Save sirkkalap/078e796924c0dab1d5be95b3a96db3ec to your computer and use it in GitHub Desktop.
A snippet to demonstrate editing Cachet Incident message text.
...
package fi.XXX.digitraffic.status.dto.cachet;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;
@JsonIgnoreProperties(ignoreUnknown = true)
public class IncidentEditMsgDto {
public final Integer incident;
public final String message;
public IncidentEditMsgDto(
@JsonProperty("id") int incident,
@JsonProperty("message") String message) {
this.message = message;
this.incident = incident;
}
}
...
package fi.XXX.digitraffic.status.service;
import fi.XXX.digitraffic.status.dto.cachet.IncidentEditMsgDto;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.util.UriComponentsBuilder;
public class CachetService {
private final RestTemplate restTemplate;
public CachetService(String apiBase, String apiKey) {
this.apiBase = apiBase;
headers = new HttpHeaders();
headers.set("Accept", MediaType.APPLICATION_JSON_VALUE);
headers.set("X-Cachet-Token", apiKey);
restTemplate = new RestTemplate();
}
private void appendToIncidentMessage(IncidentDto incident, String messageTail) {
String newMessage = incident.message + messageTail;
final IncidentEditMsgDto editIncident = new IncidentEditMsgDto(incident.incident, newMessage);
final UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(apiBase + "/incidents").pathSegment(editIncident.incident.toString());
final HttpEntity<IncidentEditMsgDto> entity = new HttpEntity<>(editIncident, headers);
try {
restTemplate.exchange(builder.toUriString(), HttpMethod.PUT, entity, byte[].class);
} catch (RestClientException e) {
LOG.error(String.format("Error in request: %s, Error: %s", builder.toUriString(), e.getMessage()));
System.exit(1);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment