Search in sources :

Example 1 with HttpResponse

use of com.aliyuncs.fc.http.HttpResponse in project fc-java-sdk by aliyun.

the class DefaultFcClient method doAction.

public HttpResponse doAction(HttpRequest request, String form, String method) throws ClientException, ServerException {
    request.validate();
    try {
        PrepareUrl prepareUrl = signRequest(request, form, method);
        int retryTimes = 1;
        HttpResponse response = HttpResponse.getResponse(prepareUrl.getUrl(), prepareUrl.getHeader(), request, method, config.getConnectTimeoutMillis(), config.getReadTimeoutMillis());
        while (500 <= response.getStatus() && AUTO_RETRY && retryTimes < MAX_RETRIES) {
            prepareUrl = signRequest(request, form, method);
            response = HttpResponse.getResponse(prepareUrl.getUrl(), prepareUrl.getHeader(), request, method, config.getConnectTimeoutMillis(), config.getReadTimeoutMillis());
            retryTimes++;
        }
        if (response.getStatus() >= 500) {
            String requestId = response.getHeaderValue(HeaderKeys.REQUEST_ID);
            String stringContent = response.getContent() == null ? "" : new String(response.getContent());
            ServerException se;
            try {
                se = new Gson().fromJson(stringContent, ServerException.class);
            } catch (JsonParseException e) {
                se = new ServerException("InternalServiceError", "Failed to parse response content", requestId);
            }
            se.setStatusCode(response.getStatus());
            se.setRequestId(requestId);
            throw se;
        } else if (response.getStatus() >= 300) {
            ClientException ce;
            if (response.getContent() == null) {
                ce = new ClientException("SDK.ServerUnreachable", "Failed to get response content from server");
            } else {
                try {
                    ce = new Gson().fromJson(new String(response.getContent()), ClientException.class);
                } catch (JsonParseException e) {
                    ce = new ClientException("SDK.ResponseNotParsable", "Failed to parse response content", e);
                }
            }
            if (ce == null) {
                ce = new ClientException("SDK.UnknownError", "Unknown client error");
            }
            ce.setStatusCode(response.getStatus());
            ce.setRequestId(response.getHeaderValue(HeaderKeys.REQUEST_ID));
            throw ce;
        }
        return response;
    } catch (InvalidKeyException exp) {
        throw new ClientException("SDK.InvalidAccessSecret", "Speicified access secret is not valid.");
    } catch (SocketTimeoutException exp) {
        throw new ClientException("SDK.ServerUnreachable", "SocketTimeoutException has occurred on a socket read or accept.");
    } catch (IOException exp) {
        throw new ClientException("SDK.ServerUnreachable", "Server unreachable: " + exp.toString());
    } catch (NoSuchAlgorithmException exp) {
        throw new ClientException("SDK.InvalidMD5Algorithm", "MD5 hash is not supported by client side.");
    }
}
Also used : PrepareUrl(com.aliyuncs.fc.model.PrepareUrl) ServerException(com.aliyuncs.fc.exceptions.ServerException) SocketTimeoutException(java.net.SocketTimeoutException) HttpResponse(com.aliyuncs.fc.http.HttpResponse) Gson(com.google.gson.Gson) ClientException(com.aliyuncs.fc.exceptions.ClientException) IOException(java.io.IOException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) JsonParseException(com.google.gson.JsonParseException) InvalidKeyException(java.security.InvalidKeyException)

Example 2 with HttpResponse

use of com.aliyuncs.fc.http.HttpResponse in project fc-java-sdk by aliyun.

the class FunctionComputeClient method getService.

public GetServiceResponse getService(GetServiceRequest request) throws ClientException, ServerException {
    HttpResponse response = client.doAction(request, CONTENT_TYPE_APPLICATION_JSON, "GET");
    ServiceMetadata serviceMetadata = GSON.fromJson(new String(response.getContent()), ServiceMetadata.class);
    GetServiceResponse getServiceResponse = new GetServiceResponse();
    getServiceResponse.setServiceMetadata(serviceMetadata);
    getServiceResponse.setHeader(response.getHeaders());
    getServiceResponse.setContent(response.getContent());
    getServiceResponse.setStatus(response.getStatus());
    return getServiceResponse;
}
Also used : HttpResponse(com.aliyuncs.fc.http.HttpResponse) ServiceMetadata(com.aliyuncs.fc.model.ServiceMetadata)

Example 3 with HttpResponse

use of com.aliyuncs.fc.http.HttpResponse in project fc-java-sdk by aliyun.

the class FunctionComputeClient method createService.

public CreateServiceResponse createService(CreateServiceRequest request) throws ClientException, ServerException {
    HttpResponse response = client.doAction(request, CONTENT_TYPE_APPLICATION_JSON, "POST");
    ServiceMetadata serviceMetadata = GSON.fromJson(new String(response.getContent()), ServiceMetadata.class);
    CreateServiceResponse createServiceResponse = new CreateServiceResponse();
    createServiceResponse.setServiceMetadata(serviceMetadata);
    createServiceResponse.setHeaders(response.getHeaders());
    createServiceResponse.setContent(response.getContent());
    createServiceResponse.setStatus(response.getStatus());
    return createServiceResponse;
}
Also used : HttpResponse(com.aliyuncs.fc.http.HttpResponse) ServiceMetadata(com.aliyuncs.fc.model.ServiceMetadata)

Example 4 with HttpResponse

use of com.aliyuncs.fc.http.HttpResponse in project fc-java-sdk by aliyun.

the class FunctionComputeClient method getTrigger.

public GetTriggerResponse getTrigger(GetTriggerRequest request) throws ClientException, ServerException {
    HttpResponse response = client.doAction(request, CONTENT_TYPE_APPLICATION_JSON, "GET");
    TriggerMetadata triggerMetadata = GSON.fromJson(new String(response.getContent()), TriggerMetadata.class);
    GetTriggerResponse getTriggerResponse = new GetTriggerResponse();
    getTriggerResponse.setTriggerMetadata(triggerMetadata);
    getTriggerResponse.setHeader(response.getHeaders());
    getTriggerResponse.setContent(response.getContent());
    getTriggerResponse.setStatus(response.getStatus());
    return getTriggerResponse;
}
Also used : HttpResponse(com.aliyuncs.fc.http.HttpResponse) TriggerMetadata(com.aliyuncs.fc.model.TriggerMetadata)

Example 5 with HttpResponse

use of com.aliyuncs.fc.http.HttpResponse in project fc-java-sdk by aliyun.

the class FunctionComputeClient method deleteTrigger.

public DeleteTriggerResponse deleteTrigger(DeleteTriggerRequest request) throws ClientException, ServerException {
    HttpResponse response = client.doAction(request, CONTENT_TYPE_APPLICATION_JSON, "DELETE");
    DeleteTriggerResponse deleteTriggerResponse = new DeleteTriggerResponse();
    deleteTriggerResponse.setHeader(response.getHeaders());
    deleteTriggerResponse.setStatus(response.getStatus());
    return deleteTriggerResponse;
}
Also used : HttpResponse(com.aliyuncs.fc.http.HttpResponse)

Aggregations

HttpResponse (com.aliyuncs.fc.http.HttpResponse)18 FunctionMetadata (com.aliyuncs.fc.model.FunctionMetadata)3 ServiceMetadata (com.aliyuncs.fc.model.ServiceMetadata)3 TriggerMetadata (com.aliyuncs.fc.model.TriggerMetadata)3 ClientException (com.aliyuncs.fc.exceptions.ClientException)1 ServerException (com.aliyuncs.fc.exceptions.ServerException)1 FunctionCodeMetadata (com.aliyuncs.fc.model.FunctionCodeMetadata)1 PrepareUrl (com.aliyuncs.fc.model.PrepareUrl)1 Gson (com.google.gson.Gson)1 JsonParseException (com.google.gson.JsonParseException)1 IOException (java.io.IOException)1 SocketTimeoutException (java.net.SocketTimeoutException)1 InvalidKeyException (java.security.InvalidKeyException)1 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)1