Class PhotosLibraryClient
- java.lang.Object
-
- com.google.photos.library.v1.internal.InternalPhotosLibraryClient
-
- com.google.photos.library.v1.PhotosLibraryClient
-
- All Implemented Interfaces:
com.google.api.gax.core.BackgroundResource
,java.lang.AutoCloseable
public final class PhotosLibraryClient extends InternalPhotosLibraryClient
An wrapper class ofInternalPhotosLibraryClient
.Client to interact with the Google Photos Library API. You can perform the following actions on behalf of the user:
- upload media items directly to their Google Photos library
- create albums
- add media items (including album enrichments) to albums
- list and download content from their Google Photos library
- filter results by media type, date range or content category
- create, join, and access shared albums
This class provides the ability to make remote calls to the backing service through method calls that map to API methods. Sample code to get started:
PhotosLibrarySettings photosLibrarySettings = PhotosLibrarySettings.newBuilder() .setCredentialsProvider(FixedCredentialsProvider.create(myCredentials)) .build(); try (PhotosLibraryClient photosLibraryClient = PhotosLibraryClient.initialize(photosLibrarySettings)) { Album createdAlbum = photosLibraryClient.createAlbum("My Album"); }
Note: close() needs to be called on the photosLibraryClient object to clean up resources such as threads. In the example above, try-with-resources is used, which automatically calls close().
The surface of this class includes several types of Java methods for each of the API's methods:
- A "flattened" method. With this type of method, the fields of the request type have been converted into function parameters. It may be the case that not all fields are available as parameters, and not every API method will have a flattened method entry point.
- A "request object" method. This type of method only takes one parameter, a request object, which must be constructed before the call. Not every API method will have a request object method.
- A "callable" method. This type of method takes no parameters and returns an immutable API callable object, which can be used to initiate calls to the service.
Many parameters require resource names to be formatted in a particular way. To assist with these names, this class includes a format method for each type of name, and additionally a parse method to extract the individual identifiers contained within names that are returned.
Do not use
InternalPhotosLibraryClient.create()
orInternalPhotosLibraryClient.create(InternalPhotosLibrarySettings)
for this wrapper.
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from class com.google.photos.library.v1.internal.InternalPhotosLibraryClient
InternalPhotosLibraryClient.ListAlbumsFixedSizeCollection, InternalPhotosLibraryClient.ListAlbumsPage, InternalPhotosLibraryClient.ListAlbumsPagedResponse, InternalPhotosLibraryClient.ListMediaItemsFixedSizeCollection, InternalPhotosLibraryClient.ListMediaItemsPage, InternalPhotosLibraryClient.ListMediaItemsPagedResponse, InternalPhotosLibraryClient.ListSharedAlbumsFixedSizeCollection, InternalPhotosLibraryClient.ListSharedAlbumsPage, InternalPhotosLibraryClient.ListSharedAlbumsPagedResponse, InternalPhotosLibraryClient.SearchMediaItemsFixedSizeCollection, InternalPhotosLibraryClient.SearchMediaItemsPage, InternalPhotosLibraryClient.SearchMediaItemsPagedResponse
-
-
Constructor Summary
Constructors Modifier Constructor Description protected
PhotosLibraryClient(PhotosLibraryStub stub, PhotosLibraryUploadStub uploadStub)
protected
PhotosLibraryClient(PhotosLibrarySettings settings)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description BatchCreateMediaItemsResponse
batchCreateMediaItems(java.lang.String albumId, java.util.List<NewMediaItem> newMediaItems)
Creates one or more media items in a user's Google Photos library and adds them to an album.BatchCreateMediaItemsResponse
batchCreateMediaItems(java.util.List<NewMediaItem> newMediaItems)
Creates one or more media items in a user's Google Photos library.void
close()
Album
createAlbum(java.lang.String albumTitle)
Creates an album in a user's Google Photos library.static PhotosLibraryClient
initialize(PhotosLibrarySettings settings)
Creates anPhotosLibraryClient
instance withPhotosLibrarySettings
.InternalPhotosLibraryClient.ListAlbumsPagedResponse
listAlbums()
Lists all albums shown to a user in the Albums tab of the Google Photos app.InternalPhotosLibraryClient.ListMediaItemsPagedResponse
listMediaItems()
List all media items in a user's Google Photos library.InternalPhotosLibraryClient.ListSharedAlbumsPagedResponse
listSharedAlbums()
Lists all shared albums available in the Sharing tab of the user's Google Photos app.InternalPhotosLibraryClient.SearchMediaItemsPagedResponse
searchMediaItems(Filters filters, OrderBy... orderBy)
Searches for media items in a user's Google Photos library.Album
updateAlbumCoverPhoto(Album album, MediaItem newCoverPhotoMediaItem)
Updates the media item used as the cover photo for an album.Album
updateAlbumCoverPhoto(Album album, java.lang.String newCoverPhotoMediaItemId)
Updates the media item used as the cover photo for an album.Album
updateAlbumTitle(Album album, java.lang.String newTitle)
Updates the title of an album.MediaItem
updateMediaItemDescription(MediaItem mediaItem, java.lang.String newDescription)
Updates the description of a media item.UploadMediaItemResponse
uploadMediaItem(UploadMediaItemRequest request)
Uploads a file to Google Photos.com.google.api.gax.rpc.UnaryCallable<UploadMediaItemRequest,UploadMediaItemResponse>
uploadMediaItemCallable()
Returns a callable to upload a file to Google Photos.-
Methods inherited from class com.google.photos.library.v1.internal.InternalPhotosLibraryClient
addEnrichmentToAlbum, addEnrichmentToAlbum, addEnrichmentToAlbumCallable, awaitTermination, batchAddMediaItemsToAlbum, batchAddMediaItemsToAlbum, batchAddMediaItemsToAlbumCallable, batchCreateMediaItems, batchCreateMediaItems, batchCreateMediaItemsCallable, batchGetMediaItems, batchGetMediaItems, batchGetMediaItemsCallable, batchRemoveMediaItemsFromAlbum, batchRemoveMediaItemsFromAlbum, batchRemoveMediaItemsFromAlbumCallable, create, create, create, createAlbum, createAlbum, createAlbumCallable, getAlbum, getAlbum, getAlbumCallable, getMediaItem, getMediaItem, getMediaItemCallable, getSettings, getSharedAlbum, getSharedAlbum, getSharedAlbumCallable, getStub, isShutdown, isTerminated, joinSharedAlbum, joinSharedAlbum, joinSharedAlbumCallable, leaveSharedAlbum, leaveSharedAlbum, leaveSharedAlbumCallable, listAlbums, listAlbums, listAlbumsCallable, listAlbumsPagedCallable, listMediaItems, listMediaItemsCallable, listMediaItemsPagedCallable, listSharedAlbums, listSharedAlbums, listSharedAlbumsCallable, listSharedAlbumsPagedCallable, searchMediaItems, searchMediaItems, searchMediaItems, searchMediaItems, searchMediaItemsCallable, searchMediaItemsPagedCallable, shareAlbum, shareAlbum, shareAlbumCallable, shutdown, shutdownNow, unshareAlbum, unshareAlbum, unshareAlbumCallable, updateAlbum, updateAlbum, updateAlbum, updateAlbumCallable, updateMediaItem, updateMediaItem, updateMediaItemCallable
-
-
-
-
Constructor Detail
-
PhotosLibraryClient
protected PhotosLibraryClient(PhotosLibrarySettings settings) throws java.io.IOException
- Throws:
java.io.IOException
-
PhotosLibraryClient
protected PhotosLibraryClient(PhotosLibraryStub stub, PhotosLibraryUploadStub uploadStub)
-
-
Method Detail
-
initialize
public static PhotosLibraryClient initialize(PhotosLibrarySettings settings) throws java.io.IOException
Creates anPhotosLibraryClient
instance withPhotosLibrarySettings
.- Throws:
java.io.IOException
-
uploadMediaItemCallable
public final com.google.api.gax.rpc.UnaryCallable<UploadMediaItemRequest,UploadMediaItemResponse> uploadMediaItemCallable()
Returns a callable to upload a file to Google Photos.
-
uploadMediaItem
public final UploadMediaItemResponse uploadMediaItem(UploadMediaItemRequest request)
Uploads a file to Google Photos.
-
batchCreateMediaItems
public final BatchCreateMediaItemsResponse batchCreateMediaItems(java.util.List<NewMediaItem> newMediaItems)
Creates one or more media items in a user's Google Photos library.The items are only added to the library and not to an album.
- Parameters:
newMediaItems
- List of media items to be created.- See Also:
InternalPhotosLibraryClient.batchCreateMediaItems(String, List, AlbumPosition)
-
batchCreateMediaItems
public final BatchCreateMediaItemsResponse batchCreateMediaItems(java.lang.String albumId, java.util.List<NewMediaItem> newMediaItems)
Creates one or more media items in a user's Google Photos library and adds them to an album.- Parameters:
albumId
- Identifier of the album where the media items are added. The media items are also added to the user's library.newMediaItems
- List of media items to be created.- See Also:
InternalPhotosLibraryClient.batchCreateMediaItems(String, List, AlbumPosition)
-
createAlbum
public final Album createAlbum(java.lang.String albumTitle)
Creates an album in a user's Google Photos library.- Parameters:
albumTitle
- Name of the album displayed to the user in their Google Photos account. This string shouldn't be more than 500 characters.- Throws:
com.google.api.gax.rpc.ApiException
- if the remote call failsjava.lang.IllegalArgumentException
- if albumTitle is null or empty.
-
updateAlbumTitle
public final Album updateAlbumTitle(Album album, java.lang.String newTitle)
Updates the title of an album.Only the `id` field of the album is used to identify the album. The album must be created by the developer and owned by the user.
- Parameters:
album
- Required. The [Album][google.photos.types.Album] to update.The album’s `id` field is used to identify the album to be updated.
newTitle
- Required. The new title of the album.- Returns:
- The updated album.
- Throws:
java.lang.IllegalArgumentException
- if newTitle is null or empty.com.google.api.gax.rpc.ApiException
- if the remote call fails
-
updateAlbumCoverPhoto
public final Album updateAlbumCoverPhoto(Album album, MediaItem newCoverPhotoMediaItem)
Updates the media item used as the cover photo for an album.The newCoverPhotoMediaItem must be a media item contained within the album.
Only the `id` field of the album is used to identify the album. The album must be created by the developer and owned by the user.
- Parameters:
album
- Required. The [Album][google.photos.types.Album] to update.The album’s `id` field is used to identify the album to be updated.
newCoverPhotoMediaItem
- Required. The new cover photo for the album.- Returns:
- The updated album.
- Throws:
java.lang.IllegalArgumentException
- if newCoverPhotoMediaItem is null.com.google.api.gax.rpc.ApiException
- if the remote call fails.
-
updateAlbumCoverPhoto
public final Album updateAlbumCoverPhoto(Album album, java.lang.String newCoverPhotoMediaItemId)
Updates the media item used as the cover photo for an album.The newCoverPhotoMediaItemId must refer to a media item contained within the album.
Only the `id` field of the album is used to identify the album. The album must be created by the developer and owned by the user.
- Parameters:
album
- Required. The [Album][google.photos.types.Album] to update.The album’s `id` field is used to identify the album to be updated.
newCoverPhotoMediaItemId
- Required. The identifier of the new cover photo for the album.- Returns:
- The updated album.
- Throws:
java.lang.IllegalArgumentException
- if newCoverPhotoMediaItem is null.com.google.api.gax.rpc.ApiException
- if the remote call fails.
-
updateMediaItemDescription
public final MediaItem updateMediaItemDescription(MediaItem mediaItem, java.lang.String newDescription)
Updates the description of a media item.Only the `id` field of the media item is used to identify the item. The media item must be created by the developer and owned by the user.
- Parameters:
mediaItem
- Required. The [MediaItem][google.photos.types.MediaItem] to update.The media item's `id` field is used to identify the media item to be updated.
newDescription
- Required. The new description of the media item.- Returns:
- The updated media item.
- Throws:
java.lang.IllegalArgumentException
- if newDescription is null.com.google.api.gax.rpc.ApiException
- if the remote call fails.
-
listAlbums
public final InternalPhotosLibraryClient.ListAlbumsPagedResponse listAlbums()
Lists all albums shown to a user in the Albums tab of the Google Photos app.Calls
InternalPhotosLibraryClient.listAlbums(boolean)
with excludeNonAppCreateData set to false.
-
listMediaItems
public final InternalPhotosLibraryClient.ListMediaItemsPagedResponse listMediaItems()
List all media items in a user's Google Photos library.Calls
InternalPhotosLibraryClient.listMediaItems(ListMediaItemsRequest)
with an emptyListMediaItemsRequest
.
-
listSharedAlbums
public final InternalPhotosLibraryClient.ListSharedAlbumsPagedResponse listSharedAlbums()
Lists all shared albums available in the Sharing tab of the user's Google Photos app.Calls
InternalPhotosLibraryClient.listSharedAlbums(boolean)
with excludeNonAppCreateData set to false.
-
searchMediaItems
public final InternalPhotosLibraryClient.SearchMediaItemsPagedResponse searchMediaItems(Filters filters, OrderBy... orderBy)
Searches for media items in a user's Google Photos library. If no filters are set, then all media items in the user's library are returned. If an album is set, all media items in the specified album are returned. If filters are specified, media items that match the filters from the user's library are listed. If you set both the album and the filters, the request results in an error.Sample code:
try (InternalPhotosLibraryClient internalPhotosLibraryClient = InternalPhotosLibraryClient.create()) { Filters filters = Filters.newBuilder().build(); OrderBy newestFirstOrder = OrderBy.MEDIAMETADATA_CREATION_TIME_DESC; for (MediaItem element : internalPhotosLibraryClient.searchMediaItems(filters, newestFirstOrder).iterateAll()) { // doThingsWith(element); } }
- Parameters:
filters
- Filters to apply to the request. Can't be set in conjunction with an `albumId`.orderBy
- An optional field to specify the sort order of the search results. The `orderBy` field only works when a [dateFilter][google.photos.library.v1.DateFilter] is used. When this field is not specified, results are displayed newest first, oldest last by their [creationTime][google.photos.types.MediaMetadata.creation_time]. ProvidingOrderBy.MEDIAMETADATA_CREATION_TIME
displays search results in the opposite order, oldest first then newest last. To display results newest first then oldest last, include the `desc` argument usingOrderBy.MEDIAMETADATA_CREATION_TIME_DESC
.The only additional filters that can be used with this parameter are [includeArchivedMedia][google.photos.library.v1.Filters.include_archived_media] and [excludeNonAppCreatedData][google.photos.library.v1.Filters.exclude_non_app_created_data]. No other filters are supported.
- Throws:
com.google.api.gax.rpc.ApiException
- if the remote call fails
-
close
public void close()
- Specified by:
close
in interfacejava.lang.AutoCloseable
- Overrides:
close
in classInternalPhotosLibraryClient
-
-