66import java .io .File ;
77import java .io .FileInputStream ;
88import java .io .IOException ;
9+ import java .text .DateFormat ;
10+ import java .text .SimpleDateFormat ;
911import java .util .Properties ;
1012
11- import retrofit .RestAdapter ;
13+ import com .fasterxml .jackson .annotation .JsonAutoDetect ;
14+ import com .fasterxml .jackson .annotation .JsonInclude ;
15+ import com .fasterxml .jackson .databind .ObjectMapper ;
16+ import com .fasterxml .jackson .databind .SerializationFeature ;
17+ import com .squareup .okhttp .Interceptor ;
18+ import com .squareup .okhttp .OkHttpClient ;
19+ import com .squareup .okhttp .Request ;
20+ import com .squareup .okhttp .Response ;
21+
22+ import retrofit .JacksonConverterFactory ;
23+ import retrofit .Retrofit ;
1224
1325import com .imsweb .seerapi .client .disease .DiseaseService ;
1426import com .imsweb .seerapi .client .glossary .GlossaryService ;
2335 */
2436public final class SeerApi {
2537
26- RestAdapter _restAdapter ;
38+ private Retrofit _retroFit ;
2739
2840 /**
2941 * Creates a client API root object
3042 * @param baseUrl base URL for API
3143 * @param apiKey API key
3244 */
33- private SeerApi (String baseUrl , String apiKey ) {
34- if (baseUrl .endsWith ("/" ))
35- baseUrl = baseUrl .substring (0 , baseUrl .length () - 1 );
36-
37- _restAdapter = new RestAdapter .Builder ()
38- .setEndpoint (baseUrl )
39- .setConverter (new SeerApiJacksonConverter ())
40- .setRequestInterceptor (new SeerApiRequestInterceptor (apiKey ))
41- .setErrorHandler (new SeerApiErrorHandler ())
45+ private SeerApi (String baseUrl , final String apiKey ) {
46+ if (!baseUrl .endsWith ("/" ))
47+ baseUrl += "/" ;
48+
49+ OkHttpClient client = new OkHttpClient ();
50+ client .interceptors ().add (new Interceptor () {
51+ @ Override
52+ public Response intercept (Chain chain ) throws IOException {
53+ Request original = chain .request ();
54+
55+ // add the api key to all requests
56+ Request request = original .newBuilder ()
57+ .header ("Accept" , "application/json" )
58+ .header ("X-SEERAPI-Key" , apiKey )
59+ .method (original .method (), original .body ())
60+ .build ();
61+
62+ return chain .proceed (request );
63+ }
64+ });
65+ client .interceptors ().add (new ErrorInterceptor ());
66+
67+ _retroFit = new Retrofit .Builder ()
68+ .baseUrl (baseUrl )
69+ .addConverterFactory (JacksonConverterFactory .create (getMapper ()))
70+ .client (client )
4271 .build ();
4372 }
4473
74+ /**
75+ * Return the internal ObjectMapper
76+ * @return
77+ */
78+ protected static ObjectMapper getMapper () {
79+ ObjectMapper mapper = new ObjectMapper ();
80+
81+ // do not write null values
82+ mapper .configure (SerializationFeature .WRITE_NULL_MAP_VALUES , false );
83+ mapper .setSerializationInclusion (JsonInclude .Include .NON_NULL );
84+
85+ mapper .setVisibilityChecker (mapper .getSerializationConfig ().getDefaultVisibilityChecker ()
86+ .withFieldVisibility (JsonAutoDetect .Visibility .ANY )
87+ .withGetterVisibility (JsonAutoDetect .Visibility .NONE )
88+ .withSetterVisibility (JsonAutoDetect .Visibility .NONE )
89+ .withCreatorVisibility (JsonAutoDetect .Visibility .NONE ));
90+
91+ // set Date objects to output in readable customized format
92+ mapper .configure (SerializationFeature .WRITE_DATES_AS_TIMESTAMPS , false );
93+
94+ DateFormat dateFormat = new SimpleDateFormat ("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'" );
95+ mapper .setDateFormat (dateFormat );
96+
97+ return mapper ;
98+ }
99+
45100 /**
46101 * Return the disease service
47102 * @return an interface to all the disease APIs
48103 */
49104 public DiseaseService disease () {
50- return _restAdapter .create (DiseaseService .class );
105+ return _retroFit .create (DiseaseService .class );
51106 }
52107
53108 /**
54109 * Return the glossary service
55110 * @return an interface to all the glossary APIs
56111 */
57112 public GlossaryService glossary () {
58- return _restAdapter .create (GlossaryService .class );
113+ return _retroFit .create (GlossaryService .class );
59114 }
60115
61116 /**
62117 * Return the NAACCR service
63118 * @return an inteface to all the NAACCR APIs
64119 */
65120 public NaaccrService naaccr () {
66- return _restAdapter .create (NaaccrService .class );
121+ return _retroFit .create (NaaccrService .class );
67122 }
68123
69124 /**
70125 * Return the Rx service
71126 * @return an inteface to all the Rx APIs
72127 */
73128 public RxService rx () {
74- return _restAdapter .create (RxService .class );
129+ return _retroFit .create (RxService .class );
75130 }
76131
77132 /**
78133 * Return the site recode service
79134 * @return an interface to all the site recode APIs
80135 */
81136 public SiteRecodeService siteRecode () {
82- return _restAdapter .create (SiteRecodeService .class );
137+ return _retroFit .create (SiteRecodeService .class );
83138 }
84139
85140 /**
86141 * Return the staging service
87142 * @return an interface to all the staging APIs
88143 */
89144 public StagingService staging () {
90- return _restAdapter .create (StagingService .class );
145+ return _retroFit .create (StagingService .class );
91146 }
92147
93148 /**
94149 * Return the surgery service
95150 * @return an interface to all the surgery APIs
96151 */
97152 public SurgeryService surgery () {
98- return _restAdapter .create (SurgeryService .class );
153+ return _retroFit .create (SurgeryService .class );
99154 }
100155
101156 /**
@@ -104,7 +159,7 @@ public SurgeryService surgery() {
104159 public static class Builder {
105160
106161 // default base URL
107- private static final String _SEERAPI_URL = "https://api.seer.cancer.gov/rest" ;
162+ private static final String _SEERAPI_URL = "https://api.seer.cancer.gov/rest/ " ;
108163
109164 // environment variable for URL and API key
110165 private static final String _ENV_URL = "SEER_API_URL" ;
0 commit comments