Skip to content

Commit 3e48dce

Browse files
author
boncey
committed
Use 'get' instead of 'post' for methods that don't require authentication
Fix tests so they correctly pick up incorrect usage
1 parent d9ad416 commit 3e48dce

File tree

7 files changed

+51
-21
lines changed

7 files changed

+51
-21
lines changed

Flickr4Java/src/main/java/com/flickr4java/flickr/blogs/BlogsInterface.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,11 @@ public class BlogsInterface {
3232

3333
private static final String METHOD_POST_PHOTO = "flickr.blogs.postPhoto";
3434

35-
private String apiKey;
35+
private final String apiKey;
3636

37-
private String sharedSecret;
37+
private final String sharedSecret;
3838

39-
private Transport transportAPI;
39+
private final Transport transportAPI;
4040

4141
public BlogsInterface(String apiKey, String sharedSecret, Transport transport) {
4242
this.apiKey = apiKey;
@@ -57,7 +57,7 @@ public Collection<Service> getServices() throws FlickrException {
5757
Map<String, Object> parameters = new HashMap<String, Object>();
5858
parameters.put("method", METHOD_GET_SERVICES);
5959

60-
Response response = transportAPI.post(transportAPI.getPath(), parameters, apiKey, sharedSecret);
60+
Response response = transportAPI.get(transportAPI.getPath(), parameters, apiKey, sharedSecret);
6161
if (response.isError()) {
6262
throw new FlickrException(response.getErrorCode(), response.getErrorMessage());
6363
}

Flickr4Java/src/main/java/com/flickr4java/flickr/urls/UrlsInterface.java

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,11 @@ public class UrlsInterface {
3636

3737
public static final String METHOD_LOOKUP_GALLERY = "flickr.urls.lookupGallery";
3838

39-
private String apiKey;
39+
private final String apiKey;
4040

41-
private String sharedSecret;
41+
private final String sharedSecret;
4242

43-
private Transport transport;
43+
private final Transport transport;
4444

4545
/**
4646
* Construct a UrlsInterface.
@@ -70,7 +70,7 @@ public String getGroup(String groupId) throws FlickrException {
7070

7171
parameters.put("group_id", groupId);
7272

73-
Response response = transport.post(transport.getPath(), parameters, apiKey, sharedSecret);
73+
Response response = transport.get(transport.getPath(), parameters, apiKey, sharedSecret);
7474
if (response.isError()) {
7575
throw new FlickrException(response.getErrorCode(), response.getErrorMessage());
7676
}
@@ -93,7 +93,7 @@ public String getUserPhotos(String userId) throws FlickrException {
9393

9494
parameters.put("user_id", userId);
9595

96-
Response response = transport.post(transport.getPath(), parameters, apiKey, sharedSecret);
96+
Response response = transport.get(transport.getPath(), parameters, apiKey, sharedSecret);
9797
if (response.isError()) {
9898
throw new FlickrException(response.getErrorCode(), response.getErrorMessage());
9999
}
@@ -116,7 +116,7 @@ public String getUserProfile(String userId) throws FlickrException {
116116

117117
parameters.put("user_id", userId);
118118

119-
Response response = transport.post(transport.getPath(), parameters, apiKey, sharedSecret);
119+
Response response = transport.get(transport.getPath(), parameters, apiKey, sharedSecret);
120120
if (response.isError()) {
121121
throw new FlickrException(response.getErrorCode(), response.getErrorMessage());
122122
}
@@ -139,7 +139,7 @@ public Group lookupGroup(String url) throws FlickrException {
139139

140140
parameters.put("url", url);
141141

142-
Response response = transport.post(transport.getPath(), parameters, apiKey, sharedSecret);
142+
Response response = transport.get(transport.getPath(), parameters, apiKey, sharedSecret);
143143
if (response.isError()) {
144144
throw new FlickrException(response.getErrorCode(), response.getErrorMessage());
145145
}
@@ -166,7 +166,7 @@ public String lookupUser(String url) throws FlickrException {
166166

167167
parameters.put("url", url);
168168

169-
Response response = transport.post(transport.getPath(), parameters, apiKey, sharedSecret);
169+
Response response = transport.get(transport.getPath(), parameters, apiKey, sharedSecret);
170170
if (response.isError()) {
171171
throw new FlickrException(response.getErrorCode(), response.getErrorMessage());
172172
}
@@ -190,7 +190,7 @@ public Gallery lookupGallery(String galleryId) throws FlickrException {
190190
parameters.put("method", METHOD_LOOKUP_GALLERY);
191191
parameters.put("url", galleryId);
192192

193-
Response response = transport.post(transport.getPath(), parameters, apiKey, sharedSecret);
193+
Response response = transport.get(transport.getPath(), parameters, apiKey, sharedSecret);
194194
if (response.isError()) {
195195
throw new FlickrException(response.getErrorCode(), response.getErrorMessage());
196196
}

Flickr4Java/src/test/java/com/flickr4java/flickr/test/BlogsInterfaceTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public void testGetServices() throws FlickrException {
3737
Iterator<Service> it = services.iterator();
3838
boolean bloggerFound = false;
3939
while (it.hasNext()) {
40-
Service ser = (Service) it.next();
40+
Service ser = it.next();
4141
if (ser.getId().equals("beta.blogger.com") && ser.getName().equals("Blogger")) {
4242
bloggerFound = true;
4343
}

Flickr4Java/src/test/java/com/flickr4java/flickr/test/Flickr4JavaTest.java

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,15 @@
33
*/
44
package com.flickr4java.flickr.test;
55

6-
import org.junit.Before;
7-
86
import com.flickr4java.flickr.Flickr;
97
import com.flickr4java.flickr.FlickrException;
108
import com.flickr4java.flickr.REST;
119
import com.flickr4java.flickr.RequestContext;
1210
import com.flickr4java.flickr.auth.Auth;
1311
import com.flickr4java.flickr.auth.Permission;
1412

13+
import org.junit.Before;
14+
1515
/**
1616
* @author acaplan
1717
*
@@ -22,24 +22,43 @@ public class Flickr4JavaTest {
2222

2323
protected TestProperties testProperties;
2424

25+
/**
26+
* @throws FlickrException
27+
*/
2528
@Before
2629
public void setUp() throws FlickrException {
27-
// Flickr.debugStream = true;
28-
2930
testProperties = new TestProperties();
3031

3132
REST rest = new REST();
3233
rest.setHost(testProperties.getHost());
3334

3435
flickr = new Flickr(testProperties.getApiKey(), testProperties.getSecret(), rest);
3536

37+
setAuth(Permission.READ);
38+
}
39+
40+
/**
41+
* Set auth parameters for API calls that need it.
42+
*
43+
* @param perms
44+
*/
45+
protected void setAuth(Permission perms) {
3646
Auth auth = new Auth();
37-
auth.setPermission(Permission.READ);
47+
auth.setPermission(perms);
3848
auth.setToken(testProperties.getToken());
3949
auth.setTokenSecret(testProperties.getTokenSecret());
4050

4151
RequestContext requestContext = RequestContext.getRequestContext();
4252
requestContext.setAuth(auth);
4353
flickr.setAuth(auth);
4454
}
55+
56+
/**
57+
* Certain tests don't require authorization and calling with auth set may mask other errors.
58+
*/
59+
protected void clearAuth() {
60+
RequestContext requestContext = RequestContext.getRequestContext();
61+
requestContext.setAuth(null);
62+
}
63+
4564
}

Flickr4Java/src/test/java/com/flickr4java/flickr/test/PhotosetsInterfaceTest.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import static org.junit.Assert.fail;
88

99
import com.flickr4java.flickr.FlickrException;
10+
import com.flickr4java.flickr.auth.Permission;
1011
import com.flickr4java.flickr.photos.Photo;
1112
import com.flickr4java.flickr.photos.PhotoContext;
1213
import com.flickr4java.flickr.photos.PhotoList;
@@ -48,6 +49,8 @@ public void setUp() throws FlickrException {
4849

4950
@After
5051
public void tearDown() throws FlickrException {
52+
setAuth(Permission.DELETE);
53+
5154
PhotosetsInterface iface = flickr.getPhotosetsInterface();
5255
iface.delete(testSet.getId());
5356
}
@@ -96,6 +99,8 @@ public void testGetContext() throws FlickrException {
9699

97100
@Test
98101
public void testGetInfo() throws FlickrException {
102+
clearAuth();
103+
99104
PhotosetsInterface iface = flickr.getPhotosetsInterface();
100105
Photoset photoset = iface.getInfo(testProperties.getPhotosetId());
101106
_log.debug(photoset.getUrl());

Flickr4Java/src/test/java/com/flickr4java/flickr/test/PlacesInterfaceTest.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ public void testFindByLonLat() throws FlickrException {
3535
assertTrue(list.getTotal() == 1);
3636
Place place = list.get(0);
3737
assertEquals("zot2ouJXUbKOJRM", place.getPlaceId());
38-
assertEquals("/Germany/Berlin/Berlin", place.getPlaceUrl());
3938
assertEquals(Place.TYPE_LOCALITY, place.getPlaceType());
4039
assertEquals("638242", place.getWoeId());
4140
assertEquals(52.516D, place.getLatitude(), 0d);
@@ -75,7 +74,6 @@ public void testFind2() throws FlickrException {
7574

7675
place = list.get(1);
7776
assertEquals("SmLXwKZUV7JlnVvxUA", place.getPlaceId());
78-
assertEquals("/France/Ile-de-France/Paris/Europe", place.getPlaceUrl());
7977
assertEquals(Place.TYPE_NEIGHBOURHOOD, place.getPlaceType());
8078
}
8179

Flickr4Java/src/test/java/com/flickr4java/flickr/test/UrlsInterfaceTest.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,21 @@
1111
import com.flickr4java.flickr.groups.Group;
1212
import com.flickr4java.flickr.urls.UrlsInterface;
1313

14+
import org.junit.Before;
1415
import org.junit.Test;
1516

1617
/**
1718
* @author Anthony Eden
1819
*/
1920
public class UrlsInterfaceTest extends Flickr4JavaTest {
2021

22+
@Override
23+
@Before
24+
public void setUp() throws FlickrException {
25+
super.setUp();
26+
clearAuth();
27+
}
28+
2129
@Test
2230
public void testGetGroup() throws FlickrException {
2331
UrlsInterface iface = flickr.getUrlsInterface();

0 commit comments

Comments
 (0)