Skip to content

Commit 06b5a77

Browse files
author
Shailesh Jagannath Padave
committed
Added API n ConflictException
1 parent 41e0093 commit 06b5a77

File tree

3 files changed

+199
-0
lines changed

3 files changed

+199
-0
lines changed
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
/*
2+
* Copyright 2020 Conductor Authors.
3+
* <p>
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
5+
* the License. You may obtain a copy of the License at
6+
* <p>
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
* <p>
9+
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
10+
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
11+
* specific language governing permissions and limitations under the License.
12+
*/
13+
package com.netflix.conductor.client.http;
14+
15+
import java.util.Map;
16+
17+
import org.apache.commons.lang3.Validate;
18+
19+
import com.netflix.conductor.client.http.ConductorClientRequest.Method;
20+
21+
import com.fasterxml.jackson.core.type.TypeReference;
22+
23+
/**
24+
* Client for incoming webhook operations in Conductor
25+
*/
26+
public final class IncomingWebhookClient {
27+
28+
private ConductorClient client;
29+
30+
/** Creates a default incoming webhook client */
31+
public IncomingWebhookClient() {
32+
}
33+
34+
public IncomingWebhookClient(ConductorClient client) {
35+
this.client = client;
36+
}
37+
38+
/**
39+
* Kept only for backwards compatibility
40+
*
41+
* @param rootUri basePath for the ApiClient
42+
*/
43+
@Deprecated
44+
public void setRootURI(String rootUri) {
45+
if (client != null) {
46+
client.shutdown();
47+
}
48+
client = new ConductorClient(rootUri);
49+
}
50+
51+
/**
52+
* Handle incoming webhook with POST method
53+
*
54+
* @param id The webhook ID
55+
* @param payload The webhook payload as string
56+
* @return The response from the webhook handler
57+
*/
58+
public String handleWebhook(String id, String payload) {
59+
Validate.notBlank(id, "Webhook ID cannot be blank");
60+
Validate.notNull(payload, "Payload cannot be null");
61+
62+
ConductorClientRequest.Builder requestBuilder = ConductorClientRequest.builder()
63+
.method(Method.POST)
64+
.path("/webhook/{id}")
65+
.addPathParam("id", id)
66+
.body(payload);
67+
68+
ConductorClientRequest request = requestBuilder.build();
69+
70+
ConductorClientResponse<String> resp = client.execute(request, new TypeReference<>() {
71+
});
72+
73+
return resp.getData();
74+
}
75+
76+
/**
77+
* Handle incoming webhook with GET method (typically for ping/health checks)
78+
*
79+
* @param id The webhook ID
80+
* @return The response from the webhook handler
81+
*/
82+
public String handlePing(String id) {
83+
Validate.notBlank(id, "Webhook ID cannot be blank");
84+
85+
ConductorClientRequest.Builder requestBuilder = ConductorClientRequest.builder()
86+
.method(Method.GET)
87+
.path("/webhook/{id}")
88+
.addPathParam("id", id);
89+
90+
ConductorClientRequest request = requestBuilder.build();
91+
92+
ConductorClientResponse<String> resp = client.execute(request, new TypeReference<>() {
93+
});
94+
95+
return resp.getData();
96+
}
97+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
* Copyright 2022 Orkes, Inc.
3+
* <p>
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
5+
* the License. You may obtain a copy of the License at
6+
* <p>
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
* <p>
9+
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
10+
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
11+
* specific language governing permissions and limitations under the License.
12+
*/
13+
package io.orkes.conductor.client;
14+
15+
import com.netflix.conductor.client.exception.ConductorClientException;
16+
17+
public class OrkesClientException extends ConductorClientException {
18+
19+
public OrkesClientException() {
20+
super("Orkes client error");
21+
}
22+
23+
public OrkesClientException(String message) {
24+
super(message);
25+
}
26+
27+
public OrkesClientException(String message, Throwable cause) {
28+
super(message, cause);
29+
}
30+
31+
public OrkesClientException(int status, String message) {
32+
super(status, message);
33+
}
34+
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/*
2+
* Copyright 2022 Orkes, Inc.
3+
* <p>
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
5+
* the License. You may obtain a copy of the License at
6+
* <p>
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
* <p>
9+
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
10+
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
11+
* specific language governing permissions and limitations under the License.
12+
*/
13+
package io.orkes.conductor.client.http;
14+
15+
import io.orkes.conductor.client.OrkesClientException;
16+
import org.apache.commons.lang3.StringUtils;
17+
18+
import java.util.List;
19+
import java.util.Map;
20+
21+
public class ConflictException extends OrkesClientException {
22+
private int code;
23+
private final Map<String, List<String>> responseHeaders;
24+
private String responseBody;
25+
private String message;
26+
27+
public ConflictException(String message, Throwable throwable, int code, Map<String, List<String>> responseHeaders, String responseBody) {
28+
super(message, throwable);
29+
super.setCode(String.valueOf(code));
30+
super.setStatus(code);
31+
this.code = code;
32+
this.responseHeaders = responseHeaders;
33+
this.responseBody = responseBody;
34+
this.message = message;
35+
}
36+
37+
public ConflictException(String message, int code, Map<String, List<String>> responseHeaders, String responseBody) {
38+
this(message, null, code, responseHeaders, responseBody);
39+
super.setCode(String.valueOf(code));
40+
super.setStatus(code);
41+
this.code = code;
42+
this.message = message;
43+
this.responseBody = responseBody;
44+
}
45+
46+
public int getStatusCode() {
47+
return this.code;
48+
}
49+
50+
public Map<String, List<String>> getResponseHeaders() {
51+
return this.responseHeaders;
52+
}
53+
54+
@Override
55+
public String getMessage() {
56+
int statusCode = this.getStatusCode();
57+
return statusCode + ":" + (StringUtils.isBlank(this.responseBody) ? super.getMessage() : this.responseBody);
58+
}
59+
60+
@Override
61+
public String toString() {
62+
return this.responseBody;
63+
}
64+
65+
public boolean isClientError() {
66+
return this.code > 399 && this.code < 499;
67+
}
68+
}

0 commit comments

Comments
 (0)