Skip to content

Commit 889141d

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

File tree

3 files changed

+198
-0
lines changed

3 files changed

+198
-0
lines changed
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
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+
16+
import org.apache.commons.lang3.Validate;
17+
18+
import com.netflix.conductor.client.http.ConductorClientRequest.Method;
19+
20+
import com.fasterxml.jackson.core.type.TypeReference;
21+
22+
/**
23+
* Client for incoming webhook operations in Conductor
24+
*/
25+
public final class IncomingWebhookClient {
26+
27+
private ConductorClient client;
28+
29+
/** Creates a default incoming webhook client */
30+
public IncomingWebhookClient() {
31+
}
32+
33+
public IncomingWebhookClient(ConductorClient client) {
34+
this.client = client;
35+
}
36+
37+
/**
38+
* Kept only for backwards compatibility
39+
*
40+
* @param rootUri basePath for the ApiClient
41+
*/
42+
@Deprecated
43+
public void setRootURI(String rootUri) {
44+
if (client != null) {
45+
client.shutdown();
46+
}
47+
client = new ConductorClient(rootUri);
48+
}
49+
50+
/**
51+
* Handle incoming webhook with POST method
52+
*
53+
* @param id The webhook ID
54+
* @param payload The webhook payload as string
55+
* @return The response from the webhook handler
56+
*/
57+
public String handleWebhook(String id, String payload) {
58+
Validate.notBlank(id, "Webhook ID cannot be blank");
59+
Validate.notNull(payload, "Payload cannot be null");
60+
61+
ConductorClientRequest.Builder requestBuilder = ConductorClientRequest.builder()
62+
.method(Method.POST)
63+
.path("/webhook/{id}")
64+
.addPathParam("id", id)
65+
.body(payload);
66+
67+
ConductorClientRequest request = requestBuilder.build();
68+
69+
ConductorClientResponse<String> resp = client.execute(request, new TypeReference<>() {
70+
});
71+
72+
return resp.getData();
73+
}
74+
75+
/**
76+
* Handle incoming webhook with GET method (typically for ping/health checks)
77+
*
78+
* @param id The webhook ID
79+
* @return The response from the webhook handler
80+
*/
81+
public String handlePing(String id) {
82+
Validate.notBlank(id, "Webhook ID cannot be blank");
83+
84+
ConductorClientRequest.Builder requestBuilder = ConductorClientRequest.builder()
85+
.method(Method.GET)
86+
.path("/webhook/{id}")
87+
.addPathParam("id", id);
88+
89+
ConductorClientRequest request = requestBuilder.build();
90+
91+
ConductorClientResponse<String> resp = client.execute(request, new TypeReference<>() {
92+
});
93+
94+
return resp.getData();
95+
}
96+
}
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)