6666 */
6767public class ApiClient {
6868
69+ protected String basePath = "http://localhost" ;
6970 protected String basePath = "http://localhost" ;
7071 protected List <ServerConfiguration > servers = new ArrayList <ServerConfiguration >(Arrays .asList (
7172 new ServerConfiguration (
@@ -80,21 +81,36 @@ public class ApiClient {
8081 protected Map <String , String > defaultHeaderMap = new HashMap <String , String >();
8182 protected Map <String , String > defaultCookieMap = new HashMap <String , String >();
8283 protected String tempFolderPath = null ;
84+ protected boolean debugging = false ;
85+ protected Map <String , String > defaultHeaderMap = new HashMap <String , String >();
86+ protected Map <String , String > defaultCookieMap = new HashMap <String , String >();
87+ protected String tempFolderPath = null ;
8388
89+ protected Map <String , Authentication > authentications ;
8490 protected Map <String , Authentication > authentications ;
8591
92+ protected DateFormat dateFormat ;
93+ protected DateFormat datetimeFormat ;
94+ protected boolean lenientDatetimeFormat ;
95+ protected int dateLength ;
8696 protected DateFormat dateFormat ;
8797 protected DateFormat datetimeFormat ;
8898 protected boolean lenientDatetimeFormat ;
8999 protected int dateLength ;
90100
101+ protected InputStream sslCaCert ;
102+ protected boolean verifyingSsl ;
103+ protected KeyManager [] keyManagers ;
91104 protected InputStream sslCaCert ;
92105 protected boolean verifyingSsl ;
93106 protected KeyManager [] keyManagers ;
94107
108+ protected OkHttpClient httpClient ;
109+ protected JSON json ;
95110 protected OkHttpClient httpClient ;
96111 protected JSON json ;
97112
113+ protected HttpLoggingInterceptor loggingInterceptor ;
98114 protected HttpLoggingInterceptor loggingInterceptor ;
99115
100116 /**
@@ -199,6 +215,7 @@ protected void initHttpClient() {
199215 initHttpClient (Collections .<Interceptor >emptyList ());
200216 }
201217
218+ protected void initHttpClient (List <Interceptor > interceptors ) {
202219 protected void initHttpClient (List <Interceptor > interceptors ) {
203220 OkHttpClient .Builder builder = new OkHttpClient .Builder ();
204221 builder .addNetworkInterceptor (getProgressInterceptor ());
@@ -209,6 +226,7 @@ protected void initHttpClient(List<Interceptor> interceptors) {
209226 httpClient = builder .build ();
210227 }
211228
229+ protected void init () {
212230 protected void init () {
213231 verifyingSsl = true ;
214232
@@ -234,6 +252,8 @@ public String getBasePath() {
234252 *
235253 * @param basePath Base path of the URL (e.g http://localhost)
236254 * @return An instance of ApiClient
255+ * @param basePath Base path of the URL (e.g http://localhost)
256+ * @return An instance of ApiClient
237257 */
238258 public ApiClient setBasePath (String basePath ) {
239259 this .basePath = basePath ;
@@ -282,6 +302,7 @@ public OkHttpClient getHttpClient() {
282302 *
283303 * @param newHttpClient An instance of OkHttpClient
284304 * @return ApiClient
305+ * @return ApiClient
285306 * @throws java.lang.NullPointerException when newHttpClient is null
286307 */
287308 public ApiClient setHttpClient (OkHttpClient newHttpClient ) {
@@ -789,6 +810,7 @@ public List<Pair> parameterToPair(String name, Object value) {
789810 * @param value The value of the parameter.
790811 * @return A list of {@code Pair} objects.
791812 */
813+ public List <Pair > parameterToPairs (String collectionFormat , String name , Collection <?> value ) {
792814 public List <Pair > parameterToPairs (String collectionFormat , String name , Collection <?> value ) {
793815 List <Pair > params = new ArrayList <Pair >();
794816
@@ -897,6 +919,7 @@ public String collectionPathParameterToString(String collectionFormat, Collectio
897919 */
898920 public String sanitizeFilename (String filename ) {
899921 return filename .replaceFirst ("^.*[/\\ \\ ]" , "" );
922+ return filename .replaceFirst ("^.*[/\\ \\ ]" , "" );
900923 }
901924
902925 /**
@@ -1006,6 +1029,8 @@ public <T> T deserialize(Response response, Type returnType) throws ApiException
10061029 return (T ) downloadFileFromResponse (response );
10071030 }
10081031
1032+ ResponseBody respBody = response .body ();
1033+ if (respBody == null ) {
10091034 ResponseBody respBody = response .body ();
10101035 if (respBody == null ) {
10111036 return null ;
@@ -1016,6 +1041,18 @@ public <T> T deserialize(Response response, Type returnType) throws ApiException
10161041 // ensuring a default content type
10171042 contentType = "application/json" ;
10181043 }
1044+ try {
1045+ if (isJsonMime (contentType )) {
1046+ return JSON .deserialize (respBody .byteStream (), returnType );
1047+ } else if (returnType .equals (String .class )) {
1048+ String respBodyString = respBody .string ();
1049+ if (respBodyString .isEmpty ()) {
1050+ return null ;
1051+ }
1052+ // Expecting string, return the raw response body.
1053+ return (T ) respBodyString ;
1054+ } else {
1055+ throw new ApiException (
10191056 try {
10201057 if (isJsonMime (contentType )) {
10211058 return JSON .deserialize (respBody .byteStream (), returnType );
@@ -1033,6 +1070,10 @@ public <T> T deserialize(Response response, Type returnType) throws ApiException
10331070 response .headers ().toMultimap (),
10341071 response .body ().string ());
10351072 }
1073+ } catch (IOException e ) {
1074+ throw new ApiException (e );
1075+ response .body ().string ());
1076+ }
10361077 } catch (IOException e ) {
10371078 throw new ApiException (e );
10381079 }
@@ -1363,6 +1404,8 @@ public String buildUrl(String baseUrl, String path, List<Pair> queryParams, List
13631404 throw new ArrayIndexOutOfBoundsException (String .format (
13641405 Locale .ROOT ,
13651406 "Invalid index %d when selecting the host settings. Must be less than %d" , serverIndex , servers .size ()
1407+ Locale .ROOT ,
1408+ "Invalid index %d when selecting the host settings. Must be less than %d" , serverIndex , servers .size ()
13661409 ));
13671410 }
13681411 baseURL = servers .get (serverIndex ).URL (serverVariables );
@@ -1435,10 +1478,12 @@ public void processHeaderParams(Map<String, String> headerParams, Request.Builde
14351478 public void processCookieParams (Map <String , String > cookieParams , Request .Builder reqBuilder ) {
14361479 for (Entry <String , String > param : cookieParams .entrySet ()) {
14371480 reqBuilder .addHeader ("Cookie" , String .format (Locale .ROOT , "%s=%s" , param .getKey (), param .getValue ()));
1481+ reqBuilder .addHeader ("Cookie" , String .format (Locale .ROOT , "%s=%s" , param .getKey (), param .getValue ()));
14381482 }
14391483 for (Entry <String , String > param : defaultCookieMap .entrySet ()) {
14401484 if (!cookieParams .containsKey (param .getKey ())) {
14411485 reqBuilder .addHeader ("Cookie" , String .format (Locale .ROOT , "%s=%s" , param .getKey (), param .getValue ()));
1486+ reqBuilder .addHeader ("Cookie" , String .format (Locale .ROOT , "%s=%s" , param .getKey (), param .getValue ()));
14421487 }
14431488 }
14441489 }
@@ -1531,6 +1576,7 @@ public String guessContentTypeFromFile(File file) {
15311576 * @param key The key of the Header element
15321577 * @param file The file to add to the Header
15331578 */
1579+ protected void addPartToMultiPartBuilder (MultipartBody .Builder mpBuilder , String key , File file ) {
15341580 protected void addPartToMultiPartBuilder (MultipartBody .Builder mpBuilder , String key , File file ) {
15351581 Headers partHeaders = Headers .of ("Content-Disposition" , "form-data; name=\" " + key + "\" ; filename=\" " + file .getName () + "\" " );
15361582 MediaType mediaType = MediaType .parse (guessContentTypeFromFile (file ));
@@ -1544,6 +1590,7 @@ protected void addPartToMultiPartBuilder(MultipartBody.Builder mpBuilder, String
15441590 * @param key The key of the Header element
15451591 * @param obj The complex object to add to the Header
15461592 */
1593+ protected void addPartToMultiPartBuilder (MultipartBody .Builder mpBuilder , String key , Object obj ) {
15471594 protected void addPartToMultiPartBuilder (MultipartBody .Builder mpBuilder , String key , Object obj ) {
15481595 RequestBody requestBody ;
15491596 if (obj instanceof String ) {
@@ -1566,6 +1613,7 @@ protected void addPartToMultiPartBuilder(MultipartBody.Builder mpBuilder, String
15661613 * Get network interceptor to add it to the httpClient to track download progress for
15671614 * async requests.
15681615 */
1616+ protected Interceptor getProgressInterceptor () {
15691617 protected Interceptor getProgressInterceptor () {
15701618 return new Interceptor () {
15711619 @ Override
@@ -1587,6 +1635,7 @@ public Response intercept(Interceptor.Chain chain) throws IOException {
15871635 * Apply SSL related settings to httpClient according to the current values of
15881636 * verifyingSsl and sslCaCert.
15891637 */
1638+ protected void applySslSettings () {
15901639 protected void applySslSettings () {
15911640 try {
15921641 TrustManager [] trustManagers ;
@@ -1649,6 +1698,7 @@ public boolean verify(String hostname, SSLSession session) {
16491698 }
16501699 }
16511700
1701+ protected KeyStore newEmptyKeyStore (char[] password ) throws GeneralSecurityException {
16521702 protected KeyStore newEmptyKeyStore (char[] password ) throws GeneralSecurityException {
16531703 try {
16541704 KeyStore keyStore = KeyStore .getInstance (KeyStore .getDefaultType ());
@@ -1666,6 +1716,7 @@ protected KeyStore newEmptyKeyStore(char[] password) throws GeneralSecurityExcep
16661716 * @return The string representation of the HTTP request body
16671717 * @throws com.bandwidth.sdk.ApiException If fail to serialize the request body object into a string
16681718 */
1719+ protected String requestBodyToString (RequestBody requestBody ) throws ApiException {
16691720 protected String requestBodyToString (RequestBody requestBody ) throws ApiException {
16701721 if (requestBody != null ) {
16711722 try {
0 commit comments