Skip to content

Commit

Permalink
IUsersOperations.getUsers is using POST request now (this fixes probl…
Browse files Browse the repository at this point in the history
…em when lots of users are requested)
  • Loading branch information
disovi committed Jul 31, 2015
1 parent a7c25e8 commit 4fd0b5c
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 16 deletions.
Expand Up @@ -22,7 +22,7 @@
* @author vkolodrevskiy
*/
public interface IUsersOperations {
public final static String DEFAULT_FIELDS = "first_name,last_name,photo_50,photo_100,photo_200,contacts,bdate,sex,screen_name";
String DEFAULT_FIELDS = "first_name,last_name,photo_50,photo_100,photo_200,contacts,bdate,sex,screen_name";
/**
* Retrieves the profile for the authenticated user.
* @return the user's profile information.
Expand Down
Expand Up @@ -26,6 +26,7 @@
import org.springframework.social.vkontakte.api.VKontakteErrorException;
import org.springframework.social.vkontakte.api.impl.json.VKArray;
import org.springframework.util.Assert;
import org.springframework.util.MultiValueMap;

import java.net.URI;
import java.util.ArrayList;
Expand Down Expand Up @@ -74,6 +75,13 @@ protected URI makeOperationURL(String method, Properties params, ApiVersion apiV
return uri.build();
}

protected URI makeOperationPOST(String method, MultiValueMap<String, Object> data, ApiVersion apiVersion) {
URIBuilder uri = URIBuilder.fromUri(VK_REST_URL + method);
data.set("access_token", accessToken);
data.set("v", apiVersion.toString());
return uri.build();
}

protected void preProcessURI(URIBuilder uri) {
// add access_token
// TODO: for some methods we do not need access token, so think about it.
Expand Down
Expand Up @@ -20,6 +20,8 @@
import org.springframework.social.vkontakte.api.IUsersOperations;
import org.springframework.social.vkontakte.api.VKontakteProfile;
import org.springframework.social.vkontakte.api.VKontakteProfiles;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;

import java.net.URI;
Expand All @@ -41,7 +43,7 @@ public UsersTemplate(RestTemplate restTemplate, String accessToken, ObjectMapper

public List<VKontakteProfile> getUsers(List<String> userIds, String fields) {
requireAuthorization();
Properties props = new Properties();
MultiValueMap<String, Object> data = new LinkedMultiValueMap<String, Object>();

StringBuilder uids = new StringBuilder();
if(userIds != null) {
Expand All @@ -50,15 +52,13 @@ public List<VKontakteProfile> getUsers(List<String> userIds, String fields) {
uids.append(uid);
else uids.append(",").append(uid);
}
props.put("user_ids", uids.toString());
data.set("user_ids", uids.toString());
}

props.put("fields", fields != null? fields: IUsersOperations.DEFAULT_FIELDS);
data.set("fields", fields != null? fields: IUsersOperations.DEFAULT_FIELDS);

// see documentation under http://vk.com/dev/users.get
URI uri = makeOperationURL("users.get", props, ApiVersion.VERSION_5_27);

VKontakteProfiles profiles = restTemplate.getForObject(uri, VKontakteProfiles.class);
URI uri = makeOperationPOST("users.get", data, ApiVersion.VERSION_5_27);
VKontakteProfiles profiles = restTemplate.postForObject(uri, data, VKontakteProfiles.class);
checkForError(profiles);

return profiles.getProfiles();
Expand Down
Expand Up @@ -26,6 +26,7 @@

import static org.junit.Assert.assertEquals;
import static org.springframework.http.HttpMethod.GET;
import static org.springframework.http.HttpMethod.POST;
import static org.springframework.http.MediaType.APPLICATION_JSON;
import static org.springframework.test.web.client.match.MockRestRequestMatchers.method;
import static org.springframework.test.web.client.match.MockRestRequestMatchers.requestTo;
Expand All @@ -40,8 +41,8 @@ public class UsersTemplateTest extends AbstractVKontakteApiTest {
@Test
public void getUser_currentUser() {
mockServer
.expect(requestTo("https://api.vk.com/method/users.get?access_token=ACCESS_TOKEN&v=5.27&fields=sex%2C+bdate%2C+city%2C+country%2C+photo_50%2C+photo_100%2C+photo_200_orig%2C+photo_200%2C+photo_400_orig%2C+photo_max%2C+photo_max_orig%2C+photo_id%2C+online%2C+online_mobile%2C+domain%2C+has_mobile%2C+contacts%2C+connections%2C+site%2C+education%2C+universities%2C+schools%2C+can_post%2C+can_see_all_posts%2C+can_see_audio%2C+can_write_private_message%2C+status%2C+last_seen%2C+common_count%2C+relation%2C+relatives%2C+counters%2C+screen_name%2C+maiden_name%2C+timezone%2C+occupation%2Cactivities%2C+interests%2C+music%2C+movies%2C+tv%2C+books%2C+games%2C+about%2C+quotes%2C+personal%2C+friends_status"))
.andExpect(method(GET)).andRespond(withSuccess(jsonResource("list-of-profiles-5_27"), APPLICATION_JSON));
.expect(requestTo("https://api.vk.com/method/users.get"))
.andExpect(method(POST)).andRespond(withSuccess(jsonResource("list-of-profiles-5_27"), APPLICATION_JSON));

VKontakteProfile profile = vkontakte.usersOperations().getUser("sex, bdate, city, country, photo_50, photo_100, photo_200_orig, photo_200, photo_400_orig, photo_max, photo_max_orig, photo_id, online, online_mobile, domain, has_mobile, contacts, connections, site, education, universities, schools, can_post, can_see_all_posts, can_see_audio, can_write_private_message, status, last_seen, common_count, relation, relatives, counters, screen_name, maiden_name, timezone, occupation,activities, interests, music, movies, tv, books, games, about, quotes, personal, friends_status");

Expand All @@ -54,8 +55,8 @@ public void getUser_currentUser() {
@Test
public void getUsers_currentUser() {
mockServer
.expect(requestTo("https://api.vk.com/method/users.get?access_token=ACCESS_TOKEN&v=5.27&fields=first_name%2Clast_name%2Cphoto_50%2Cphoto_100%2Cphoto_200%2Ccontacts%2Cbdate%2Csex%2Cscreen_name&user_ids=durov%2C2183%2C77478"))
.andExpect(method(GET)).andRespond(withSuccess(jsonResource("list-of-profiles-5_27"), APPLICATION_JSON));
.expect(requestTo("https://api.vk.com/method/users.get"))
.andExpect(method(POST)).andRespond(withSuccess(jsonResource("list-of-profiles-5_27"), APPLICATION_JSON));
String[] userIds = {"durov", "2183", "77478"};
List<VKontakteProfile> profiles = vkontakte.usersOperations().getUsers(Arrays.asList(userIds));

Expand All @@ -76,17 +77,17 @@ public void getUsers_unauthorized() {
@Test(expected = VKontakteErrorException.class)
public void getUser_expiredToken() {
mockServer
.expect(requestTo("https://api.vk.com/method/users.get?access_token=ACCESS_TOKEN&v=5.27&fields=first_name%2Clast_name%2Cphoto_50%2Cphoto_100%2Cphoto_200%2Ccontacts%2Cbdate%2Csex%2Cscreen_name"))
.andExpect(method(GET)).andRespond(withSuccess(jsonResource("error-code-5"), APPLICATION_JSON));
.expect(requestTo("https://api.vk.com/method/users.get"))
.andExpect(method(POST)).andRespond(withSuccess(jsonResource("error-code-5"), APPLICATION_JSON));

vkontakte.usersOperations().getUser();
}

@Test(expected = VKontakteErrorException.class)
public void getUsers_expiredToken() {
mockServer
.expect(requestTo("https://api.vk.com/method/users.get?access_token=ACCESS_TOKEN&v=5.27&fields=first_name%2Clast_name%2Cphoto_50%2Cphoto_100%2Cphoto_200%2Ccontacts%2Cbdate%2Csex%2Cscreen_name&user_ids=1%2C2"))
.andExpect(method(GET)).andRespond(withSuccess(jsonResource("error-code-5"), APPLICATION_JSON));
.expect(requestTo("https://api.vk.com/method/users.get"))
.andExpect(method(POST)).andRespond(withSuccess(jsonResource("error-code-5"), APPLICATION_JSON));
String[] userIds = {"1", "2"};
vkontakte.usersOperations().getUsers(Arrays.asList(userIds));
}
Expand Down

0 comments on commit 4fd0b5c

Please sign in to comment.