src/test/java/io/swagger/test/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 String invokeAPI(String path, String method, Map<String, String> queryParams, Object body, Map<String, String> headerParams, Entity<?> formParams, String accept, String contentType, String[] authNames) throws ApiException {
        updateParamsForAuth(authNames, queryParams, headerParams);
        final ClientConfig clientConfig = new ClientConfig();
        clientConfig.register(MultiPartFeature.class);
        if (debugging) {
            clientConfig.register(LoggingFilter.class);
        }
        Client client = ClientBuilder.newClient(clientConfig);
        WebTarget target = client.target(this.basePath).path(path);
        for (String key : queryParams.keySet()) {
            String value = queryParams.get(key);
            if (value != null) {
                target = target.queryParam(key, value);
            }
        }
        Invocation.Builder invocationBuilder = target.request(contentType);
        for (String key : headerParams.keySet()) {
            String value = headerParams.get(key);
            if (value != null) {
                invocationBuilder = invocationBuilder.header(key, value);
            }
        }
        for (String key : defaultHeaderMap.keySet()) {
            if (!headerParams.containsKey(key)) {
                String value = defaultHeaderMap.get(key);
                if (value != null) {
                    invocationBuilder = invocationBuilder.header(key, value);
                }
            }
        }
        Response response = null;
        invocationBuilder = invocationBuilder.accept(accept);
        if ("GET".equals(method)) {
            response = invocationBuilder.get();
        } else if ("POST".equals(method)) {
            if (formParams != null) {
                response = invocationBuilder.post(formParams);
            } else if (body == null) {
                response = invocationBuilder.post(null);
            } else {
                response = invocationBuilder.post(Entity.json(serialize(body)));
            }
        } else if ("PUT".equals(method)) {
            if (formParams != null) {
                response = invocationBuilder.put(formParams);
            } else if (body == null) {
                response = invocationBuilder.put(null);
            } else {
                response = invocationBuilder.put(Entity.json(serialize(body)));
            }
        } else if ("DELETE".equals(method)) {
            response = invocationBuilder.delete();
        } else {
            throw new ApiException(500, "unknown method type " + method);
        }
        if (response.getStatus() == Status.NO_CONTENT.getStatusCode()) {
            return null;
        } else if (response.getStatusInfo().getFamily().equals(Status.Family.SUCCESSFUL)) {
            if (response.hasEntity()) {
                return (String) response.readEntity(String.class);
            } else {
                return "";
            }
        } else {
            String message = "error";
            String respBody = null;
            Map<String, List<String>> responseHeaders = new HashMap<String, List<String>>();
            if (response.hasEntity()) {
                try {
                    message = String.valueOf(response.readEntity(String.class));
                    for (String key : response.getHeaders().keySet()) {
                        List<Object> values = response.getHeaders().get(key);
                        for (Object o : values) {
                            List<String> headers = responseHeaders.get(key);
                            if (headers == null) {
                                headers = new ArrayList<String>();
                                responseHeaders.put(key, headers);
                            }
                            headers.add(String.valueOf(o));
                        }
                    }
                } catch (RuntimeException e) {
                }
            }
            throw new ApiException(
                    response.getStatusInfo().getStatusCode(),
                    message,
                    responseHeaders,
                    respBody);
        }
    }
public String invokeAPI ( String path , String method , Map < String , String > queryParams , Object body , Map < String , String > headerParams , Entity < ? > formParams , String accept , String contentType , String [ ] authNames ) throws ApiException { updateParamsForAuth ( authNames , queryParams , headerParams ) ; final ClientConfig clientConfig = new ClientConfig ( ) ; clientConfig . register ( MultiPartFeature . class ) ; if ( debugging ) { clientConfig . register ( LoggingFilter . class ) ; } Client client = ClientBuilder . newClient ( clientConfig ) ; WebTarget target = client . target ( this . basePath ) . path ( path ) ; for ( String key : queryParams . keySet ( ) ) { String value = queryParams . get ( key ) ; if ( value != null ) { target = target . queryParam ( key , value ) ; } } Invocation . Builder invocationBuilder = target . request ( contentType ) ; for ( String key : headerParams . keySet ( ) ) { String value = headerParams . get ( key ) ; if ( value != null ) { invocationBuilder = invocationBuilder . header ( key , value ) ; } } for ( String key : defaultHeaderMap . keySet ( ) ) { if ( ! headerParams . containsKey ( key ) ) { String value = defaultHeaderMap . get ( key ) ; if ( value != null ) { invocationBuilder = invocationBuilder . header ( key , value ) ; } } } Response response = null ; invocationBuilder = invocationBuilder . accept ( accept ) ; if ( "GET" . equals ( method ) ) { response = invocationBuilder . get ( ) ; } else if ( "POST" . equals ( method ) ) { if ( formParams != null ) { response = invocationBuilder . post ( formParams ) ; } else if ( body == null ) { response = invocationBuilder . post ( null ) ; } else { response = invocationBuilder . post ( Entity . json ( serialize ( body ) ) ) ; } } else if ( "PUT" . equals ( method ) ) { if ( formParams != null ) { response = invocationBuilder . put ( formParams ) ; } else if ( body == null ) { response = invocationBuilder . put ( null ) ; } else { response = invocationBuilder . put ( Entity . json ( serialize ( body ) ) ) ; } } else if ( "DELETE" . equals ( method ) ) { response = invocationBuilder . delete ( ) ; } else { throw new ApiException ( 500 , "unknown method type " + method ) ; } if ( response . getStatus ( ) == Status . NO_CONTENT . getStatusCode ( ) ) { return null ; } else if ( response . getStatusInfo ( ) . getFamily ( ) . equals ( Status . Family . SUCCESSFUL ) ) { if ( response . hasEntity ( ) ) { return ( String ) response . readEntity ( String . class ) ; } else { return "" ; } } else { String message = "error" ; String respBody = null ; Map < String , List < String > > responseHeaders = new HashMap < String , List < String > > ( ) ; if ( response . hasEntity ( ) ) { try { message = String . valueOf ( response . readEntity ( String . class ) ) ; for ( String key : response . getHeaders ( ) . keySet ( ) ) { List < Object > values = response . getHeaders ( ) . get ( key ) ; for ( Object o : values ) { List < String > headers = responseHeaders . get ( key ) ; if ( headers == null ) { headers = new ArrayList < String > ( ) ; responseHeaders . put ( key , headers ) ; } headers . add ( String . valueOf ( o ) ) ; } } } catch ( RuntimeException e ) { } } throw new ApiException ( response . getStatusInfo ( ) . getStatusCode ( ) , message , responseHeaders , respBody ) ; } }
Invoke API by sending HTTP request with the given options.
Invoke API by sending HTTP request with the given options .
// e.printStackTrace();