src/main/java/net/systran/platform/translation/client/ApiClient.java
/**
   * Invoke API by sending HTTP request with the given options.
   *
   * @param path The sub-path of the HTTP URL
   * @param method The request method, one of "GET", "POST", "PUT", and "DELETE"
   * @param queryParams The query parameters
   * @param body The request body object
   * @param headerParams The header parameters
   * @param formParams The form parameters
   * @param accept The request's Accept header
   * @param contentType The request's Content-Type header
   * @param authNames The authentications to apply
   * @return The response body in type of string
   */
Invoke API by sending HTTP request with the given options.
Invoke API by sending HTTP request with the given options .
public ClientResponse invokeAPI(String path, String method, Map<String, Object> queryParams, Object body, Map<String, String> headerParams, Map<String, String> formParams, String accept, String contentType, String[] authNames) throws ApiException {
    updateParamsForAuth(authNames, queryParams, headerParams);
    Client client = getClient();
    StringBuilder b = new StringBuilder();
    for(String key : queryParams.keySet()) {
      Object value = queryParams.get(key);
      if (value != null){
        if (value instanceof String[]) {
          for (String val : (String[])value) {
            if(b.toString().length() == 0)
              b.append("?");
            else
              b.append("&");
            b.append(escapeString(key)).append("=").append(escapeString(val));
          }
        } else {
          if(b.toString().length() == 0)
            b.append("?");
          else
            b.append("&");
          b.append(escapeString(key)).append("=").append(escapeString(value.toString()));
        }
      }
    }
    String querystring = b.toString();
    Builder builder;
    if (accept == null)
      builder = client.resource(basePath + path + querystring).getRequestBuilder();
    else
      builder = client.resource(basePath + path + querystring).accept(accept);
    for(String key : headerParams.keySet()) {
      builder = builder.header(key, headerParams.get(key));
    }
    for(String key : defaultHeaderMap.keySet()) {
      if(!headerParams.containsKey(key)) {
        builder = builder.header(key, defaultHeaderMap.get(key));
      }
    }
    ClientResponse response = null;
    if("GET".equals(method) && body == null) {
      response = (ClientResponse) builder.get(ClientResponse.class);
    }
    else if ("POST".equals(method) || ("GET".equals(method) && body != null)) {
      if ("GET".equals(method)) {
        builder = builder.header("X-HTTP-Method-Override", "GET");
      }
      if (contentType.startsWith("application/x-www-form-urlencoded")) {
        String encodedFormParams = this
            .getXWWWFormUrlencodedParams(formParams);
        response = builder.type(contentType).post(ClientResponse.class,
            encodedFormParams);
      } else if (body == null) {
        response = builder.post(ClientResponse.class, null);
      } else if(body instanceof FormDataMultiPart) {
        response = builder.type(contentType).post(ClientResponse.class, body);
      }
      else
        response = builder.type(contentType).post(ClientResponse.class, serialize(body));
    }
    else if ("PUT".equals(method)) {
      if ("application/x-www-form-urlencoded".equals(contentType)) {
          String encodedFormParams = this
              .getXWWWFormUrlencodedParams(formParams);
          response = builder.type(contentType).put(ClientResponse.class,
              encodedFormParams);
      } else if(body == null) {
        response = builder.put(ClientResponse.class, null);
      } else {
          response = builder.type(contentType).put(ClientResponse.class, serialize(body));
      }
    }
    else if ("DELETE".equals(method)) {
      if ("application/x-www-form-urlencoded".equals(contentType)) {
        String encodedFormParams = this
            .getXWWWFormUrlencodedParams(formParams);
        response = builder.type(contentType).delete(ClientResponse.class,
            encodedFormParams);
      } else if(body == null) {
        response = builder.delete(ClientResponse.class);
      } else {
        response = builder.type(contentType).delete(ClientResponse.class, serialize(body));
      }
    }
    else {
      throw new ApiException(500, "unknown method type " + method);
    }
    if(response.getClientResponseStatus() == ClientResponse.Status.NO_CONTENT) {
      return null;
    }
    else if(response.getClientResponseStatus().getFamily() == Family.SUCCESSFUL) {
      return response;
    }
    else {
      String message = "error";
      String respBody = null;
      if(response.hasEntity()) {
        try{
          respBody = String.valueOf(response.getEntity(String.class));
          message = respBody;
        }
        catch (RuntimeException e) {
        }
      }
      throw new ApiException(
                response.getClientResponseStatus().getStatusCode(),
                message,
                response.getHeaders(),
                respBody);
    }
  }
public ClientResponse invokeAPI ( String path , String method , Map < String , Object > queryParams , Object body , Map < String , String > headerParams , Map < String , String > formParams , String accept , String contentType , String [ ] authNames ) throws ApiException { updateParamsForAuth ( authNames , queryParams , headerParams ) ; Client client = getClient ( ) ; StringBuilder b = new StringBuilder ( ) ; for ( String key : queryParams . keySet ( ) ) { Object value = queryParams . get ( key ) ; if ( value != null ) { if ( value instanceof String [ ] ) { for ( String val : ( String [ ] ) value ) { if ( b . toString ( ) . length ( ) == 0 ) b . append ( "?" ) ; else b . append ( "&" ) ; b . append ( escapeString ( key ) ) . append ( "=" ) . append ( escapeString ( val ) ) ; } } else { if ( b . toString ( ) . length ( ) == 0 ) b . append ( "?" ) ; else b . append ( "&" ) ; b . append ( escapeString ( key ) ) . append ( "=" ) . append ( escapeString ( value . toString ( ) ) ) ; } } } String querystring = b . toString ( ) ; Builder builder ; if ( accept == null ) builder = client . resource ( basePath + path + querystring ) . getRequestBuilder ( ) ; else builder = client . resource ( basePath + path + querystring ) . accept ( accept ) ; for ( String key : headerParams . keySet ( ) ) { builder = builder . header ( key , headerParams . get ( key ) ) ; } for ( String key : defaultHeaderMap . keySet ( ) ) { if ( ! headerParams . containsKey ( key ) ) { builder = builder . header ( key , defaultHeaderMap . get ( key ) ) ; } } ClientResponse response = null ; if ( "GET" . equals ( method ) && body == null ) { response = ( ClientResponse ) builder . get ( ClientResponse . class ) ; } else if ( "POST" . equals ( method ) || ( "GET" . equals ( method ) && body != null ) ) { if ( "GET" . equals ( method ) ) { builder = builder . header ( "X-HTTP-Method-Override" , "GET" ) ; } if ( contentType . startsWith ( "application/x-www-form-urlencoded" ) ) { String encodedFormParams = this . getXWWWFormUrlencodedParams ( formParams ) ; response = builder . type ( contentType ) . post ( ClientResponse . class , encodedFormParams ) ; } else if ( body == null ) { response = builder . post ( ClientResponse . class , null ) ; } else if ( body instanceof FormDataMultiPart ) { response = builder . type ( contentType ) . post ( ClientResponse . class , body ) ; } else response = builder . type ( contentType ) . post ( ClientResponse . class , serialize ( body ) ) ; } else if ( "PUT" . equals ( method ) ) { if ( "application/x-www-form-urlencoded" . equals ( contentType ) ) { String encodedFormParams = this . getXWWWFormUrlencodedParams ( formParams ) ; response = builder . type ( contentType ) . put ( ClientResponse . class , encodedFormParams ) ; } else if ( body == null ) { response = builder . put ( ClientResponse . class , null ) ; } else { response = builder . type ( contentType ) . put ( ClientResponse . class , serialize ( body ) ) ; } } else if ( "DELETE" . equals ( method ) ) { if ( "application/x-www-form-urlencoded" . equals ( contentType ) ) { String encodedFormParams = this . getXWWWFormUrlencodedParams ( formParams ) ; response = builder . type ( contentType ) . delete ( ClientResponse . class , encodedFormParams ) ; } else if ( body == null ) { response = builder . delete ( ClientResponse . class ) ; } else { response = builder . type ( contentType ) . delete ( ClientResponse . class , serialize ( body ) ) ; } } else { throw new ApiException ( 500 , "unknown method type " + method ) ; } if ( response . getClientResponseStatus ( ) == ClientResponse . Status . NO_CONTENT ) { return null ; } else if ( response . getClientResponseStatus ( ) . getFamily ( ) == Family . SUCCESSFUL ) { return response ; } else { String message = "error" ; String respBody = null ; if ( response . hasEntity ( ) ) { try { respBody = String . valueOf ( response . getEntity ( String . class ) ) ; message = respBody ; } catch ( RuntimeException e ) { } } throw new ApiException ( response . getClientResponseStatus ( ) . getStatusCode ( ) , message , response . getHeaders ( ) , respBody ) ; } }
Invoke API by sending HTTP request with the given options.
Invoke API by sending HTTP request with the given options .
// e.printStackTrace();