Skip to content

Commit 164ae03

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

File tree

4 files changed

+316
-0
lines changed

4 files changed

+316
-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 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 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: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
/*
2+
* Copyright 2022 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 io.orkes.conductor.client.http;
14+
15+
import java.util.List;
16+
import java.util.Map;
17+
18+
import org.apache.commons.lang3.StringUtils;
19+
20+
import io.orkes.conductor.client.OrkesClientException;
21+
22+
public class ApiException extends OrkesClientException {
23+
private int code;
24+
private Map<String, List<String>> responseHeaders;
25+
private String responseBody;
26+
27+
public ApiException() {
28+
super("API Exception");
29+
this.code = 0;
30+
this.responseHeaders = null;
31+
this.responseBody = null;
32+
}
33+
34+
public ApiException(Throwable throwable) {
35+
super(throwable.getMessage(), throwable);
36+
this.code = 0;
37+
this.responseHeaders = null;
38+
this.responseBody = null;
39+
}
40+
41+
public ApiException(String message) {
42+
super(message);
43+
this.code = 0;
44+
this.responseHeaders = null;
45+
this.responseBody = null;
46+
}
47+
48+
public ApiException(String message, Throwable throwable, int code, Map<String, List<String>> responseHeaders, String responseBody) {
49+
super(message, throwable);
50+
this.code = 0;
51+
this.responseHeaders = null;
52+
this.responseBody = null;
53+
super.setCode(String.valueOf(code));
54+
super.setStatus(code);
55+
this.code = code;
56+
this.responseHeaders = responseHeaders;
57+
this.responseBody = responseBody;
58+
}
59+
60+
public ApiException(String message, int code, Map<String, List<String>> responseHeaders, String responseBody) {
61+
this(message, null, code, responseHeaders, responseBody);
62+
super.setCode(String.valueOf(code));
63+
super.setStatus(code);
64+
}
65+
66+
public ApiException(String message, Throwable throwable, int code, Map<String, List<String>> responseHeaders) {
67+
this(message, throwable, code, responseHeaders, null);
68+
super.setCode(String.valueOf(code));
69+
super.setStatus(code);
70+
}
71+
72+
public ApiException(int code, Map<String, List<String>> responseHeaders, String responseBody) {
73+
this(null, null, code, responseHeaders, responseBody);
74+
super.setCode(String.valueOf(code));
75+
super.setStatus(code);
76+
}
77+
78+
public ApiException(int code, String message) {
79+
super(message);
80+
this.code = 0;
81+
this.responseHeaders = null;
82+
this.responseBody = null;
83+
this.code = code;
84+
super.setCode(String.valueOf(code));
85+
super.setStatus(code);
86+
}
87+
88+
public ApiException(int code, String message, Map<String, List<String>> responseHeaders, String responseBody) {
89+
this(code, message);
90+
this.responseHeaders = responseHeaders;
91+
this.responseBody = responseBody;
92+
super.setCode(String.valueOf(code));
93+
super.setStatus(code);
94+
}
95+
96+
public boolean isClientError() {
97+
return this.code > 399 && this.code < 499;
98+
}
99+
100+
public int getStatusCode() {
101+
return this.code;
102+
}
103+
104+
public Map<String, List<String>> getResponseHeaders() {
105+
return this.responseHeaders;
106+
}
107+
108+
public String getResponseBody() {
109+
return this.responseBody;
110+
}
111+
112+
@Override
113+
public String getMessage() {
114+
int statusCode = this.getStatusCode();
115+
return statusCode + ":" + (StringUtils.isBlank(this.responseBody) ? super.getMessage() : this.responseBody);
116+
}
117+
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/*
2+
* Copyright 2022 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 io.orkes.conductor.client.http;
14+
15+
import java.util.List;
16+
import java.util.Map;
17+
18+
import org.apache.commons.lang3.StringUtils;
19+
20+
import io.orkes.conductor.client.OrkesClientException;
21+
22+
public class ConflictException extends OrkesClientException {
23+
private int code;
24+
private final Map<String, List<String>> responseHeaders;
25+
private String responseBody;
26+
private String message;
27+
28+
public ConflictException(String message, Throwable throwable, int code, Map<String, List<String>> responseHeaders, String responseBody) {
29+
super(message, throwable);
30+
super.setCode(String.valueOf(code));
31+
super.setStatus(code);
32+
this.code = code;
33+
this.responseHeaders = responseHeaders;
34+
this.responseBody = responseBody;
35+
this.message = message;
36+
}
37+
38+
public ConflictException(String message, int code, Map<String, List<String>> responseHeaders, String responseBody) {
39+
this(message, null, code, responseHeaders, responseBody);
40+
super.setCode(String.valueOf(code));
41+
super.setStatus(code);
42+
this.code = code;
43+
this.message = message;
44+
this.responseBody = responseBody;
45+
}
46+
47+
public int getStatusCode() {
48+
return this.code;
49+
}
50+
51+
public Map<String, List<String>> getResponseHeaders() {
52+
return this.responseHeaders;
53+
}
54+
55+
@Override
56+
public String getMessage() {
57+
int statusCode = this.getStatusCode();
58+
return statusCode + ":" + (StringUtils.isBlank(this.responseBody) ? super.getMessage() : this.responseBody);
59+
}
60+
61+
@Override
62+
public String toString() {
63+
return this.responseBody;
64+
}
65+
66+
public boolean isClientError() {
67+
return this.code > 399 && this.code < 499;
68+
}
69+
}

0 commit comments

Comments
 (0)