1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34 package fr.paris.lutece.plugins.referentielfo.v1.rs;
35
36 import com.fasterxml.jackson.core.JsonProcessingException;
37 import com.fasterxml.jackson.databind.ObjectMapper;
38 import com.google.gson.reflect.TypeToken;
39 import fr.paris.lutece.plugins.referentielfo.v1.model.CycleScolaire;
40 import fr.paris.lutece.plugins.referentielfo.v1.model.exception.ReferentielException;
41 import fr.paris.lutece.portal.service.util.AppLogService;
42 import fr.paris.lutece.util.httpaccess.HttpAccess;
43 import fr.paris.lutece.util.httpaccess.HttpAccessException;
44
45 import java.util.ArrayList;
46 import java.util.List;
47 import java.util.Map;
48 import java.util.Optional;
49
50 public class ReferentielCycleScolaireService extends AbstractWSReferentiel implements IReferentielCycleScolaireService
51 {
52 @Deprecated
53 public ReferentielCycleScolaireService( String referentielBaseUrl )
54 {
55 super( referentielBaseUrl );
56 }
57 public ReferentielCycleScolaireService( String referentielBaseUrl, String xGraviteeApiKey )
58 {
59 super( referentielBaseUrl, xGraviteeApiKey );
60 }
61
62 @Override
63 public CycleScolaire recuperationCycleScolaireParValeur( Integer valeur ) throws ReferentielException
64 {
65 HttpAccess httpAccess = new HttpAccess( );
66 try
67 {
68 String apiUrl = getReferentielBaseUrl( ) + Constants.RECUPERATION_CYCLESCOLAIRE_PAR_VALEUR + valeur;
69 Map<String, String> mapHeadersRequest = genereHeaderGravitee( null );
70 String response = httpAccess.doGet( apiUrl, null, null, mapHeadersRequest );
71
72 gererErreur( response );
73
74 ObjectMapper mapper = new ObjectMapper();
75
76 return mapper.readValue(response, CycleScolaire.class);
77 }
78 catch( HttpAccessException e )
79 {
80 AppLogService.error("error during mapping response", e);
81 throw new ReferentielException( e );
82 }
83 catch ( JsonProcessingException e )
84 {
85 throw new ReferentielException( e );
86 }
87 }
88
89 @Override
90 public List<CycleScolaire> recuperationListeCycleScolaire( ) throws ReferentielException
91 {
92 HttpAccess httpAccess = new HttpAccess( );
93 try
94 {
95 String apiUrl = getReferentielBaseUrl( ) + Constants.RECUPERATION_LISTE_CYCLESCOLAIRE;
96 Map<String, String> mapHeadersRequest = genereHeaderGravitee( null );
97 String response = httpAccess.doGet( apiUrl, null, null, mapHeadersRequest );
98
99 gererErreur( response );
100
101 return ( List<CycleScolaire> ) Optional.ofNullable( getGson( ).fromJson( response, new TypeToken<ArrayList<CycleScolaire>>( ) {}.getType( ) ) ).orElse( new ArrayList<>( ) );
102 }
103 catch( HttpAccessException e )
104 {
105 AppLogService.error("error during mapping response", e);
106 throw new ReferentielException( e );
107 }
108 }
109 }