9a40dc57b9b8196ef904cf4236c5aab86ea3ddf5
[dcaegen2/services/sdk.git] /
1 /*
2  * ============LICENSE_START=======================================================
3  * DCAEGEN2-SERVICES-SDK
4  * ================================================================================
5  * Copyright (C) 2018-2019 NOKIA Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20 package org.onap.dcaegen2.services.sdk.rest.services.aai.client.service.http;
21
22 import io.vavr.collection.HashMap;
23 import java.util.Map;
24 import org.onap.dcaegen2.services.sdk.rest.services.adapters.http.HttpMethod;
25 import org.onap.dcaegen2.services.sdk.rest.services.adapters.http.HttpRequest;
26 import org.onap.dcaegen2.services.sdk.rest.services.adapters.http.ImmutableHttpRequest;
27 import org.onap.dcaegen2.services.sdk.rest.services.adapters.http.RequestBody;
28 import org.onap.dcaegen2.services.sdk.rest.services.model.ClientModel;
29 import org.onap.dcaegen2.services.sdk.rest.services.model.JsonBodyBuilder;
30 import org.onap.dcaegen2.services.sdk.rest.services.model.logging.RequestDiagnosticContext;
31
32 public final class AaiRequests {
33
34     private AaiRequests(){}
35
36     public static HttpRequest createAaiPatchRequest(String url,
37             RequestDiagnosticContext context,
38             Map<String, String> customHeaders,
39             JsonBodyBuilder jsonBodyBuilder,
40             ClientModel clientModel) {
41
42         return buildAaiRequestWithBody(url, context, customHeaders,
43                 jsonBodyBuilder, clientModel, HttpMethod.PATCH);
44     }
45
46     public static HttpRequest createAaiPutRequest(String url,
47             RequestDiagnosticContext context,
48             Map<String, String> customHeaders,
49             JsonBodyBuilder jsonBodyBuilder,
50             ClientModel clientModel) {
51
52         return buildAaiRequestWithBody(url, context, customHeaders,
53                 jsonBodyBuilder, clientModel, HttpMethod.PUT);
54     }
55
56     private static HttpRequest buildAaiRequestWithBody(String url,
57             RequestDiagnosticContext context,
58             Map<String, String> customHeaders,
59             JsonBodyBuilder jsonBodyBuilder,
60             ClientModel clientModel,
61             HttpMethod method) {
62
63         String jsonBody = jsonBodyBuilder.createJsonBody(clientModel);
64
65         return ImmutableHttpRequest.builder()
66                 .url(url)
67                 .customHeaders(HashMap.ofAll(customHeaders))
68                 .diagnosticContext(context)
69                 .body(RequestBody.fromString(jsonBody))
70                 .method(method)
71                 .build();
72     }
73
74     public static HttpRequest createAaiGetRequest(String url,
75             RequestDiagnosticContext context,
76             Map<String, String> customHeaders) {
77         return ImmutableHttpRequest.builder()
78                 .method(HttpMethod.GET)
79                 .url(url)
80                 .customHeaders(HashMap.ofAll(customHeaders))
81                 .diagnosticContext(context)
82                 .build();
83     }
84 }