Skip to content

Commit 3aaca1a

Browse files
Merge pull request #1 from remcowesterhoud/website_support
Website support
2 parents 50266dd + 76a0ef9 commit 3aaca1a

7 files changed

Lines changed: 119 additions & 45 deletions

File tree

src/main/java/com/westerhoud/osrs/taskman/TaskmanConfig.java

Lines changed: 53 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,80 @@
11
package com.westerhoud.osrs.taskman;
22

3+
import com.westerhoud.osrs.taskman.domain.TaskSource;
34
import net.runelite.client.config.Config;
45
import net.runelite.client.config.ConfigGroup;
56
import net.runelite.client.config.ConfigItem;
7+
import net.runelite.client.config.ConfigSection;
68

79
@ConfigGroup("taskman")
810
public interface TaskmanConfig extends Config {
9-
@ConfigItem(
11+
12+
@ConfigSection(
13+
name = "Spreadsheet",
14+
description = "Spreadsheet configuration",
1015
position = 1,
16+
closedByDefault = true)
17+
String spreadsheet = "spreadsheet";
18+
19+
@ConfigSection(
20+
name = "Website",
21+
description = "Website configuration",
22+
position = 2,
23+
closedByDefault = true)
24+
String website = "website";
25+
26+
@ConfigItem(
27+
position = 0,
28+
keyName = "taskSource",
29+
name = "Task source",
30+
description = "Spreadsheet or website")
31+
default TaskSource taskSource() {
32+
return TaskSource.SPREADSHEET;
33+
}
34+
35+
@ConfigItem(
36+
position = 3,
1137
keyName = "key",
1238
name = "Spreadsheet key",
13-
description = "The key of your spreadsheet")
39+
description = "The key of your spreadsheet",
40+
section = spreadsheet)
1441
default String spreadsheetKey() {
1542
return "";
1643
}
1744

1845
@ConfigItem(
19-
position = 2,
46+
position = 4,
2047
keyName = "passphrase",
2148
name = "Passphrase",
2249
secret = true,
23-
description = "The passphrase you have added in your sheet")
50+
description = "The passphrase you have added in your sheet",
51+
section = spreadsheet)
2452
default String passphrase() {
2553
return "";
2654
}
2755

2856
@ConfigItem(
29-
position = 2,
57+
position = 5,
58+
keyName = "websiteUsername",
59+
name = "Username",
60+
description = "Website username",
61+
section = website)
62+
default String websiteUsername() {
63+
return "username";
64+
}
65+
66+
@ConfigItem(
67+
position = 6,
68+
keyName = "websitePassword",
69+
name = "Password",
70+
description = "Website password",
71+
section = website)
72+
default String websitePassword() {
73+
return "password";
74+
}
75+
76+
@ConfigItem(
77+
position = 7,
3078
keyName = "showOverlay",
3179
name = "Show current task overlay",
3280
description = "Adds an overlay displaying the current task to the game client")

src/main/java/com/westerhoud/osrs/taskman/TaskmanPlugin.java

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import com.westerhoud.osrs.taskman.domain.AccountCredentials;
55
import com.westerhoud.osrs.taskman.domain.AccountProgress;
66
import com.westerhoud.osrs.taskman.domain.Task;
7-
import com.westerhoud.osrs.taskman.service.SheetService;
7+
import com.westerhoud.osrs.taskman.service.TaskService;
88
import com.westerhoud.osrs.taskman.ui.CurrentTaskOverlay;
99
import com.westerhoud.osrs.taskman.ui.TaskmanPluginPanel;
1010
import java.awt.image.BufferedImage;
@@ -36,15 +36,15 @@ public class TaskmanPlugin extends Plugin {
3636
@Inject private CurrentTaskOverlay currentTaskOverlay;
3737

3838
private TaskmanPluginPanel sidePanel;
39-
private SheetService sheetService;
39+
private TaskService taskService;
4040
private NavigationButton navigationButton;
4141

4242
@Override
4343
protected void startUp() throws Exception {
4444
// Sidebar
4545
final BufferedImage icon = ImageUtil.loadImageResource(getClass(), "icon.png");
4646

47-
sheetService = new SheetService(okHttpClient);
47+
taskService = new TaskService(okHttpClient);
4848

4949
sidePanel = new TaskmanPluginPanel(this);
5050
navigationButton =
@@ -66,29 +66,38 @@ protected void shutDown() throws Exception {
6666
}
6767

6868
public Task getCurrentTask() throws Exception {
69-
final Task task = sheetService.getCurrentTask(getCredentials().getIdentifier());
69+
final Task task = taskService.getCurrentTask(getCredentials());
7070
currentTaskOverlay.setTask(task);
7171
return task;
7272
}
7373

7474
public Task generateTask() throws Exception {
75-
final Task task = sheetService.generateTask(getCredentials());
75+
final Task task = taskService.generateTask(getCredentials());
7676
currentTaskOverlay.setTask(task);
7777
return task;
7878
}
7979

8080
public Task completeTask() throws Exception {
81-
final Task task = sheetService.completeTask(getCredentials());
81+
final Task task = taskService.completeTask(getCredentials());
8282
currentTaskOverlay.setTask(task);
8383
return task;
8484
}
8585

8686
public AccountProgress progress() throws Exception {
87-
return sheetService.getAccountProgress(getCredentials().getIdentifier());
87+
return taskService.getAccountProgress(getCredentials());
8888
}
8989

9090
private AccountCredentials getCredentials() {
91-
return new AccountCredentials(config.spreadsheetKey(), config.passphrase());
91+
switch (config.taskSource()) {
92+
case SPREADSHEET:
93+
return new AccountCredentials(
94+
config.spreadsheetKey(), config.passphrase(), config.taskSource());
95+
case WEBSITE:
96+
return new AccountCredentials(
97+
config.websiteUsername(), config.websitePassword(), config.taskSource());
98+
default:
99+
throw new IllegalArgumentException("No task source selected in config.");
100+
}
92101
}
93102

94103
@Subscribe

src/main/java/com/westerhoud/osrs/taskman/api/TaskService.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@
77

88
public interface TaskService {
99

10-
Task getCurrentTask(final String identifier) throws IOException;
10+
Task getCurrentTask(final AccountCredentials credentials) throws IOException;
1111

1212
Task generateTask(final AccountCredentials credentials) throws IOException;
1313

1414
Task completeTask(final AccountCredentials credentials) throws IOException;
1515

16-
AccountProgress getAccountProgress(final String identifier) throws IOException;
16+
AccountProgress getAccountProgress(final AccountCredentials credentials) throws IOException;
1717
}

src/main/java/com/westerhoud/osrs/taskman/domain/AccountCredentials.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,13 @@ public class AccountCredentials {
99

1010
final String identifier;
1111
final String password;
12+
final TaskSource source;
13+
14+
public boolean isValid() {
15+
return identifier != null
16+
&& !identifier.isEmpty()
17+
&& password != null
18+
&& !password.isEmpty()
19+
&& source != null;
20+
}
1221
}

src/main/java/com/westerhoud/osrs/taskman/domain/SheetRequestBody.java

Lines changed: 0 additions & 13 deletions
This file was deleted.
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package com.westerhoud.osrs.taskman.domain;
2+
3+
public enum TaskSource {
4+
SPREADSHEET,
5+
WEBSITE
6+
}

src/main/java/com/westerhoud/osrs/taskman/service/SheetService.java renamed to src/main/java/com/westerhoud/osrs/taskman/service/TaskService.java

Lines changed: 32 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
package com.westerhoud.osrs.taskman.service;
22

33
import com.google.gson.Gson;
4-
import com.westerhoud.osrs.taskman.api.TaskService;
54
import com.westerhoud.osrs.taskman.domain.AccountCredentials;
65
import com.westerhoud.osrs.taskman.domain.AccountProgress;
76
import com.westerhoud.osrs.taskman.domain.ErrorResponse;
8-
import com.westerhoud.osrs.taskman.domain.SheetRequestBody;
97
import com.westerhoud.osrs.taskman.domain.Task;
108
import java.io.IOException;
119
import lombok.extern.slf4j.Slf4j;
@@ -16,17 +14,20 @@
1614
import okhttp3.Response;
1715

1816
@Slf4j
19-
public class SheetService implements TaskService {
17+
public class TaskService implements com.westerhoud.osrs.taskman.api.TaskService {
2018

21-
private static final String BASE_URL = "https://osrs-taskman.herokuapp.com/sheet";
19+
public static final String TASKMAN_IDENTIFIER_HEADER = "x-taskman-identifier";
20+
public static final String TASKMAN_PASSWORD_HEADER = "x-taskman-password";
21+
public static final String TASKMAN_SOURCE_HEADER = "x-taskman-source";
22+
private static final String BASE_URL = "https://osrs-taskman.herokuapp.com/task";
2223
private final OkHttpClient client;
2324
private final Gson gson;
2425
private final String currentUrl;
2526
private final String generateUrl;
2627
private final String completeUrl;
2728
private final String progressUrl;
2829

29-
public SheetService(final OkHttpClient okHttpClient) {
30+
public TaskService(final OkHttpClient okHttpClient) {
3031
client = okHttpClient;
3132
gson = new Gson();
3233
currentUrl = BASE_URL + "/current";
@@ -36,14 +37,20 @@ public SheetService(final OkHttpClient okHttpClient) {
3637
}
3738

3839
@Override
39-
public Task getCurrentTask(final String key) throws IOException {
40-
if (key == null || key.isEmpty()) {
40+
public Task getCurrentTask(final AccountCredentials credentials) throws IOException {
41+
if (!credentials.isValid()) {
4142
throw new IllegalArgumentException(
42-
"Please set your username / spreadsheet key in the plugin configurations");
43+
"Please configure your credentials in the plugin configurations");
4344
}
4445

4546
final Request request =
46-
new Request.Builder().url(String.format("%s?key=%s", currentUrl, key)).get().build();
47+
new Request.Builder()
48+
.url(currentUrl)
49+
.addHeader(TASKMAN_IDENTIFIER_HEADER, credentials.getIdentifier())
50+
.addHeader(TASKMAN_PASSWORD_HEADER, credentials.getPassword())
51+
.addHeader(TASKMAN_SOURCE_HEADER, credentials.getSource().name())
52+
.get()
53+
.build();
4754

4855
return executeRequest(request);
4956
}
@@ -54,7 +61,8 @@ public Task generateTask(final AccountCredentials credentials) throws IOExceptio
5461
new Request.Builder()
5562
.url(generateUrl)
5663
.header("Content-Type", "application/json")
57-
.post(getRequestBody(credentials.getIdentifier(), credentials.getPassword()))
64+
.addHeader(TASKMAN_SOURCE_HEADER, credentials.getSource().name())
65+
.post(getRequestBody(credentials))
5866
.build();
5967
return executeRequest(request);
6068
}
@@ -65,20 +73,28 @@ public Task completeTask(final AccountCredentials credentials) throws IOExceptio
6573
new Request.Builder()
6674
.url(completeUrl)
6775
.header("Content-Type", "application/json")
68-
.post(getRequestBody(credentials.getIdentifier(), credentials.getPassword()))
76+
.addHeader(TASKMAN_SOURCE_HEADER, credentials.getSource().name())
77+
.post(getRequestBody(credentials))
6978
.build();
7079
return executeRequest(request);
7180
}
7281

7382
@Override
74-
public AccountProgress getAccountProgress(final String key) throws IOException {
75-
if (key == null || key.isEmpty()) {
83+
public AccountProgress getAccountProgress(final AccountCredentials credentials)
84+
throws IOException {
85+
if (!credentials.isValid()) {
7686
throw new IllegalArgumentException(
7787
"Please set your username / spreadsheet key in the plugin configurations");
7888
}
7989

8090
final Request request =
81-
new Request.Builder().url(String.format("%s?key=%s", progressUrl, key)).get().build();
91+
new Request.Builder()
92+
.url(progressUrl)
93+
.addHeader(TASKMAN_IDENTIFIER_HEADER, credentials.getIdentifier())
94+
.addHeader(TASKMAN_PASSWORD_HEADER, credentials.getPassword())
95+
.addHeader(TASKMAN_SOURCE_HEADER, credentials.getSource().name())
96+
.get()
97+
.build();
8298

8399
final Response response = client.newCall(request).execute();
84100

@@ -90,9 +106,8 @@ public AccountProgress getAccountProgress(final String key) throws IOException {
90106
throw new IllegalArgumentException(error.getMessage());
91107
}
92108

93-
private RequestBody getRequestBody(final String key, final String passphrase) {
94-
final SheetRequestBody body = new SheetRequestBody(key, passphrase);
95-
return RequestBody.create(MediaType.parse("application/json"), gson.toJson(body));
109+
private RequestBody getRequestBody(final AccountCredentials credentials) {
110+
return RequestBody.create(MediaType.parse("application/json"), gson.toJson(credentials));
96111
}
97112

98113
private Task executeRequest(final Request request) throws IOException {

0 commit comments

Comments
 (0)