• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License").
5  * You may not use this file except in compliance with the License.
6  * A copy of the License is located at
7  *
8  *  http://aws.amazon.com/apache2.0
9  *
10  * or in the "license" file accompanying this file. This file is distributed
11  * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12  * express or implied. See the License for the specific language governing
13  * permissions and limitations under the License.
14  */
15 
16 package software.amazon.awssdk.services.machinelearning.internal;
17 
18 import java.net.URI;
19 import java.net.URISyntaxException;
20 import software.amazon.awssdk.annotations.SdkInternalApi;
21 import software.amazon.awssdk.core.exception.SdkClientException;
22 import software.amazon.awssdk.core.interceptor.Context;
23 import software.amazon.awssdk.core.interceptor.ExecutionAttributes;
24 import software.amazon.awssdk.core.interceptor.ExecutionInterceptor;
25 import software.amazon.awssdk.http.SdkHttpRequest;
26 import software.amazon.awssdk.services.machinelearning.model.PredictRequest;
27 
28 /**
29  * Predict calls are sent to a predictor-specific endpoint. This handler
30  * extracts the PredictRequest's PredictEndpoint "parameter" and swaps it in as
31  * the endpoint to send the request to.
32  */
33 @SdkInternalApi
34 public final class PredictEndpointInterceptor implements ExecutionInterceptor {
35 
36     @Override
modifyHttpRequest(Context.ModifyHttpRequest context, ExecutionAttributes executionAttributes)37     public SdkHttpRequest modifyHttpRequest(Context.ModifyHttpRequest context, ExecutionAttributes executionAttributes) {
38         SdkHttpRequest request = context.httpRequest();
39         Object originalRequest = context.request();
40         if (originalRequest instanceof PredictRequest) {
41             PredictRequest pr = (PredictRequest) originalRequest;
42             if (pr.predictEndpoint() == null) {
43                 throw SdkClientException.builder().message("PredictRequest.PredictEndpoint is required!").build();
44             }
45 
46             try {
47                 URI endpoint = new URI(pr.predictEndpoint());
48                 return request.toBuilder().uri(endpoint).build();
49             } catch (URISyntaxException e) {
50                 throw SdkClientException.builder()
51                                         .message("Unable to parse PredictRequest.PredictEndpoint")
52                                         .cause(e)
53                                         .build();
54             }
55         }
56         return request;
57     }
58 
59 }
60