Skip to content

Commit 76a0ef9

Browse files
feat: support website
1 parent 2f32bb0 commit 76a0ef9

5 files changed

Lines changed: 53 additions & 32 deletions

File tree

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

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ protected void shutDown() throws Exception {
6666
}
6767

6868
public Task getCurrentTask() throws Exception {
69-
final Task task = taskService.getCurrentTask(getCredentials().getIdentifier());
69+
final Task task = taskService.getCurrentTask(getCredentials());
7070
currentTaskOverlay.setTask(task);
7171
return task;
7272
}
@@ -84,11 +84,20 @@ public Task completeTask() throws Exception {
8484
}
8585

8686
public AccountProgress progress() throws Exception {
87-
return taskService.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.

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

Lines changed: 30 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import com.westerhoud.osrs.taskman.domain.AccountCredentials;
55
import com.westerhoud.osrs.taskman.domain.AccountProgress;
66
import com.westerhoud.osrs.taskman.domain.ErrorResponse;
7-
import com.westerhoud.osrs.taskman.domain.SheetRequestBody;
87
import com.westerhoud.osrs.taskman.domain.Task;
98
import java.io.IOException;
109
import lombok.extern.slf4j.Slf4j;
@@ -17,7 +16,10 @@
1716
@Slf4j
1817
public class TaskService implements com.westerhoud.osrs.taskman.api.TaskService {
1918

20-
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";
2123
private final OkHttpClient client;
2224
private final Gson gson;
2325
private final String currentUrl;
@@ -35,14 +37,20 @@ public TaskService(final OkHttpClient okHttpClient) {
3537
}
3638

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

4446
final Request request =
45-
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();
4654

4755
return executeRequest(request);
4856
}
@@ -53,7 +61,8 @@ public Task generateTask(final AccountCredentials credentials) throws IOExceptio
5361
new Request.Builder()
5462
.url(generateUrl)
5563
.header("Content-Type", "application/json")
56-
.post(getRequestBody(credentials.getIdentifier(), credentials.getPassword()))
64+
.addHeader(TASKMAN_SOURCE_HEADER, credentials.getSource().name())
65+
.post(getRequestBody(credentials))
5766
.build();
5867
return executeRequest(request);
5968
}
@@ -64,20 +73,28 @@ public Task completeTask(final AccountCredentials credentials) throws IOExceptio
6473
new Request.Builder()
6574
.url(completeUrl)
6675
.header("Content-Type", "application/json")
67-
.post(getRequestBody(credentials.getIdentifier(), credentials.getPassword()))
76+
.addHeader(TASKMAN_SOURCE_HEADER, credentials.getSource().name())
77+
.post(getRequestBody(credentials))
6878
.build();
6979
return executeRequest(request);
7080
}
7181

7282
@Override
73-
public AccountProgress getAccountProgress(final String key) throws IOException {
74-
if (key == null || key.isEmpty()) {
83+
public AccountProgress getAccountProgress(final AccountCredentials credentials)
84+
throws IOException {
85+
if (!credentials.isValid()) {
7586
throw new IllegalArgumentException(
7687
"Please set your username / spreadsheet key in the plugin configurations");
7788
}
7889

7990
final Request request =
80-
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();
8198

8299
final Response response = client.newCall(request).execute();
83100

@@ -89,9 +106,8 @@ public AccountProgress getAccountProgress(final String key) throws IOException {
89106
throw new IllegalArgumentException(error.getMessage());
90107
}
91108

92-
private RequestBody getRequestBody(final String key, final String passphrase) {
93-
final SheetRequestBody body = new SheetRequestBody(key, passphrase);
94-
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));
95111
}
96112

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

0 commit comments

Comments
 (0)