Skip to content

Commit ce99a03

Browse files
committed
Allow specifying request context when requesting Media.
This resolves #45 To ensure backward compatibility, a new `getMedia` with an additional nullable String parameter is added, that the existing getMedia now call. (`getMedia(:id, null)`)
1 parent 0d11bea commit ce99a03

File tree

3 files changed

+22
-4
lines changed

3 files changed

+22
-4
lines changed

src/main/java/com/afrozaar/wordpress/wpapi/v2/Client.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,13 @@ public List<Media> getMedia() {
283283

284284
@Override
285285
public Media getMedia(Long id) {
286-
return CustomRenderableParser.parse(doExchange1(Request.MEDIA, HttpMethod.GET, String.class, forExpand(id), ImmutableMap.of(CONTEXT_, Contexts.EDIT), null), Media.class);
286+
return getMedia(id, null);
287+
}
288+
289+
@Override
290+
public Media getMedia(Long id, @Nullable String context) {
291+
final ImmutableMap<String, Object> queryParams = ImmutableMap.of(CONTEXT_, ofNullable(context).orElse(Contexts.EDIT));
292+
return CustomRenderableParser.parse(doExchange1(Request.MEDIA, HttpMethod.GET, String.class, forExpand(id), queryParams, null), Media.class);
287293
}
288294

289295
@Override

src/main/java/com/afrozaar/wordpress/wpapi/v2/api/Medias.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77
import org.springframework.core.io.Resource;
88

9+
import javax.annotation.Nullable;
10+
911
import java.util.List;
1012

1113
public interface Medias {
@@ -16,6 +18,13 @@ public interface Medias {
1618

1719
Media getMedia(Long id);
1820

21+
/**
22+
* @param id The media's ID.
23+
* @param context One of {@code view}, {@code edit} or {@code embed}.
24+
* If {@code context} is not provided, it will default to {@code edit}
25+
*/
26+
Media getMedia(Long id, @Nullable String context);
27+
1928
Media updateMedia(Media media);
2029

2130
boolean deleteMedia(Media media);

src/test/java/com/afrozaar/wordpress/wpapi/v2/WordpressClientTest.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import static org.junit.Assert.fail;
66

7+
import com.afrozaar.wordpress.wpapi.v2.api.Contexts;
78
import com.afrozaar.wordpress.wpapi.v2.config.ClientConfig;
89
import com.afrozaar.wordpress.wpapi.v2.config.ClientFactory;
910
import com.afrozaar.wordpress.wpapi.v2.exception.WpApiParsedException;
@@ -60,9 +61,6 @@ public void CreateMedia_MustFallOverWhenResourceDoesNotReturnFilename() throws W
6061

6162
@Test
6263
public void CreateMedia_MustNotFallOverWhenResourceReturnsFileName() throws WpApiParsedException {
63-
64-
65-
6664
try {
6765
wordpress.createMedia(MediaBuilder.aMedia().build(), new FilenameWrapperByteArrayResource(new byte[0], "myfile.png"));
6866
} catch (RuntimeException e) {
@@ -72,4 +70,9 @@ public void CreateMedia_MustNotFallOverWhenResourceReturnsFileName() throws WpAp
7270
}
7371
}
7472

73+
@Test
74+
public void GetMediaWithViewContext_MustReturnMediaWithAvailableFieldsPopulated() {
75+
System.out.println("media = " + wordpress.getMedia(1033L, Contexts.VIEW));
76+
}
77+
7578
}

0 commit comments

Comments
 (0)