Skip to content

Commit 0e39489

Browse files
author
Markus Keunecke
committed
Additional constructor with reference to zuliprc (fix #8)
This constructor takes a file name as argument. The file name is used to parse the zuliprc file.
1 parent 24203e4 commit 0e39489

File tree

1 file changed

+34
-1
lines changed

1 file changed

+34
-1
lines changed

src/main/java/io/taliox/zulip/ZulipRestExecutor.java

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,14 @@
22

33
import io.taliox.zulip.calls.ZulipRestAPICall;
44
import io.taliox.zulip.controller.HttpController;
5+
import org.apache.commons.configuration2.INIConfiguration;
6+
import org.apache.commons.configuration2.SubnodeConfiguration;
7+
import org.apache.commons.configuration2.ex.ConfigurationException;
8+
9+
import java.io.FileNotFoundException;
10+
import java.io.FileReader;
11+
import java.io.IOException;
12+
import java.io.Reader;
513

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

38+
/**
39+
* A constructor for the Zulip REST API executor taking the file name of a zuliprc file. The information from the
40+
* file is used to configure the executor.
41+
* <br/>
42+
* An example that will be accepted:
43+
* [api]
44+
* email=some@mail.com
45+
* key=API_KEY
46+
* site=https://your.zulip.com
47+
* <br/>
48+
* The Executor is responsible for communicating with your Zulip server.
49+
* @param zuliprcFileName
50+
* Name of the zuliprc file which can be generated by a Zulip client.
51+
*/
52+
public ZulipRestExecutor(String zuliprcFileName) throws IOException, ConfigurationException {
53+
INIConfiguration iniConfiguration = new INIConfiguration();
54+
iniConfiguration.read(new FileReader(zuliprcFileName));
55+
SubnodeConfiguration apiSection = iniConfiguration.getSection("api");
56+
this.httpController = new HttpController(
57+
apiSection.getString("email"),
58+
apiSection.getString("key"),
59+
apiSection.getString("site")
60+
);
61+
}
62+
3063
/**
3164
* Executes a HTTP call to a Zulip server.
3265
*
@@ -38,4 +71,4 @@ public String executeCall(ZulipRestAPICall call) {
3871
return call.execute(this);
3972
}
4073

41-
}
74+
}

0 commit comments

Comments
 (0)