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

add getPhotoWithAllFields(photoId) method to MediaOperations #222

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,16 @@ public interface MediaOperations {
* @throws InsufficientPermissionException if the photo is not public and if the user has not granted "user_photos" permission.
*/
Photo getPhoto(String photoId);


/**
* Retrieve data for a specified photo - with user defined requested fields
* Requires "user_photos" permission if the photo is not public.
* @param photoId the photo's ID
* @return the requested {@link Photo}
* @throws ApiException if there is an error while communicating with Facebook.
* @throws InsufficientPermissionException if the photo is not public and if the user has not granted "user_photos" permission.
*/
Photo getPhotoWithAllFields(String photoId);
/**
* Retrieves a photo's image as an array of bytes. Returns the image in Facebook's "normal" type.
* Requires "user_photos" permission if the photo is not public.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,150 +34,154 @@

class MediaTemplate implements MediaOperations {

private final GraphApi graphApi;

private final RestTemplate restTemplate;

public MediaTemplate(GraphApi graphApi, RestTemplate restTemplate) {
this.graphApi = graphApi;
this.restTemplate = restTemplate;
}

public PagedList<Album> getAlbums() {
return getAlbums("me", new PagingParameters(25, 0, null, null));
}

public PagedList<Album> getAlbums(PagingParameters pagedListParameters) {
return getAlbums("me", pagedListParameters);
}

public PagedList<Album> getAlbums(String userId) {
return getAlbums(userId, new PagingParameters(25, 0, null, null));
}

public PagedList<Album> getAlbums(String userId, PagingParameters pagedListParameters) {
return graphApi.fetchConnections(userId, "albums", Album.class, getPagingParameters(pagedListParameters));
}

public Album getAlbum(String albumId) {
return graphApi.fetchObject(albumId, Album.class);
}

public String createAlbum(String name, String description) {
return createAlbum("me", name, description);
}

public String createAlbum(String ownerId, String name, String description) {
MultiValueMap<String, Object> data = new LinkedMultiValueMap<String, Object>();
data.set("name", name);
data.set("message", description);
return graphApi.publish(ownerId, "albums", data);
}

public byte[] getAlbumImage(String albumId) {
return getAlbumImage(albumId, ImageType.ALBUM);
}

public byte[] getAlbumImage(String albumId, ImageType imageType) {
return graphApi.fetchImage(albumId, "picture", imageType);
}

public PagedList<Photo> getPhotos(String objectId) {
return getPhotos(objectId, new PagingParameters(25, 0, null, null));
}

public PagedList<Photo> getPhotos(String objectId, PagingParameters pagedListParameters) {
return graphApi.fetchConnections(objectId, "photos", Photo.class, getPagingParameters(pagedListParameters), ALL_PHOTO_FIELDS);
}

public Photo getPhoto(String photoId) {
return graphApi.fetchObject(photoId, Photo.class);
}

public byte[] getPhotoImage(String photoId) {
return getPhotoImage(photoId, ImageType.NORMAL);
}

public byte[] getPhotoImage(String photoId, ImageType imageType) {
return graphApi.fetchImage(photoId, "picture", imageType);
}

public String postPhoto(Resource photo) {
MultiValueMap<String, Object> parts = new LinkedMultiValueMap<String, Object>();
parts.set("source", photo);
return graphApi.publish("me", "photos", parts);
}

public String postPhoto(Resource photo, String caption) {
MultiValueMap<String, Object> parts = new LinkedMultiValueMap<String, Object>();
parts.set("source", photo);
parts.set("message", caption);
return graphApi.publish("me", "photos", parts);
}

public String postPhoto(String albumId, Resource photo) {
MultiValueMap<String, Object> parts = new LinkedMultiValueMap<String, Object>();
parts.set("source", photo);
return graphApi.publish(albumId, "photos", parts);
}

public String postPhoto(String albumId, Resource photo, String caption) {
MultiValueMap<String, Object> parts = new LinkedMultiValueMap<String, Object>();
parts.set("source", photo);
parts.set("message", caption);
return graphApi.publish(albumId, "photos", parts);
}

public PagedList<Video> getVideos() {
return getVideos("me", new PagingParameters(25, 0, null, null));
}

public PagedList<Video> getVideos(PagingParameters pagedListParameters) {
return getVideos("me", pagedListParameters);
}

public PagedList<Video> getVideos(String userId) {
return getVideos(userId, new PagingParameters(25, 0, null, null));
}

public PagedList<Video> getVideos(String userId, PagingParameters pagedListParameters) {
return graphApi.fetchConnections(userId, "videos", Video.class, getPagingParameters(pagedListParameters));
}

public Video getVideo(String videoId) {
return graphApi.fetchObject(videoId, Video.class);
}

public byte[] getVideoImage(String videoId) {
return graphApi.fetchImage(videoId, "picture", ImageType.SMALL);
}

@SuppressWarnings("unchecked")
public String postVideo(Resource video) {
MultiValueMap<String, Object> parts = new LinkedMultiValueMap<String, Object>();
parts.set("file", video);
Map<String, Object> response = restTemplate.postForObject("https://graph-video.facebook.com/me/videos", parts, Map.class);
return (String) response.get("id");
}

@SuppressWarnings("unchecked")
public String postVideo(Resource video, String title, String description) {
MultiValueMap<String, Object> parts = new LinkedMultiValueMap<String, Object>();
parts.set("file", video);
parts.set("title", title);
parts.set("description", description);
Map<String, Object> response = restTemplate.postForObject("https://graph-video.facebook.com/me/videos", parts, Map.class);
return (String) response.get("id");
}

public void tagVideo(String videoId, String userId) {
MultiValueMap<String, Object> data = new LinkedMultiValueMap<String, Object>();
data.add("tag_uid", userId);
graphApi.publish(videoId, "tags", data);
}

static final String[] ALL_PHOTO_FIELDS = {
"id", "album", "backdated_time", "backdated_time_granularity", "created_time", "from", "height", "picture",
"source", "link", "icon", "images", "name", "page_story_id", "place,updated_time", "tags"
};
private final GraphApi graphApi;

private final RestTemplate restTemplate;

public MediaTemplate(GraphApi graphApi, RestTemplate restTemplate) {
this.graphApi = graphApi;
this.restTemplate = restTemplate;
}

public PagedList<Album> getAlbums() {
return getAlbums("me", new PagingParameters(25, 0, null, null));
}

public PagedList<Album> getAlbums(PagingParameters pagedListParameters) {
return getAlbums("me", pagedListParameters);
}

public PagedList<Album> getAlbums(String userId) {
return getAlbums(userId, new PagingParameters(25, 0, null, null));
}

public PagedList<Album> getAlbums(String userId, PagingParameters pagedListParameters) {
return graphApi.fetchConnections(userId, "albums", Album.class, getPagingParameters(pagedListParameters));
}

public Album getAlbum(String albumId) {
return graphApi.fetchObject(albumId, Album.class);
}

public String createAlbum(String name, String description) {
return createAlbum("me", name, description);
}

public String createAlbum(String ownerId, String name, String description) {
MultiValueMap<String, Object> data = new LinkedMultiValueMap<String, Object>();
data.set("name", name);
data.set("message", description);
return graphApi.publish(ownerId, "albums", data);
}

public byte[] getAlbumImage(String albumId) {
return getAlbumImage(albumId, ImageType.ALBUM);
}

public byte[] getAlbumImage(String albumId, ImageType imageType) {
return graphApi.fetchImage(albumId, "picture", imageType);
}

public PagedList<Photo> getPhotos(String objectId) {
return getPhotos(objectId, new PagingParameters(25, 0, null, null));
}

public PagedList<Photo> getPhotos(String objectId, PagingParameters pagedListParameters) {
return graphApi.fetchConnections(objectId, "photos", Photo.class, getPagingParameters(pagedListParameters), ALL_PHOTO_FIELDS);
}

public Photo getPhoto(String photoId) {
return graphApi.fetchObject(photoId, Photo.class);
}

public Photo getPhotoWithAllFields(String photoId) {
return graphApi.fetchObject(photoId, Photo.class, ALL_PHOTO_FIELDS);
}

public byte[] getPhotoImage(String photoId) {
return getPhotoImage(photoId, ImageType.NORMAL);
}

public byte[] getPhotoImage(String photoId, ImageType imageType) {
return graphApi.fetchImage(photoId, "picture", imageType);
}

public String postPhoto(Resource photo) {
MultiValueMap<String, Object> parts = new LinkedMultiValueMap<String, Object>();
parts.set("source", photo);
return graphApi.publish("me", "photos", parts);
}

public String postPhoto(Resource photo, String caption) {
MultiValueMap<String, Object> parts = new LinkedMultiValueMap<String, Object>();
parts.set("source", photo);
parts.set("message", caption);
return graphApi.publish("me", "photos", parts);
}

public String postPhoto(String albumId, Resource photo) {
MultiValueMap<String, Object> parts = new LinkedMultiValueMap<String, Object>();
parts.set("source", photo);
return graphApi.publish(albumId, "photos", parts);
}

public String postPhoto(String albumId, Resource photo, String caption) {
MultiValueMap<String, Object> parts = new LinkedMultiValueMap<String, Object>();
parts.set("source", photo);
parts.set("message", caption);
return graphApi.publish(albumId, "photos", parts);
}

public PagedList<Video> getVideos() {
return getVideos("me", new PagingParameters(25, 0, null, null));
}

public PagedList<Video> getVideos(PagingParameters pagedListParameters) {
return getVideos("me", pagedListParameters);
}

public PagedList<Video> getVideos(String userId) {
return getVideos(userId, new PagingParameters(25, 0, null, null));
}

public PagedList<Video> getVideos(String userId, PagingParameters pagedListParameters) {
return graphApi.fetchConnections(userId, "videos", Video.class, getPagingParameters(pagedListParameters));
}

public Video getVideo(String videoId) {
return graphApi.fetchObject(videoId, Video.class);
}

public byte[] getVideoImage(String videoId) {
return graphApi.fetchImage(videoId, "picture", ImageType.SMALL);
}

@SuppressWarnings("unchecked")
public String postVideo(Resource video) {
MultiValueMap<String, Object> parts = new LinkedMultiValueMap<String, Object>();
parts.set("file", video);
Map<String, Object> response = restTemplate.postForObject("https://graph-video.facebook.com/me/videos", parts, Map.class);
return (String) response.get("id");
}

@SuppressWarnings("unchecked")
public String postVideo(Resource video, String title, String description) {
MultiValueMap<String, Object> parts = new LinkedMultiValueMap<String, Object>();
parts.set("file", video);
parts.set("title", title);
parts.set("description", description);
Map<String, Object> response = restTemplate.postForObject("https://graph-video.facebook.com/me/videos", parts, Map.class);
return (String) response.get("id");
}

public void tagVideo(String videoId, String userId) {
MultiValueMap<String, Object> data = new LinkedMultiValueMap<String, Object>();
data.add("tag_uid", userId);
graphApi.publish(videoId, "tags", data);
}

static final String[] ALL_PHOTO_FIELDS = {
"id", "album", "backdated_time", "backdated_time_granularity", "created_time", "from", "height", "picture",
"source", "link", "icon", "images", "name", "page_story_id", "place,updated_time", "tags"
};
}