Skip to content

A new API operation and alternative way of configuration #7

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,13 @@
<version>4.4.1</version>
</dependency>

<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-configuration2 -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-configuration2</artifactId>
<version>2.10.1</version>
</dependency>

<!-- https://mvnrepository.com/artifact/javax.activation/activation -->
<dependency>
<groupId>javax.activation</groupId>
Expand Down
35 changes: 34 additions & 1 deletion src/main/java/io/taliox/zulip/ZulipRestExecutor.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,14 @@

import io.taliox.zulip.calls.ZulipRestAPICall;
import io.taliox.zulip.controller.HttpController;
import org.apache.commons.configuration2.INIConfiguration;
import org.apache.commons.configuration2.SubnodeConfiguration;
import org.apache.commons.configuration2.ex.ConfigurationException;

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;

/**
* The Class ZulipRestExecutor which is responsible for executing requests and
Expand All @@ -27,6 +35,31 @@ public ZulipRestExecutor(String userName, String password, String serverURL) {
this.httpController = new HttpController(userName, password, serverURL);
}

/**
* A constructor for the Zulip REST API executor taking the file name of a zuliprc file. The information from the
* file is used to configure the executor.
* <br/>
* An example that will be accepted:
* [api]
* [email protected]
* key=API_KEY
* site=https://your.zulip.com
* <br/>
* The Executor is responsible for communicating with your Zulip server.
* @param zuliprcFileName
* Name of the zuliprc file which can be generated by a Zulip client.
*/
public ZulipRestExecutor(String zuliprcFileName) throws IOException, ConfigurationException {
INIConfiguration iniConfiguration = new INIConfiguration();
iniConfiguration.read(new FileReader(zuliprcFileName));
SubnodeConfiguration apiSection = iniConfiguration.getSection("api");
this.httpController = new HttpController(
apiSection.getString("email"),
apiSection.getString("key"),
apiSection.getString("site")
);
}

/**
* Executes a HTTP call to a Zulip server.
*
Expand All @@ -38,4 +71,4 @@ public String executeCall(ZulipRestAPICall call) {
return call.execute(this);
}

}
}
50 changes: 50 additions & 0 deletions src/main/java/io/taliox/zulip/calls/users/PostUserStatus.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package io.taliox.zulip.calls.users;

import io.taliox.zulip.ZulipRestExecutor;
import io.taliox.zulip.calls.ZulipRestAPICall;
import org.apache.http.client.methods.HttpPost;

/**
* Updates the status for the currently authenticated user. See the API description for details on updating the status.
*
* @see <a href=
* "https://zulip.com/api/update-status">https://zulip.com/api/update-status</a>
*/
public class PostUserStatus extends ZulipRestAPICall {

/**
* The content of the status message.
* @see <a href=
* "https://zulip.com/api/update-status#parameter-status_text">API description</a>
*/
private final String statusText;
/**
* The name of the status emoji.
* @see <a href=
* "https://zulip.com/api/update-status#parameter-emoji_name">API description</a>
*/
private final String emojiName;

/**
* Instantiate a new {@link PostUserStatus}.
*
* @param statusText
* The content of the status message. If left empty, the status message will be cleared.
* @param emojiName
* The name of the status emoji for display in the client.
*/
public PostUserStatus(String statusText, String emojiName) {
this.statusText = statusText;
this.emojiName = emojiName;
setZulipAPIUrl("/api/v1/users/me/status");
}

@Override
public String execute(ZulipRestExecutor executor) {
setHttpController(executor.httpController);
HttpPost post = new HttpPost(this.httpController.getServer() + getZulipAPIUrl());
getParameters().put("status_text", this.statusText);
getParameters().put("emoji_name", this.emojiName);
return performRequest(getParameters(), post);
}
}