res = req.asString();
+
+ return objectMapper.readValue(res.getBody(), Response.class);
+ } catch (UnirestException e) {
+ throw new IOException(e);
+ }
+ }
+}
diff --git a/src/main/java/jenkins/plugins/rocketchatnotifier/rocket/RocketChatClientImpl.java b/src/main/java/jenkins/plugins/rocketchatnotifier/rocket/RocketChatClientImpl.java
new file mode 100644
index 00000000..24773254
--- /dev/null
+++ b/src/main/java/jenkins/plugins/rocketchatnotifier/rocket/RocketChatClientImpl.java
@@ -0,0 +1,79 @@
+package jenkins.plugins.rocketchatnotifier.rocket;
+
+import jenkins.plugins.rocketchatnotifier.model.Response;
+import jenkins.plugins.rocketchatnotifier.model.Room;
+import jenkins.plugins.rocketchatnotifier.model.User;
+import sun.security.validator.ValidatorException;
+
+import java.io.IOException;
+
+/**
+ * Client for Rocket.Chat which relies on the REST API v1.
+ *
+ * Please note, this client does not cache any of the results.
+ *
+ * @version 0.1.0
+ * @since 0.0.1
+ */
+public class RocketChatClientImpl implements RocketChatClient {
+ private RocketChatClientCallBuilder callBuilder;
+
+ /**
+ * Initialize a new instance of the client providing the server's url along with username and
+ * password to use.
+ *
+ * @param serverUrl of the Rocket.Chat server, with or without it ending in "/api/"
+ * @param user which to authenticate with
+ * @param password of the user to authenticate with
+ */
+ public RocketChatClientImpl(String serverUrl, String user, String password) {
+ this.callBuilder = new RocketChatClientCallBuilder(serverUrl, user, password);
+ }
+
+
+ @Override
+ public User[] getUsers() throws IOException {
+ Response res = this.callBuilder.buildCall(RocketChatRestApiV1.UsersList);
+
+ if (!res.isSuccessful()) {
+ throw new IOException("The call to get the User's Information was unsuccessful.");
+ }
+ if (!res.isUsers()) {
+ throw new IOException("Get User Information failed to retrieve a user.");
+ }
+ return res.getUsers();
+ }
+
+ @Override
+ public User getUser(String userId) throws IOException {
+ Response res = this.callBuilder.buildCall(RocketChatRestApiV1.UsersInfo, new RocketChatQueryParams("userId", userId));
+
+ if (!res.isSuccessful())
+ throw new IOException("The call to get the User's Information was unsuccessful.");
+
+ if (!res.isUser())
+ throw new IOException("Get User Information failed to retrieve a user.");
+
+ return res.getUser();
+ }
+
+ @Override
+ public Room[] getChannels() throws IOException {
+ Response res = this.callBuilder.buildCall(RocketChatRestApiV1.ChannelsList);
+
+ if (!res.isSuccessful()) {
+ throw new IOException("The call to get the all Channel Information was unsuccessful.");
+ }
+ return res.getChannels();
+ }
+
+ @Override
+ public void send(Room room, String message) throws ValidatorException, IOException {
+
+ }
+
+ @Override
+ public void send(String room, String message) throws ValidatorException, IOException {
+
+ }
+}
diff --git a/src/main/java/jenkins/plugins/rocketchatnotifier/rocket/RocketChatQueryParams.java b/src/main/java/jenkins/plugins/rocketchatnotifier/rocket/RocketChatQueryParams.java
new file mode 100644
index 00000000..69fe15f9
--- /dev/null
+++ b/src/main/java/jenkins/plugins/rocketchatnotifier/rocket/RocketChatQueryParams.java
@@ -0,0 +1,73 @@
+package jenkins.plugins.rocketchatnotifier.rocket;
+
+
+import java.util.HashMap;
+import java.util.Map;
+
+/**
+ * Contains the keys and values for the query string.
+ *
+ * @author Bradley Hilton (graywolf336)
+ * @since 0.1.0
+ * @version 0.0.1
+ */
+public class RocketChatQueryParams {
+ private HashMap queryParams;
+
+ /** Initializes an empty query parameters object. */
+ public RocketChatQueryParams() {
+ this.queryParams = new HashMap();
+ }
+
+ /**
+ * Initializes query parameter object with one parameter already added.
+ *
+ * @param param the query name
+ * @param value the value of the parameter
+ */
+ public RocketChatQueryParams(String param, String value) {
+ this.queryParams = new HashMap();
+ this.queryParams.put(param, value);
+ }
+
+ /**
+ * Check to see if there are any query parameters.
+ *
+ * @return whether the query parameters is empty
+ */
+ public boolean isEmpty() {
+ return this.queryParams.isEmpty();
+ }
+
+ /**
+ * Gets all of the query parameters.
+ *
+ * @return the query parameters
+ */
+ public Map extends String, ? extends String> get() {
+ return this.queryParams;
+ }
+
+ /**
+ * Adds a query parameter to the list.
+ *
+ * @param param the query name
+ * @param value the value of the parameter
+ * @return the instance of the {@link RocketChatQueryParams}
+ */
+ public RocketChatQueryParams add(String param, String value) {
+ this.queryParams.put(param, value);
+ return this;
+ }
+
+ /**
+ * Adds all of the provided query parameters to the list.
+ *
+ * @param params the query parameters
+ * @return the instance of the {@link RocketChatQueryParams}
+ */
+ public RocketChatQueryParams addAll(Map extends String, ? extends String> params) {
+ this.queryParams.putAll(params);
+ return this;
+ }
+}
diff --git a/src/main/java/jenkins/plugins/rocketchatnotifier/rocket/RocketChatRestApiV1.java b/src/main/java/jenkins/plugins/rocketchatnotifier/rocket/RocketChatRestApiV1.java
new file mode 100644
index 00000000..0047426d
--- /dev/null
+++ b/src/main/java/jenkins/plugins/rocketchatnotifier/rocket/RocketChatRestApiV1.java
@@ -0,0 +1,65 @@
+package jenkins.plugins.rocketchatnotifier.rocket;
+
+
+/**
+ * Enumeration of the available REST API methods.
+ *
+ * @author Bradley Hilton (graywolf336)
+ * @version 0.0.1
+ * @since 0.1.0
+ */
+public enum RocketChatRestApiV1 {
+ /**
+ * Retrieves a list of all the users in the server.
+ */
+ UsersList("users.list", HttpMethods.GET, true),
+ /**
+ * Retrieves the user information from the server.
+ */
+ UsersInfo("users.info", HttpMethods.GET, true),
+ /**
+ * Retrieves a list of all the channels in the server.
+ */
+ ChannelsList("channels.list", HttpMethods.GET, true),
+ /**
+ * Retrieves a list of all the channels in the server.
+ */
+ ChannelsInfo("channels.info", HttpMethods.GET, true);
+
+ private String methodName;
+ private HttpMethods httpMethod;
+ private boolean requiresAuth;
+
+ private RocketChatRestApiV1(String methodName, HttpMethods httpMethod, boolean requiresAuth) {
+ this.methodName = methodName;
+ this.httpMethod = httpMethod;
+ this.requiresAuth = requiresAuth;
+ }
+
+ /**
+ * Gets the method name to be called to the server.
+ *
+ * @return the method name plus "v1/" at the start
+ */
+ public String getMethodName() {
+ return "v1/" + this.methodName;
+ }
+
+ /**
+ * Gets the {@link HttpMethods http method} which should be used.
+ *
+ * @return {@link HttpMethods http method} to be used
+ */
+ public HttpMethods getHttpMethod() {
+ return this.httpMethod;
+ }
+
+ /**
+ * Check whether the method requires authentication or not.
+ *
+ * @return whether this requires authentication or not
+ */
+ public boolean requiresAuth() {
+ return this.requiresAuth;
+ }
+}
diff --git a/src/test/java/jenkins/plugins/rocketchatnotifier/rocket/RocketChatClientIT.java b/src/test/java/jenkins/plugins/rocketchatnotifier/rocket/RocketChatClientIT.java
index 1b6080d5..7cdec0a0 100644
--- a/src/test/java/jenkins/plugins/rocketchatnotifier/rocket/RocketChatClientIT.java
+++ b/src/test/java/jenkins/plugins/rocketchatnotifier/rocket/RocketChatClientIT.java
@@ -1,36 +1,27 @@
package jenkins.plugins.rocketchatnotifier.rocket;
import jenkins.plugins.rocketchatnotifier.model.Room;
-import org.junit.After;
import org.junit.Before;
import org.junit.Test;
-import java.util.Set;
-
import static org.hamcrest.MatcherAssert.assertThat;
-import static org.hamcrest.Matchers.hasSize;
+import static org.hamcrest.Matchers.is;
/**
* Created by mreinhardt on 26.12.16.
*/
- public class RocketChatClientIT {
+public class RocketChatClientIT {
private RocketChatClient client;
@Before
public void setup() throws Exception {
- this.client = new RocketChatClient("http://localhost:4443/api/v1/", "admin", "supersecret"); // TODO read from env
- this.client.login();
- }
-
- @After
- public void cleanUp() throws Exception {
- this.client.logout();
+ this.client = new RocketChatClientImpl("http://localhost:4443/api/", "admin", "supersecret"); // TODO read from env
}
@Test
public void shouldReadRooms() throws Exception {
- Set rooms = this.client.getPublicRooms();
- assertThat(rooms, hasSize(0));
+ Room[] rooms = this.client.getChannels();
+ assertThat(rooms.length, is(1));
}
}