76d6d46465d0a137ac21048112652d37a78fdfec
[ccsdk/cds.git] /
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 import java.nio.file.Path
26
27 interface BlueprintWebClientService {
28     fun defaultHeaders(): Map<String, String>
29     fun convertToBasicHeaders(
30         mergedDefaultAndSuppliedHeaders: Map<String, String>
31     ): Array<BasicHeader>
32
33     fun exchangeResource(
34         methodType: String,
35         path: String,
36         request: String,
37         headers: Map<String, String>
38     ): WebClientResponse<String>
39
40     fun exchangeResource(
41         methodType: String,
42         path: String,
43         request: String
44     ): WebClientResponse<String>
45
46     fun uploadBinaryFile(
47         path: String,
48         filePath: Path
49     ): WebClientResponse<String>
50
51     suspend fun exchangeNB(methodType: String, path: String, request: Any): WebClientResponse<String>
52
53     suspend fun exchangeNB(methodType: String, path: String, request: Any, additionalHeaders: Map<String, String>?):
54         WebClientResponse<String>
55
56     suspend fun <T> exchangeNB(
57         methodType: String,
58         path: String,
59         request: Any,
60         additionalHeaders: Map<String, String>?,
61         responseType: Class<T>
62     ): WebClientResponse<T>
63
64     /** High performance non blocking Retry function, If execution block [block] throws BluePrintRetryException
65      * exception then this will perform wait and retrigger accoring to times [times] with delay [delay]
66      */
67     suspend fun <T> retry(
68         times: Int = 1,
69         initialDelay: Long = 0,
70         delay: Long = 1000,
71         block: suspend (Int) -> T
72     ): T {
73         val exceptionBlock = { e: Exception ->
74             if (e !is BluePrintRetryException) {
75                 throw e
76             }
77         }
78         return BluePrintIOUtils.retry(times, initialDelay, delay, block, exceptionBlock)
79     }
80
81     // TODO maybe there could be cases where we care about return headers?
82     data class WebClientResponse<T>(val status: Int, val body: T)
83 }