Skip to content

Commit cfcfded

Browse files
committed
correspond to the paging
1 parent 44e9c26 commit cfcfded

20 files changed

+305
-116
lines changed

.classpath

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,6 @@
2727
<attribute name="maven.pomderived" value="true"/>
2828
</attributes>
2929
</classpathentry>
30-
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
30+
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/jdk1.7.0_07"/>
3131
<classpathentry kind="output" path="target/classes"/>
3232
</classpath>
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
eclipse.preferences.version=1
2+
encoding/<project>=UTF-8
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
eclipse.preferences.version=1
2+
line.separator=\n

src/main/java/com/zaneli/qiita/QiitaClient.java

Lines changed: 27 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import com.zaneli.qiita.model.request.SearchRequest;
88
import com.zaneli.qiita.model.response.ItemDetail;
99
import com.zaneli.qiita.model.response.ItemInfo;
10+
import com.zaneli.qiita.model.response.PageableResponse;
1011
import com.zaneli.qiita.model.response.RateLimit;
1112
import com.zaneli.qiita.model.response.TagInfo;
1213
import com.zaneli.qiita.model.response.TokenInfo;
@@ -28,56 +29,60 @@ public void setToken(String token) {
2829
executor.setToken(token);
2930
}
3031

32+
public void setPerPage(int perPage) {
33+
executor.setPerPage(perPage);
34+
}
35+
3136
public TokenInfo authorize(AuthRequest req) throws IOException, QiitaException {
3237
return executor.postFormValue("auth", req.createParams(), TokenInfo.class);
3338
}
3439

3540
public RateLimit getRateLimit() throws IOException, QiitaException {
36-
return executor.getSingleContent("rate_limit", RateLimit.class);
41+
return executor.getContent("rate_limit", RateLimit.class);
3742
}
3843

3944
public UserInfo getOwnInfo() throws IOException, QiitaException {
40-
return executor.getSingleContent("user", UserInfo.class);
45+
return executor.getContent("user", UserInfo.class);
4146
}
4247

4348
public UserInfo getUserInfo(String userName) throws IOException, QiitaException {
44-
return executor.getSingleContent("users/" + userName, UserInfo.class);
49+
return executor.getContent("users/" + userName, UserInfo.class);
4550
}
4651

47-
public ItemInfo[] getUserItems(String userName) throws IOException, QiitaException {
48-
return executor.getMultiContents("users/" + userName + "/items", ItemInfo[].class);
52+
public PageableResponse<ItemInfo> getUserItems(String userName) throws IOException, QiitaException {
53+
return executor.getPageableContents("users/" + userName + "/items", ItemInfo[].class);
4954
}
5055

51-
public ItemInfo[] getUserStocks(String userName) throws IOException, QiitaException {
52-
return executor.getMultiContents("users/" + userName + "/stocks", ItemInfo[].class);
56+
public PageableResponse<ItemInfo> getUserStocks(String userName) throws IOException, QiitaException {
57+
return executor.getPageableContents("users/" + userName + "/stocks", ItemInfo[].class);
5358
}
5459

55-
public User[] getFollowingUsers(String userName) throws IOException, QiitaException {
56-
return executor.getMultiContents("users/" + userName + "/following_users", User[].class);
60+
public PageableResponse<User> getFollowingUsers(String userName) throws IOException, QiitaException {
61+
return executor.getPageableContents("users/" + userName + "/following_users", User[].class);
5762
}
5863

59-
public TagInfo[] getFollowingTags(String userName) throws IOException, QiitaException {
60-
return executor.getMultiContents("users/" + userName + "/following_tags", TagInfo[].class);
64+
public PageableResponse<TagInfo> getFollowingTags(String userName) throws IOException, QiitaException {
65+
return executor.getPageableContents("users/" + userName + "/following_tags", TagInfo[].class);
6166
}
6267

63-
public ItemInfo[] getTagItems(String tagName) throws IOException, QiitaException {
64-
return executor.getMultiContents("tags/" + tagName + "/items", ItemInfo[].class);
68+
public PageableResponse<ItemInfo> getTagItems(String tagName) throws IOException, QiitaException {
69+
return executor.getPageableContents("tags/" + tagName + "/items", ItemInfo[].class);
6570
}
6671

67-
public TagInfo[] getTags() throws IOException, QiitaException {
68-
return executor.getMultiContents("tags", TagInfo[].class);
72+
public PageableResponse<TagInfo> getTags() throws IOException, QiitaException {
73+
return executor.getPageableContents("tags", TagInfo[].class);
6974
}
7075

71-
public ItemInfo[] searchItems(SearchRequest req) throws IOException, QiitaException {
72-
return executor.getMultiContents("search", req.createParams(), ItemInfo[].class);
76+
public PageableResponse<ItemInfo> searchItems(SearchRequest req) throws IOException, QiitaException {
77+
return executor.getPageableContents("search", req.createParams(), ItemInfo[].class);
7378
}
7479

75-
public ItemInfo[] getNewItems() throws IOException, QiitaException {
76-
return executor.getMultiContents("items", ItemInfo[].class);
80+
public PageableResponse<ItemInfo> getNewItems() throws IOException, QiitaException {
81+
return executor.getPageableContents("items", ItemInfo[].class);
7782
}
7883

79-
public ItemInfo[] getOwnStocks() throws IOException, QiitaException {
80-
return executor.getMultiContents("stocks", ItemInfo[].class);
84+
public PageableResponse<ItemInfo> getOwnStocks() throws IOException, QiitaException {
85+
return executor.getPageableContents("stocks", ItemInfo[].class);
8186
}
8287

8388
public ItemInfo createItem(ItemRequest req) throws IOException, QiitaException {
@@ -93,7 +98,7 @@ public void deleteItem(String uuid) throws IOException, QiitaException {
9398
}
9499

95100
public ItemDetail getSpecificItem(String uuid) throws IOException, QiitaException {
96-
return executor.getSingleContent("items/" + uuid, ItemDetail.class);
101+
return executor.getContent("items/" + uuid, ItemDetail.class);
97102
}
98103

99104
public void stockItem(String uuid) throws IOException, QiitaException {

src/main/java/com/zaneli/qiita/QiitaExecutor.java

Lines changed: 40 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,20 @@
1111
import java.io.IOException;
1212
import java.io.InputStream;
1313
import java.io.UnsupportedEncodingException;
14+
import java.net.URI;
15+
import java.net.URISyntaxException;
1416
import java.net.URLEncoder;
1517
import java.util.ArrayList;
1618
import java.util.Collections;
19+
import java.util.HashMap;
1720
import java.util.List;
1821
import java.util.Map;
1922
import java.util.Map.Entry;
2023

2124
import net.arnx.jsonic.JSON;
2225

2326
import org.apache.commons.lang3.StringUtils;
27+
import org.apache.http.Header;
2428
import org.apache.http.HttpResponse;
2529
import org.apache.http.NameValuePair;
2630
import org.apache.http.client.HttpClient;
@@ -37,26 +41,33 @@
3741
import org.apache.http.message.BasicNameValuePair;
3842

3943
import com.zaneli.qiita.model.response.Error;
44+
import com.zaneli.qiita.model.response.PageableResponse;
4045
import com.zaneli.qiita.model.response.QiitaResponse;
4146

42-
class QiitaExecutor {
47+
public class QiitaExecutor {
4348

4449
private static final String ENDPOINT_URL = "https://qiita.com/api/v1";
4550

4651
private String token;
4752

53+
private int perPage = 20;
54+
4855
QiitaExecutor() {
4956
}
5057

5158
void setToken(String token) {
5259
this.token = token;
5360
}
5461

55-
<T extends QiitaResponse> T getSingleContent(String apiPath, Class<T> responseType) throws IOException, QiitaException {
56-
return getSingleContent(apiPath, Collections.<String, String>emptyMap(), responseType);
62+
public void setPerPage(int perPage) {
63+
this.perPage = perPage;
64+
}
65+
66+
<T extends QiitaResponse> T getContent(String apiPath, Class<T> responseType) throws IOException, QiitaException {
67+
return getContent(apiPath, Collections.<String, String>emptyMap(), responseType);
5768
}
5869

59-
<T extends QiitaResponse> T getSingleContent(
70+
<T extends QiitaResponse> T getContent(
6071
String apiPath, Map<String, String> params, Class<T> responseType) throws IOException, QiitaException {
6172
HttpGet request = new HttpGet(createQuery(createUrl(apiPath, token), params));
6273
HttpResponse response = execute(request);
@@ -67,18 +78,30 @@ <T extends QiitaResponse> T getSingleContent(
6778
}
6879
}
6980

70-
<T extends QiitaResponse> T[] getMultiContents(String apiPath, Class<T[]> responseType) throws IOException, QiitaException {
71-
return getMultiContents(apiPath, Collections.<String, String>emptyMap(), responseType);
81+
<T extends QiitaResponse> PageableResponse<T> getPageableContents(
82+
String apiPath, Class<T[]> responseType) throws IOException, QiitaException {
83+
return getPageableContents(apiPath, new HashMap<String, String>(), responseType);
7284
}
7385

74-
<T extends QiitaResponse> T[] getMultiContents(
86+
<T extends QiitaResponse> PageableResponse<T> getPageableContents(
7587
String apiPath, Map<String, String> params, Class<T[]> responseType) throws IOException, QiitaException {
76-
HttpGet request = new HttpGet(createQuery(createUrl(apiPath, token), params));
88+
params.put("per_page", Integer.toString(perPage));
89+
try {
90+
return getPageableContents(new URI(createQuery(createUrl(apiPath, token), params)), params, responseType);
91+
} catch (URISyntaxException e) {
92+
throw new QiitaException(e);
93+
}
94+
}
95+
96+
public <T extends QiitaResponse> PageableResponse<T> getPageableContents(
97+
URI uri, Map<String, String> params, Class<T[]> responseType) throws IOException, QiitaException {
98+
HttpGet request = new HttpGet(uri);
7799
HttpResponse response = execute(request);
78100
verifyStatusCode(response, SC_OK);
101+
String[] linkHeaderValues = getHeaderValues(response.getHeaders("Link"));
79102
try (InputStream in = response.getEntity().getContent()) {
80103
T[] res = JSON.decode(in, responseType);
81-
return res;
104+
return new PageableResponse<T>(this, params, res, linkHeaderValues);
82105
}
83106
}
84107

@@ -185,4 +208,12 @@ private static String createUrl(String apiPath, String token) {
185208
}
186209
return ENDPOINT_URL + '/' + apiPath + "?token=" + token;
187210
}
211+
212+
private static String[] getHeaderValues(Header[] headers) {
213+
List<String> values = new ArrayList<>();
214+
for (Header header : headers) {
215+
values.add(header.getValue());
216+
}
217+
return values.toArray(new String[headers.length]);
218+
}
188219
}

src/main/java/com/zaneli/qiita/model/response/ItemInfo.java

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
package com.zaneli.qiita.model.response;
22

33
import java.io.Serializable;
4+
import java.text.DateFormat;
45
import java.text.ParseException;
6+
import java.text.SimpleDateFormat;
57
import java.util.Date;
68

79
import org.apache.commons.lang3.builder.EqualsBuilder;
810
import org.apache.commons.lang3.builder.HashCodeBuilder;
911
import org.apache.commons.lang3.builder.ToStringBuilder;
1012

11-
import com.zaneli.qiita.util.DateTimeUtil;
12-
1313
@SuppressWarnings("serial")
1414
public class ItemInfo extends QiitaResponse {
1515

@@ -76,15 +76,15 @@ public Date getCreatedAt() {
7676
}
7777

7878
public void setCreatedAt(String createdAt) throws ParseException {
79-
this.createdAt = DateTimeUtil.parse(createdAt);
79+
this.createdAt = parse(createdAt);
8080
}
8181

8282
public Date getUpdatedAt() {
8383
return updatedAt;
8484
}
8585

8686
public void setUpdatedAt(String updatedAt) throws ParseException {
87-
this.updatedAt = DateTimeUtil.parse(updatedAt);
87+
this.updatedAt = parse(updatedAt);
8888
}
8989

9090
public String getCreatedAtInWords() {
@@ -220,4 +220,18 @@ public int hashCode() {
220220
return HashCodeBuilder.reflectionHashCode(this);
221221
}
222222
}
223+
224+
225+
private static final ThreadLocal<DateFormat> DATE_FORMAT = new ThreadLocal<DateFormat>() {
226+
@Override
227+
protected DateFormat initialValue() {
228+
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss +0900");
229+
dateFormat.setLenient(false);
230+
return dateFormat;
231+
}
232+
};
233+
234+
private static Date parse(String dateString) throws ParseException {
235+
return DATE_FORMAT.get().parse(dateString);
236+
}
223237
}

0 commit comments

Comments
 (0)