Refactor rest clients and support timeouts
[ccsdk/cds.git] / ms / blueprintsprocessor / modules / commons / rest-lib / src / main / kotlin / org / onap / ccsdk / cds / blueprintsprocessor / rest / service / BlueprintWebClientService.kt
1 /*
2  * Copyright © 2017-2019 AT&T, Bell Canada, Nordix Foundation
3  * Modifications Copyright © 2018-2019 IBM.
4  * Modifications Copyright © 2019 Huawei.
5  * Modifications Copyright © 2022 Deutsche Telekom AG.
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  */
19
20 package org.onap.ccsdk.cds.blueprintsprocessor.rest.service
21
22 import org.apache.http.message.BasicHeader
23 import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintRetryException
24 import org.onap.ccsdk.cds.controllerblueprints.core.utils.BluePrintIOUtils
25
26 interface BlueprintWebClientService {
27     fun defaultHeaders(): Map<String, String>
28     fun convertToBasicHeaders(
29         mergedDefaultAndSuppliedHeaders: Map<String, String>
30     ): Array<BasicHeader>
31
32     fun exchangeResource(
33         methodType: String,
34         path: String,
35         request: String,
36         headers: Map<String, String>
37     ): WebClientResponse<String>
38
39     fun exchangeResource(
40         methodType: String,
41         path: String,
42         request: String
43     ): WebClientResponse<String>
44
45     suspend fun exchangeNB(methodType: String, path: String, request: Any): WebClientResponse<String>
46
47     suspend fun exchangeNB(methodType: String, path: String, request: Any, additionalHeaders: Map<String, String>?):
48         WebClientResponse<String>
49
50     suspend fun <T> exchangeNB(
51         methodType: String,
52         path: String,
53         request: Any,
54         additionalHeaders: Map<String, String>?,
55         responseType: Class<T>
56     ): WebClientResponse<T>
57
58     /** High performance non blocking Retry function, If execution block [block] throws BluePrintRetryException
59      * exception then this will perform wait and retrigger accoring to times [times] with delay [delay]
60      */
61     suspend fun <T> retry(
62         times: Int = 1,
63         initialDelay: Long = 0,
64         delay: Long = 1000,
65         block: suspend (Int) -> T
66     ): T {
67         val exceptionBlock = { e: Exception ->
68             if (e !is BluePrintRetryException) {
69                 throw e
70             }
71         }
72         return BluePrintIOUtils.retry(times, initialDelay, delay, block, exceptionBlock)
73     }
74
75     // TODO maybe there could be cases where we care about return headers?
76     data class WebClientResponse<T>(val status: Int, val body: T)
77 }