Skip to content

Instantly share code, notes, and snippets.

@Fabien-V
Created September 27, 2019 08:27
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 Fabien-V/fa6534159c52509ab296f128b8fe890c to your computer and use it in GitHub Desktop.
Save Fabien-V/fa6534159c52509ab296f128b8fe890c to your computer and use it in GitHub Desktop.
package com.example.demo.callNP6;
import com.example.demo.entities.Segment;
import org.springframework.http.*;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
import java.util.Collections;
@Service
public class SegmentsServices {
RestTemplate restTemplate = new RestTemplate();
/*GET ALL SEGMENTS*/
public Segment[] getAllSegments() {
// Définition des entêtes HTTP
HttpHeaders headers = new HttpHeaders();
headers.set("x-key", "BLABLABLA");
headers.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON));
// Définition de l'objet pour le protocole HTTP
HttpEntity entity = new HttpEntity<String>(headers);
// Appel de restTemplate avec la config http définie précédemment
return restTemplate.exchange(
"https://api-cm.np6.com/segments/",
HttpMethod.GET,
entity,
Segment[].class).getBody();
}
/*GET A SPECIFIC SEGMENT*/
public Segment getSpecificSegment(String id) {
// Definition des entetes HTTP
HttpHeaders headers = new HttpHeaders();
headers.set("x-key", "BLABLABLA");
headers.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON));
// Définition de l'objet pour le protocole HTTP
HttpEntity<String> entity = new HttpEntity<String>(headers);
// Appel de restTemplate avec la config http définie précédemment
Segment mySpecificSegment = restTemplate.exchange(
"https://api-cm.np6.com/segments/" + id,
HttpMethod.GET,
entity,
Segment.class).getBody();
return mySpecificSegment;
}
/*CREATE A SEGMENT*/
public Segment createSegment(String segment) {
// Entete authentification pour appel API
HttpHeaders headers = new HttpHeaders();
headers.set("x-key", "BLABLABLA");
headers.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON));
// Entete spécifique POST
headers.setContentType(MediaType.APPLICATION_JSON_UTF8);
headers.setCacheControl("no-cache");
// Creation requete avec les entetes et l'objet à passer en POST
HttpEntity<String> entity = new HttpEntity<String>(segment, headers);
ResponseEntity<Segment> result = restTemplate.exchange(
"https://api-cm.np6.com/segments/",
HttpMethod.POST,
entity,
Segment.class); // Renvoi un json de type Segment
System.out.println(result.getStatusCodeValue());
return result.getBody();
}
/*UPDATE A SPECIFIC SEGMENT*/
public Segment updateSegment(String id, String segment) {
// Entete authentification pour appel API
HttpHeaders headers = new HttpHeaders();
headers.set("x-key", "BLABLABLA");
headers.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON));
// Entete spécifique PUT
headers.setContentType(MediaType.APPLICATION_JSON_UTF8);
headers.setCacheControl("no-cache");
// Creation requete avec les entetes et l'objet à passer en UPDATE
HttpEntity<String> entity = new HttpEntity<String>(segment, headers);
// Appel de restTemplate avec la config http définie précédemment
ResponseEntity<Segment> result = restTemplate.exchange(
"https://api-cm.np6.com/segments/" + id,
HttpMethod.PUT,
entity,
Segment.class);
System.out.println(result.getStatusCodeValue());
return result.getBody();
}
/*EMPTY A SPECIFIC SEGMENT*/
public Segment emptySegment(String id) {
// Entete authentification pour appel API
HttpHeaders headers = new HttpHeaders();
headers.set("x-key", "BLABLABLA");
//headers.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON)); INUTILE ICI
// Creation requete avec les entetes et l'objet à passer en DELETE
HttpEntity<String> entity = new HttpEntity<String>(headers);
// Appel de restTemplate avec la config http définie précédemment
Segment myEmptySegment = restTemplate.exchange(
"https://api-cm.np6.com/segments/" + id + "/targets",
HttpMethod.DELETE,
entity,
Segment.class).getBody();
return myEmptySegment;
}
/*DELETE A SPECIFIC SEGMENT*/
public Segment deleteSegment(String id) {
// Entete authentification pour appel API
HttpHeaders headers = new HttpHeaders();
headers.set("x-key", "BLABLABLA");
// Creation requete avec les entetes et l'objet à passer en DELETE
HttpEntity<String> entity = new HttpEntity<String>(headers);
// Appel de restTemplate avec la config http définie précédemment
Segment myDeletedSegment = restTemplate.exchange(
"https://api-cm.np6.com/segments/" + id,
HttpMethod.DELETE,
entity,
Segment.class).getBody();
return myDeletedSegment;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment