diff --git a/src/main/java/org/rundeck/api/ApiCall.java b/src/main/java/org/rundeck/api/ApiCall.java index c0a7625..51d6143 100644 --- a/src/main/java/org/rundeck/api/ApiCall.java +++ b/src/main/java/org/rundeck/api/ApiCall.java @@ -219,6 +219,30 @@ class ApiCall { return response; } + + /** + * Execute an HTTP POST or GET request to the RunDeck instance, on the given path, depend ing of the {@link + * ApiPathBuilder} contains POST content or not (attachments or Form data). We will login first, and then execute + * the API call. At the end, the given parser will be used to convert the response to a more useful result object. + * + * @param apiPath on which we will make the HTTP request - see {@link ApiPathBuilder} + * @param parser used to parse the response + * + * @return the result of the call, as formatted by the parser + * + * @throws RundeckApiException in case of error when calling the API + * @throws RundeckApiLoginException if the login fails (in case of login-based authentication) + * @throws RundeckApiTokenException if the token is invalid (in case of token-based authentication) + */ + public T postOrGet(ApiPathBuilder apiPath, XmlNodeParser parser) throws RundeckApiException, + RundeckApiLoginException, + RundeckApiTokenException { + if (apiPath.hasPostContent()) { + return post(apiPath, parser); + } else { + return get(apiPath, parser); + } + } /** * Execute an HTTP POST request to the RunDeck instance, on the given path. We will login first, and then execute diff --git a/src/main/java/org/rundeck/api/ApiPathBuilder.java b/src/main/java/org/rundeck/api/ApiPathBuilder.java index baa4c88..3e78c93 100644 --- a/src/main/java/org/rundeck/api/ApiPathBuilder.java +++ b/src/main/java/org/rundeck/api/ApiPathBuilder.java @@ -284,4 +284,11 @@ class ApiPathBuilder { public List getForm() { return form; } + + /** + * Return true if there are any Attachments or Form data for a POST request. + */ + public boolean hasPostContent() { + return getAttachments().size() > 0 || getForm().size() > 0; + } } diff --git a/src/main/java/org/rundeck/api/RundeckClient.java b/src/main/java/org/rundeck/api/RundeckClient.java index 92557f2..331cb6f 100644 --- a/src/main/java/org/rundeck/api/RundeckClient.java +++ b/src/main/java/org/rundeck/api/RundeckClient.java @@ -2284,8 +2284,7 @@ public class RundeckClient implements Serializable { .param("max", max) .param("offset", offset); - - return new ApiCall(this).post(builder, new HistoryParser("result/events")); + return new ApiCall(this).postOrGet(builder, new HistoryParser("result/events")); } /* diff --git a/src/test/java/org/rundeck/api/RundeckClientTest.java b/src/test/java/org/rundeck/api/RundeckClientTest.java index 2d87304..e976bf6 100644 --- a/src/test/java/org/rundeck/api/RundeckClientTest.java +++ b/src/test/java/org/rundeck/api/RundeckClientTest.java @@ -101,6 +101,24 @@ public class RundeckClientTest { Assert.assertEquals(Arrays.asList("fliff", "malk/blah3"), names); } + @Test + @Betamax(tape = "get_history_user", + match = {MatchRule.uri, MatchRule.method, MatchRule.path, MatchRule.query }) + public void getHistoryUser() throws Exception { + final RundeckHistory test = client.getHistory("demo", "bob", (String)null, (List)null, null, null, null, null, null); + Assert.assertEquals(1, test.getCount()); + Assert.assertEquals(20, test.getMax()); + Assert.assertEquals(0, test.getOffset()); + Assert.assertEquals(1, test.getTotal()); + final List events = test.getEvents(); + Assert.assertEquals(1, events.size()); + final List names = new ArrayList(); + for (final RundeckEvent event : events) { + names.add(event.getUser()); + } + Assert.assertEquals(Arrays.asList("bob"), names); + } + @Before public void setUp() throws Exception { // not that you can put whatever here, because we don't actually connect to the RunDeck instance diff --git a/src/test/resources/betamax/tapes/get_history_user.yaml b/src/test/resources/betamax/tapes/get_history_user.yaml new file mode 100644 index 0000000..d604d3f --- /dev/null +++ b/src/test/resources/betamax/tapes/get_history_user.yaml @@ -0,0 +1,22 @@ +!tape +name: get_history_user +interactions: +- recorded: 2012-08-24T21:37:50.531Z + request: + method: GET + uri: http://rundeck.local:4440/api/5/history?project=demo&userFilter=bob + headers: + Content-Length: '52' + Content-Type: application/x-www-form-urlencoded; charset=UTF-8 + Host: rundeck.localtest:8080 + Proxy-Connection: Keep-Alive + User-Agent: RunDeck API Java Client 5 + X-RunDeck-Auth-Token: 960412PR40dRREU5d87S2Ce2OeddD5c1 + response: + status: 200 + headers: + Content-Type: text/xml; charset=utf-8 + Expires: Thu, 01 Jan 1970 00:00:00 GMT + Server: Jetty(6.1.21) + Set-Cookie: JSESSIONID=118zw5docyvss;Path=/rundeck + body: malk/blah2succeededecho aljdsfbobdemo2012-08-23T23:21:31Z2012-08-23T23:21:31Z