Skip to content

Commit a92d35e

Browse files
schalonerSteve Chaloner
andauthored
List categories (#109)
* List categories - Added `listCategories(chartKey)` to API - Updated README - Tests * Removed `chartKey` from response --------- Co-authored-by: Steve Chaloner <[email protected]>
1 parent 408c1e8 commit a92d35e

File tree

3 files changed

+45
-0
lines changed

3 files changed

+45
-0
lines changed

README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,20 @@ Event event = client.events.create(chart.key);
207207
System.out.println("Created event with key " + event.key);
208208
```
209209

210+
### Listing categories
211+
212+
```java
213+
import seatsio.SeatsioClient;
214+
import seatsio.Category;
215+
216+
SeatsioClient client = new SeatsioClient(Region.EU, "<WORKSPACE SECRET KEY>");
217+
218+
List<Category> categories = client.charts.listCategories("the chart key");
219+
categories.forEach(category -> {
220+
System.out.println("Category " + category.label);
221+
});
222+
```
223+
210224
## Error handling
211225

212226
When an API call results in a 4xx or 5xx error (e.g. when a chart could not be found), a SeatsioException is thrown.

src/main/java/seatsio/charts/Charts.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package seatsio.charts;
22

3+
import com.google.gson.Gson;
34
import com.google.gson.JsonElement;
45
import com.google.gson.JsonObject;
56
import com.google.gson.JsonParser;
@@ -111,6 +112,13 @@ public void removeCategory(String chartKey, CategoryKey categoryKey) {
111112
.routeParam("categoryKey", categoryKey.toString()));
112113
}
113114

115+
public List<Category> listCategories(String chartKey) {
116+
final String response = unirest.stringResponse(get(baseUrl + "/charts/{key}/categories")
117+
.routeParam("key", chartKey));
118+
final Gson gson = gson();
119+
return gson.fromJson(response, ListCategoriesResponse.class).categories;
120+
}
121+
114122
public Chart copy(String key) {
115123
String response = unirest.stringResponse(UnirestWrapper.post(baseUrl + "/charts/{key}/version/published/actions/copy")
116124
.routeParam("key", key));
@@ -238,4 +246,8 @@ private Map<String, Object> toMap(ChartListParams chartListParams) {
238246
}
239247
return chartListParams.asMap();
240248
}
249+
250+
private static final class ListCategoriesResponse {
251+
public List<Category> categories;
252+
}
241253
}

src/test/java/seatsio/charts/ManageCategoriesTest.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,14 @@
33
import com.google.common.collect.ImmutableMap;
44
import org.junit.jupiter.api.Test;
55
import seatsio.SeatsioClientTest;
6+
import seatsio.SeatsioException;
67

78
import java.util.List;
89
import java.util.Map;
910

1011
import static com.google.common.collect.Lists.newArrayList;
1112
import static org.assertj.core.api.Assertions.assertThat;
13+
import static org.junit.jupiter.api.Assertions.assertThrows;
1214

1315
@SuppressWarnings("unchecked")
1416
public class ManageCategoriesTest extends SeatsioClientTest {
@@ -53,6 +55,23 @@ public void removeCategory() {
5355
);
5456
}
5557

58+
@Test
59+
public void listCategories() {
60+
List<Category> categories = newArrayList(
61+
new Category(CategoryKey.of(1L), "Category 1", "#aaaaaa"),
62+
new Category(CategoryKey.of("cat2"), "Category 2", "#bbbbbb", true)
63+
);
64+
Chart chart = client.charts.create("aChart", "BOOTHS", categories);
65+
66+
final List<Category> categoryList = client.charts.listCategories(chart.key);
67+
assertThat(categoryList).isEqualTo(categories);
68+
}
69+
70+
@Test
71+
public void listCategories_unknownChart() {
72+
assertThrows(SeatsioException.class, () -> client.charts.listCategories("unknownChart"));
73+
}
74+
5675
private List<Map<?, ?>> categories(Map<?, ?> drawing) {
5776
Map<?, ?> categoriesMap = (Map<?, ?>) drawing.get("categories");
5877
return (List<Map<?, ?>>) categoriesMap.get("list");

0 commit comments

Comments
 (0)