5656import java .util .Date ;
5757import java .util .function .Supplier ;
5858import java .util .TimeZone ;
59- import java .util .concurrent .ConcurrentHashMap ;
6059import java .util .regex .Matcher ;
6160import java .util .regex .Pattern ;
6261
8079import software .xdev .sessionize .client .auth .Authentication ;
8180
8281public class ApiClient extends JavaTimeFormatter {
83- private Map <String , String > defaultHeaderMap = new HashMap <String , String >();
84- private Map <String , String > defaultCookieMap = new HashMap <String , String >();
85- private String basePath = "https://sessionize.com" ;
82+ protected Map <String , String > defaultHeaderMap = new HashMap <String , String >();
83+ protected Map <String , String > defaultCookieMap = new HashMap <String , String >();
84+ protected String basePath = "https://sessionize.com" ;
8685 protected List <ServerConfiguration > servers = new ArrayList <ServerConfiguration >(Arrays .asList (
8786 new ServerConfiguration (
8887 "https://sessionize.com" ,
@@ -92,22 +91,22 @@ public class ApiClient extends JavaTimeFormatter {
9291 ));
9392 protected Integer serverIndex = 0 ;
9493 protected Map <String , String > serverVariables = null ;
95- private boolean debugging = false ;
96- private int connectionTimeout = 0 ;
94+ protected boolean debugging = false ;
95+ protected int connectionTimeout = 0 ;
9796
98- private CloseableHttpClient httpClient ;
99- private ObjectMapper objectMapper ;
97+ protected CloseableHttpClient httpClient ;
98+ protected ObjectMapper objectMapper ;
10099 protected String tempFolderPath = null ;
101100
102- private Map <String , Authentication > authentications ;
101+ protected Map <String , Authentication > authentications ;
103102
104- private Map < Long , Integer > lastStatusCodeByThread = new ConcurrentHashMap <>();
105- private Map < Long , Map <String , List <String >>> lastResponseHeadersByThread = new ConcurrentHashMap <>();
103+ protected ThreadLocal < Integer > lastStatusCode = new ThreadLocal <>();
104+ protected ThreadLocal < Map <String , List <String >>> lastResponseHeaders = new ThreadLocal <>();
106105
107- private DateFormat dateFormat ;
106+ protected DateFormat dateFormat ;
108107
109108 // Methods that can have a request body
110- private static List <String > bodyMethods = Arrays .asList ("POST" , "PUT" , "DELETE" , "PATCH" );
109+ protected static List <String > bodyMethods = Arrays .asList ("POST" , "PUT" , "DELETE" , "PATCH" );
111110
112111 public ApiClient (CloseableHttpClient httpClient ) {
113112 objectMapper = new ObjectMapper ();
@@ -119,6 +118,7 @@ public ApiClient(CloseableHttpClient httpClient) {
119118 objectMapper .enable (DeserializationFeature .READ_ENUMS_USING_TO_STRING );
120119 objectMapper .registerModule (new JavaTimeModule ());
121120 objectMapper .registerModule (new JsonNullableModule ());
121+ objectMapper .registerModule (new RFC3339JavaTimeModule ());
122122 objectMapper .setDateFormat (ApiClient .buildDefaultDateFormat ());
123123
124124 dateFormat = ApiClient .buildDefaultDateFormat ();
@@ -248,7 +248,7 @@ public ApiClient setServerVariables(Map<String, String> serverVariables) {
248248 */
249249 @ Deprecated
250250 public int getStatusCode () {
251- return lastStatusCodeByThread .get (Thread . currentThread (). getId () );
251+ return lastStatusCode .get ();
252252 }
253253
254254 /**
@@ -257,7 +257,7 @@ public int getStatusCode() {
257257 */
258258 @ Deprecated
259259 public Map <String , List <String >> getResponseHeaders () {
260- return lastResponseHeadersByThread .get (Thread . currentThread (). getId () );
260+ return lastResponseHeaders .get ();
261261 }
262262
263263 /**
@@ -320,7 +320,7 @@ public ApiClient setTempFolderPath(String tempFolderPath) {
320320 * @param value The header's value
321321 * @return API client
322322 */
323- public ApiClient addDefaultHeader (String key , String value ) {
323+ public final ApiClient addDefaultHeader (String key , String value ) {
324324 defaultHeaderMap .put (key , value );
325325 return this ;
326326 }
@@ -610,7 +610,7 @@ protected Map<String, List<String>> transformResponseHeaders(Header[] headers) {
610610 /**
611611 * Parse content type object from header value
612612 */
613- private ContentType getContentType (String headerValue ) throws ApiException {
613+ protected ContentType getContentType (String headerValue ) throws ApiException {
614614 try {
615615 return ContentType .parse (headerValue );
616616 } catch (UnsupportedCharsetException e ) {
@@ -621,7 +621,7 @@ private ContentType getContentType(String headerValue) throws ApiException {
621621 /**
622622 * Get content type of a response or null if one was not provided
623623 */
624- private String getResponseMimeType (HttpResponse response ) throws ApiException {
624+ protected String getResponseMimeType (HttpResponse response ) throws ApiException {
625625 Header contentTypeHeader = response .getFirstHeader ("Content-Type" );
626626 if (contentTypeHeader != null ) {
627627 return getContentType (contentTypeHeader .getValue ()).getMimeType ();
@@ -730,7 +730,7 @@ public <T> T deserialize(CloseableHttpResponse response, TypeReference<T> valueT
730730 }
731731 }
732732
733- private File downloadFileFromResponse (CloseableHttpResponse response ) throws IOException {
733+ protected File downloadFileFromResponse (CloseableHttpResponse response ) throws IOException {
734734 Header contentDispositionHeader = response .getFirstHeader ("Content-Disposition" );
735735 String contentDisposition = contentDispositionHeader == null ? null : contentDispositionHeader .getValue ();
736736 File file = prepareDownloadFile (contentDisposition );
@@ -782,6 +782,7 @@ public String getBaseURL() {
782782 if (serverIndex != null ) {
783783 if (serverIndex < 0 || serverIndex >= servers .size ()) {
784784 throw new ArrayIndexOutOfBoundsException (String .format (
785+ java .util .Locale .ROOT ,
785786 "Invalid index %d when selecting the host settings. Must be less than %d" , serverIndex , servers .size ()
786787 ));
787788 }
@@ -801,7 +802,7 @@ public String getBaseURL() {
801802 * @param urlQueryDeepObject URL query string of the deep object parameters
802803 * @return The full URL
803804 */
804- private String buildUrl (String path , List <Pair > queryParams , List <Pair > collectionQueryParams , String urlQueryDeepObject ) {
805+ protected String buildUrl (String path , List <Pair > queryParams , List <Pair > collectionQueryParams , String urlQueryDeepObject ) {
805806 String baseURL = getBaseURL ();
806807
807808 final StringBuilder url = new StringBuilder ();
@@ -867,13 +868,13 @@ protected Cookie buildCookie(String key, String value, URI uri) {
867868
868869 protected <T > T processResponse (CloseableHttpResponse response , TypeReference <T > returnType ) throws ApiException , IOException , ParseException {
869870 int statusCode = response .getCode ();
870- lastStatusCodeByThread . put ( Thread . currentThread (). getId (), statusCode );
871+ lastStatusCode . set ( statusCode );
871872 if (statusCode == HttpStatus .SC_NO_CONTENT ) {
872873 return null ;
873874 }
874875
875876 Map <String , List <String >> responseHeaders = transformResponseHeaders (response .getHeaders ());
876- lastResponseHeadersByThread . put ( Thread . currentThread (). getId (), responseHeaders );
877+ lastResponseHeaders . set ( responseHeaders );
877878
878879 if (isSuccessfulStatus (statusCode )) {
879880 return this .deserialize (response , returnType );
@@ -980,7 +981,7 @@ public <T> T invokeAPI(
980981 * @param headerParams Header parameters
981982 * @param cookieParams Cookie parameters
982983 */
983- private void updateParamsForAuth (String [] authNames , List <Pair > queryParams , Map <String , String > headerParams , Map <String , String > cookieParams ) {
984+ protected void updateParamsForAuth (String [] authNames , List <Pair > queryParams , Map <String , String > headerParams , Map <String , String > cookieParams ) {
984985 for (String authName : authNames ) {
985986 Authentication auth = authentications .get (authName );
986987 if (auth == null ) throw new RuntimeException ("Authentication undefined: " + authName );
0 commit comments