Skip to content

Commit 04af7e6

Browse files
committed
Added list module items API.
1 parent 1d6ef4c commit 04af7e6

File tree

5 files changed

+231
-0
lines changed

5 files changed

+231
-0
lines changed

src/main/java/edu/ksu/canvas/CanvasApiFactory.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,7 @@ private void setupClassMap() {
179179
readerMap.put(CourseSettingsReader.class, CourseSettingsImpl.class);
180180
readerMap.put(GradingStandardReader.class, GradingStandardImpl.class);
181181
readerMap.put(ModuleReader.class, ModuleImpl.class);
182+
readerMap.put(ModuleItemReader.class, ModuleItemImpl.class);
182183
readerMap.put(SisImportReader.class, SisImportImpl.class);
183184
readerMap.put(SelectiveDataReader.class, SelectiveDataImpl.class);
184185
readerMap.put(MigrationIssueReader.class, MigrationIssueImpl.class);
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package edu.ksu.canvas.impl;
2+
3+
import com.google.gson.reflect.TypeToken;
4+
import edu.ksu.canvas.interfaces.CanvasWriter;
5+
import edu.ksu.canvas.interfaces.ModuleItemReader;;
6+
import edu.ksu.canvas.model.ModuleItem;
7+
import edu.ksu.canvas.net.RestClient;
8+
import edu.ksu.canvas.oauth.OauthToken;
9+
import edu.ksu.canvas.requestOptions.ListModuleItemsOptions;
10+
11+
import org.slf4j.Logger;
12+
import org.slf4j.LoggerFactory;
13+
14+
import java.io.IOException;
15+
import java.lang.reflect.Type;
16+
import java.util.List;
17+
18+
public class ModuleItemImpl extends BaseImpl<ModuleItem, ModuleItemReader, CanvasWriter> implements ModuleItemReader {
19+
private static final Logger LOG = LoggerFactory.getLogger(ModuleItemImpl.class);
20+
public ModuleItemImpl(final String canvasBaseUrl, final Integer apiVersion, final OauthToken oauthToken, final RestClient restClient, final int connectTimeout, final int readTimeout, final Integer paginationPageSize, final Boolean serializeNulls) {
21+
super(canvasBaseUrl, apiVersion, oauthToken, restClient, connectTimeout, readTimeout, paginationPageSize, serializeNulls);
22+
}
23+
24+
@Override
25+
public List<ModuleItem> getModuleItemsInModule(final ListModuleItemsOptions options) throws IOException {
26+
LOG.debug("Retrieving items for module {}", options.getModuleId());
27+
String url = buildCanvasUrl(String.format("courses/%d/modules/%d/items", options.getCourseId(), options.getModuleId()), options.getOptionsMap());
28+
return getListFromCanvas(url);
29+
}
30+
31+
@Override
32+
protected Type listType() {
33+
return new TypeToken<List<ModuleItem>>() {
34+
}.getType();
35+
}
36+
37+
@Override
38+
protected Class<ModuleItem> objectType() {
39+
return ModuleItem.class;
40+
}
41+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package edu.ksu.canvas.interfaces;
2+
3+
import edu.ksu.canvas.model.ModuleItem;
4+
import edu.ksu.canvas.requestOptions.ListModuleItemsOptions;
5+
6+
import java.io.IOException;
7+
import java.util.List;
8+
9+
public interface ModuleItemReader extends CanvasReader<ModuleItem, ModuleItemReader> {
10+
/**
11+
* Retrieve the list of items in a module.
12+
*
13+
* @param options The object holding options for this API call
14+
* @return List of the items in a module
15+
* @throws IOException When there is an error communicating with Canvas
16+
*/
17+
public List<ModuleItem> getModuleItemsInModule(ListModuleItemsOptions options) throws IOException;
18+
}
Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
package edu.ksu.canvas.model;
2+
3+
import java.io.Serializable;
4+
5+
/* https://canvas.instructure.com/doc/api/modules.html#ModuleItem */
6+
public class ModuleItem extends BaseCanvasModel implements Serializable {
7+
private Long id;
8+
private Long moduleId;
9+
private Long position;
10+
private String title;
11+
private Long indent;
12+
private String type;
13+
private Long contentId;
14+
private String htmlUrl;
15+
private String url; /* optional */
16+
private String pageUrl; /* only for 'Page' type */
17+
private String externalUrl; /* only for 'ExternalUrl' and 'ExternalTool' types */
18+
private Boolean newTab; /* only for 'ExternalTool' type */
19+
private Boolean published;
20+
21+
public Long getId() {
22+
return id;
23+
}
24+
25+
public void setId(Long id) {
26+
this.id = id;
27+
}
28+
29+
public Long getModuleId() {
30+
return moduleId;
31+
}
32+
33+
public void setModuleId(Long moduleId) {
34+
this.moduleId = moduleId;
35+
}
36+
37+
public Long getPosition() {
38+
return position;
39+
}
40+
41+
public void setPosition(Long position) {
42+
this.position = position;
43+
}
44+
45+
public String getTitle() {
46+
return title;
47+
}
48+
49+
public void setTitle(String title) {
50+
this.title = title;
51+
}
52+
53+
public Long getIndent() {
54+
return indent;
55+
}
56+
57+
public void setIndent(Long indent) {
58+
this.indent = indent;
59+
}
60+
61+
public String getType() {
62+
return type;
63+
}
64+
65+
public void setType(String type) {
66+
this.type = type;
67+
}
68+
69+
public long getContentId() {
70+
return contentId;
71+
}
72+
73+
public void setContenId(long contentId) {
74+
this.contentId = contentId;
75+
}
76+
77+
public String getHtmlUrl() {
78+
return htmlUrl;
79+
}
80+
81+
public void setHtmlUrl(String htmlUrl) {
82+
this.htmlUrl = htmlUrl;
83+
}
84+
85+
public String getUrl() {
86+
return url;
87+
}
88+
89+
public void setUrl(String url) {
90+
this.url = url;
91+
}
92+
93+
public String getPageUrl() {
94+
return pageUrl;
95+
}
96+
97+
public void setPageUrl(String pageUrl) {
98+
this.pageUrl = pageUrl;
99+
}
100+
101+
public String getExternalUrl() {
102+
return externalUrl;
103+
}
104+
105+
public void setExternalUrl(String externalUrl) {
106+
this.externalUrl = externalUrl;
107+
}
108+
109+
public java.lang.Boolean getNewTab() {
110+
return newTab;
111+
}
112+
113+
public void setNewTab(java.lang.Boolean newTab) {
114+
this.newTab = newTab;
115+
}
116+
117+
public Boolean getPublished() {
118+
return published;
119+
}
120+
121+
public void setPublished(Boolean published) {
122+
this.published = published;
123+
}
124+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package edu.ksu.canvas.requestOptions;
2+
3+
import java.util.List;
4+
5+
/**
6+
* Options for listing the items in a module.
7+
*
8+
* @see <a href="https://canvas.instructure.com/doc/api/modules.html#method.context_module_items_api.index">List module items</a>
9+
*/
10+
public class ListModuleItemsOptions extends BaseOptions {
11+
12+
public enum Include {
13+
CONTENT_DETAILS
14+
}
15+
16+
private Long courseId;
17+
18+
private Long moduleId;
19+
20+
public ListModuleItemsOptions(Long courseId, Long moduleId) {
21+
this.courseId = courseId;
22+
this.moduleId = moduleId;
23+
}
24+
25+
public Long getCourseId() {
26+
return courseId;
27+
}
28+
29+
public Long getModuleId() {
30+
return moduleId;
31+
}
32+
33+
public ListModuleItemsOptions includes(List<Include> includes) {
34+
addEnumList("include[]", includes);
35+
return this;
36+
}
37+
38+
public ListModuleItemsOptions searchTerm(String searchTerm) {
39+
addSingleItem("search_term", searchTerm);
40+
return this;
41+
}
42+
43+
public ListModuleItemsOptions studentId(String studentId) {
44+
addSingleItem("student_id", studentId);
45+
return this;
46+
}
47+
}

0 commit comments

Comments
 (0)