Skip to content
This repository has been archived by the owner on Oct 20, 2022. It is now read-only.

Commit

Permalink
Add get trends feature
Browse files Browse the repository at this point in the history
  • Loading branch information
vergnesOL committed Mar 19, 2012
1 parent 61d3d98 commit d121734
Show file tree
Hide file tree
Showing 9 changed files with 284 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* Copyright 2011 France Telecom R&D Beijing Co., Ltd 北京法国电信研发中心有限公司
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http:https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.social.weibo.api;

import java.util.List;

public interface TrendOperations {

List<UserTrend> getTrends(long userId);

List<UserTrend> getTrends(long userId, int pageSize, int pageNumber);

}
69 changes: 69 additions & 0 deletions src/main/java/org/springframework/social/weibo/api/UserTrend.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*
* Copyright 2011 France Telecom R&D Beijing Co., Ltd 北京法国电信研发中心有限公司
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http:https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.social.weibo.api;

public class UserTrend {

private long id;
private String num;
private String hotword;

/**
* @return the id
*/
public long getId() {
return id;
}

/**
* @param id
* the id to set
*/
public void setId(long id) {
this.id = id;
}

/**
* @return the num
*/
public String getNum() {
return num;
}

/**
* @param num
* the num to set
*/
public void setNum(String num) {
this.num = num;
}

/**
* @return the hotword
*/
public String getHotword() {
return hotword;
}

/**
* @param hotword
* the hotword to set
*/
public void setHotword(String hotword) {
this.hotword = hotword;
}

}
2 changes: 2 additions & 0 deletions src/main/java/org/springframework/social/weibo/api/Weibo.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,6 @@ public interface Weibo {

UserOperations userOperations();

TrendOperations trendOperations();

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* Copyright 2011 France Telecom R&D Beijing Co., Ltd 北京法国电信研发中心有限公司
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http:https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.social.weibo.api.impl;

import java.util.List;

import org.codehaus.jackson.JsonNode;
import org.codehaus.jackson.map.ObjectMapper;
import org.springframework.social.weibo.api.TrendOperations;
import org.springframework.social.weibo.api.UserTrend;
import org.springframework.web.client.RestTemplate;

public class TrendTemplate extends AbstractWeiboOperations implements
TrendOperations {

protected TrendTemplate(ObjectMapper objectMapper,
RestTemplate restTemplate, boolean isAuthorized) {
super(objectMapper, restTemplate, isAuthorized);
}

@Override
public List<UserTrend> getTrends(long userId) {
requireAuthorization();
JsonNode jsonNode = restTemplate.getForObject(
buildUri("trends.json", "uid", String.valueOf(userId)),
JsonNode.class);
return deserializeDataList(jsonNode, UserTrend.class);
}

@Override
public List<UserTrend> getTrends(long userId, int pageSize, int pageNumber) {
requireAuthorization();
JsonNode jsonNode = restTemplate
.getForObject(
uriBuilder("trends.json")
.queryParam("uid", String.valueOf(userId))
.queryParam("count", String.valueOf(pageSize))
.queryParam("page", String.valueOf(pageNumber))
.build(), JsonNode.class);
return deserializeDataList(jsonNode, UserTrend.class);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import org.springframework.social.weibo.api.FavoriteOperations;
import org.springframework.social.weibo.api.FriendOperations;
import org.springframework.social.weibo.api.TimelineOperations;
import org.springframework.social.weibo.api.TrendOperations;
import org.springframework.social.weibo.api.UserOperations;
import org.springframework.social.weibo.api.Weibo;
import org.springframework.social.weibo.api.impl.json.WeiboModule;
Expand All @@ -56,6 +57,8 @@ public class WeiboTemplate extends AbstractOAuth2ApiBinding implements Weibo {

private FavoriteTemplate favouriteOperations;

private TrendTemplate trendOperations;

public WeiboTemplate(String accessToken) {
super(accessToken);
initialize();
Expand Down Expand Up @@ -122,6 +125,8 @@ private void initSubApis() {
getRestTemplate(), isAuthorized());
this.favouriteOperations = new FavoriteTemplate(objectMapper,
getRestTemplate(), isAuthorized());
this.trendOperations = new TrendTemplate(objectMapper,
getRestTemplate(), isAuthorized());
}

@Override
Expand Down Expand Up @@ -163,4 +168,9 @@ public FavoriteOperations favoriteOperations() {
return favouriteOperations;
}

@Override
public TrendOperations trendOperations() {
return trendOperations;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* Copyright 2011 France Telecom R&D Beijing Co., Ltd 北京法国电信研发中心有限公司
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http:https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.social.weibo.api.impl.json;

import org.codehaus.jackson.annotate.JsonIgnoreProperties;
import org.codehaus.jackson.annotate.JsonProperty;

/**
* Annotated mixin to add Jackson annotations to UserTrend.
*
* @author edva8332
*/
@JsonIgnoreProperties(ignoreUnknown = true)
abstract class UserTrendMixin {

String num;
String hotword;
@JsonProperty("trend_id")
long id;

}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import org.springframework.social.weibo.api.Favorite.Tag;
import org.springframework.social.weibo.api.RateLimitStatus;
import org.springframework.social.weibo.api.Status;
import org.springframework.social.weibo.api.UserTrend;
import org.springframework.social.weibo.api.WeiboProfile;
import org.springframework.social.weibo.api.impl.json.FavoriteMixin.TagMixin;

Expand All @@ -42,6 +43,7 @@ public void setupModule(SetupContext context) {
RateLimitStatusMixin.class);
context.setMixInAnnotations(Favorite.class, FavoriteMixin.class);
context.setMixInAnnotations(Tag.class, TagMixin.class);
context.setMixInAnnotations(UserTrend.class, UserTrendMixin.class);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/*
* Copyright 2011 France Telecom R&D Beijing Co., Ltd 北京法国电信研发中心有限公司
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http:https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.social.weibo.api.impl;

import static org.junit.Assert.assertEquals;
import static org.springframework.http.HttpMethod.GET;
import static org.springframework.social.test.client.RequestMatchers.header;
import static org.springframework.social.test.client.RequestMatchers.method;
import static org.springframework.social.test.client.RequestMatchers.requestTo;
import static org.springframework.social.test.client.ResponseCreators.withResponse;

import java.util.List;

import org.junit.Test;
import org.springframework.social.weibo.api.UserTrend;

public class TrendTemplateTest extends AbstractWeiboOperationsTest {

private TrendTemplate trendTemplate;

@Test
public void testGetTrendsPagination() {
mockServer
.expect(requestTo("https://api.weibo.com/2/trends.json?uid=1&count=20&page=2"))
.andExpect(method(GET))
.andExpect(header("Authorization", "OAuth2 accessToken"))
.andRespond(
withResponse(jsonResource("userTrends"),
responseHeaders));
List<UserTrend> trends = trendTemplate.getTrends(1, 20, 2);
assertEquals(2, trends.size());
UserTrend firstTrend = trends.iterator().next();
verifyUserTrend(firstTrend);
}

@Test
public void testGetTrends() {
mockServer
.expect(requestTo("https://api.weibo.com/2/trends.json?uid=1"))
.andExpect(method(GET))
.andExpect(header("Authorization", "OAuth2 accessToken"))
.andRespond(
withResponse(jsonResource("userTrends"),
responseHeaders));
List<UserTrend> trends = trendTemplate.getTrends(1);
assertEquals(2, trends.size());
UserTrend firstTrend = trends.iterator().next();
verifyUserTrend(firstTrend);
}

private void verifyUserTrend(UserTrend userTrend) {
assertEquals(1567898, userTrend.getId());
assertEquals("苹果", userTrend.getHotword());
assertEquals("225673", userTrend.getNum());
}

@Override
public void setUp() {
trendTemplate = new TrendTemplate(getObjectMapper(), getRestTemplate(),
true);
}

}
10 changes: 10 additions & 0 deletions src/test/resources/json/userTrends.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[{
"num" : 225673,
"hotword" : "苹果",
"trend_id" : 1567898
}, {
"num" : 225674,
"hotword" : "Apple",
"trend_id" : 1567899
}
]

0 comments on commit d121734

Please sign in to comment.