Move api_ classes to be members of OpenPhoto (eg: client.photos_list --> client.photos.list)

This commit is contained in:
sneakypete81 2012-09-02 09:02:18 +01:00
parent 8307dca454
commit 21c38c53cb
4 changed files with 107 additions and 72 deletions

View file

@ -1,8 +1,20 @@
from openphoto_http import OpenPhotoHttp, OpenPhotoError, OpenPhotoDuplicateError
from api_photo import ApiPhoto
from api_tag import ApiTag
from api_album import ApiAlbum
import api_photo
import api_tag
import api_album
class OpenPhoto(OpenPhotoHttp, ApiPhoto, ApiTag, ApiAlbum):
class OpenPhoto(OpenPhotoHttp):
""" Client library for OpenPhoto """
pass
def __init__(self, host,
consumer_key='', consumer_secret='',
token='', token_secret=''):
OpenPhotoHttp.__init__(self, host,
consumer_key, consumer_secret,
token, token_secret)
self.photos = api_photo.ApiPhotos(self)
self.photo = api_photo.ApiPhoto(self)
self.tags = api_tag.ApiTags(self)
self.tag = api_tag.ApiTag(self)
self.albums = api_album.ApiAlbums(self)
self.album = api_album.ApiAlbum(self)

View file

@ -1,33 +1,40 @@
from openphoto_http import OpenPhotoHttp, OpenPhotoError
from objects import Album
class ApiAlbum(OpenPhotoHttp):
def album_create(self, name, **kwds):
""" Create a new album and return it"""
result = self.post("/album/create.json", name=name, **kwds)["result"]
return Album(self, result)
class ApiAlbums:
def __init__(self, client):
self._client = client
def album_delete(self, album_id, **kwds):
def list(self, **kwds):
""" Return a list of Album objects """
results = self._client.get("/albums/list.json", **kwds)["result"]
return [Album(self._client, album) for album in results]
class ApiAlbum:
def __init__(self, client):
self._client = client
def create(self, name, **kwds):
""" Create a new album and return it"""
result = self._client.post("/album/create.json", name=name, **kwds)["result"]
return Album(self._client, result)
def delete(self, album, **kwds):
""" Delete an album """
album = Album(self, {"id": album_id})
album = Album(self._client, {"id": album})
album.delete(**kwds)
def album_form(self, album_id, **kwds):
def form(self, album, **kwds):
raise NotImplementedError()
def album_add_photos(self, album_id, photo_ids, **kwds):
def add_photos(self, album, photos, **kwds):
raise NotImplementedError()
def album_remove_photos(self, album_id, photo_ids, **kwds):
def remove_photos(self, album, photos, **kwds):
raise NotImplementedError()
def albums_list(self, **kwds):
""" Return a list of Album objects """
results = self.get("/albums/list.json", **kwds)["result"]
return [Album(self, album) for album in results]
def album_update(self, album_id, **kwds):
def update(self, album, **kwds):
""" Update an album """
album = Album(self, {"id": album_id})
album = Album(self._client, {"id": album})
album.update(**kwds)
# Don't return the album, since the API doesn't give us the modified album

View file

@ -3,77 +3,85 @@ import base64
from openphoto_http import OpenPhotoHttp, OpenPhotoError
from objects import Photo
class ApiPhoto(OpenPhotoHttp):
def photo_delete(self, photo_id, **kwds):
class ApiPhotos:
def __init__(self, client):
self._client = client
def list(self, **kwds):
""" Returns a list of Photo objects """
photos = self._client.get("/photos/list.json", **kwds)["result"]
photos = self._client._result_to_list(photos)
return [Photo(self._client, photo) for photo in photos]
def update(self, photos, **kwds):
""" Updates a list of photos """
if not self._client.post("/photos/update.json", ids=photos, **kwds)["result"]:
raise OpenPhotoError("Update response returned False")
def delete(self, photos, **kwds):
""" Deletes a list of photos """
if not self._client.post("/photos/delete.json", ids=photos, **kwds)["result"]:
raise OpenPhotoError("Delete response returned False")
class ApiPhoto:
def __init__(self, client):
self._client = client
def delete(self, photo, **kwds):
""" Delete a photo """
photo = Photo(self, {"id": photo_id})
photo = Photo(self._client, {"id": photo})
photo.delete(**kwds)
def photo_edit(self, photo_id, **kwds):
def edit(self, photo, **kwds):
""" Returns an HTML form to edit a photo """
photo = Photo(self, {"id": photo_id})
photo = Photo(self._client, {"id": photo})
return photo.edit(**kwds)
def photo_replace(self, photo_id, photo_file, **kwds):
def replace(self, photo, photo_file, **kwds):
raise NotImplementedError()
def photo_replace_encoded(self, photo_id, photo_file, **kwds):
def replace_encoded(self, photo, photo_file, **kwds):
raise NotImplementedError()
def photo_update(self, photo_id, **kwds):
def update(self, photo, **kwds):
"""
Update a photo with the specified parameters.
Returns the updated photo object
"""
photo = Photo(self, {"id": photo_id})
photo = Photo(self._client, {"id": photo})
photo.update(**kwds)
return photo
def photo_view(self, photo_id, **kwds):
def view(self, photo, **kwds):
"""
Used to view the photo at a particular size.
Returns the requested photo object
"""
photo = Photo(self, {"id": photo_id})
photo = Photo(self._client, {"id": photo})
photo.view(**kwds)
return photo
def photos_list(self, **kwds):
""" Returns a list of Photo objects """
photos = self.get("/photos/list.json", **kwds)["result"]
photos = self._result_to_list(photos)
return [Photo(self, photo) for photo in photos]
def upload(self, photo_file, **kwds):
raise NotImplementedError("Use upload_encoded instead.")
def photos_update(self, photo_ids, **kwds):
""" Updates a list of photos """
if not self.post("/photos/update.json", ids=photo_ids, **kwds)["result"]:
raise OpenPhotoError("Update response returned False")
def photos_delete(self, photo_ids, **kwds):
""" Deletes a list of photos """
if not self.post("/photos/delete.json", ids=photo_ids, **kwds)["result"]:
raise OpenPhotoError("Delete response returned False")
def photo_upload(self, photo_file, **kwds):
raise NotImplementedError("Use photo_upload_encoded instead.")
def photo_upload_encoded(self, photo_file, **kwds):
def upload_encoded(self, photo_file, **kwds):
""" Base64-encodes and uploads the specified file """
encoded_photo = base64.b64encode(open(photo_file, "rb").read())
result = self.post("/photo/upload.json", photo=encoded_photo,
**kwds)["result"]
return Photo(self, result)
result = self._client.post("/photo/upload.json", photo=encoded_photo,
**kwds)["result"]
return Photo(self._client, result)
def photo_dynamic_url(self, photo_id, **kwds):
def dynamic_url(self, photo, **kwds):
raise NotImplementedError()
def photo_next_previous(self, photo_id, **kwds):
def next_previous(self, photo, **kwds):
"""
Returns a dict containing the next and previous photo objects,
given a photo in the middle.
"""
photo = Photo(self, {"id": photo_id})
photo = Photo(self._client, {"id": photo})
return photo.next_previous(**kwds)
def photo_transform(self, photo_id, **kwds):
def transform(self, photo, **kwds):
raise NotImplementedError()

View file

@ -1,25 +1,33 @@
from openphoto_http import OpenPhotoHttp, OpenPhotoError
from objects import Tag
class ApiTag(OpenPhotoHttp):
def tag_create(self, tag_id, **kwds):
""" Create a new tag and return it """
result = self.post("/tag/create.json", tag=tag_id, **kwds)["result"]
return Tag(self, result)
class ApiTags:
def __init__(self, client):
self._client = client
def tag_delete(self, tag_id, **kwds):
def list(self, **kwds):
""" Returns a list of Tag objects """
results = self._client.get("/tags/list.json", **kwds)["result"]
return [Tag(self._client, tag) for tag in results]
class ApiTag:
def __init__(self, client):
self._client = client
def create(self, tag, **kwds):
""" Create a new tag and return it """
result = self._client.post("/tag/create.json", tag=tag, **kwds)["result"]
return Tag(self._client, result)
def delete(self, tag, **kwds):
""" Delete a tag """
tag = Tag(self, {"id": tag_id})
tag = Tag(self._client, {"id": tag})
tag.delete(**kwds)
def tag_update(self, tag_id, **kwds):
def update(self, tag, **kwds):
""" Update a tag """
tag = Tag(self, {"id": tag_id})
tag = Tag(self._client, {"id": tag})
tag.update(**kwds)
return tag
def tags_list(self, **kwds):
""" Returns a list of Tag objects """
results = self.get("/tags/list.json", **kwds)["result"]
return [Tag(self, tag) for tag in results]