Class PhotosLibraryClient

  • All Implemented Interfaces:
    com.google.api.gax.core.BackgroundResource, java.lang.AutoCloseable

    public final class PhotosLibraryClient
    extends InternalPhotosLibraryClient
    An wrapper class of InternalPhotosLibraryClient.

    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:

    1. 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.
    2. 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.
    3. 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() or InternalPhotosLibraryClient.create(InternalPhotosLibrarySettings) for this wrapper.

    • Method Detail

      • 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 fails
        java.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.
      • 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]. Providing OrderBy.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 using OrderBy.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