Skip to content
Merged
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
45 changes: 1 addition & 44 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -161,50 +161,7 @@ queries are structured in three levels:

Response data
~~~~~~~~~~~~~

The data from a query can be returned in three different formats: XML, JSON or as
a Python object.

To get the data in XML format a first positional argument (``raw_data``) has to be
set to ``True``. This is so because the default raw data response format is XML.

.. code:: python

>>> resp = cl.search_palette(True, id=2323)
>>> resp
'<?xml version="1.0" encoding="UTF-8" standalone="yes"?>\n<palettes numResults="1" totalResults="4567661">\n\t<palette>\n\t\t<id>2323</id>\n\t\t<title><![CDATA[On the lake]]></title>\n\t\t<userName><![CDATA[viatora]]></userName>\n\t\t<numViews>529</numViews>\n\t\t<numVotes>12</numVotes>\n\t\t<numComments>3</numComments>\n\t\t<numHearts>0</numHearts>\n\t\t<rank>0</rank>\n\t\t<dateCreated>2005-08-24 10:44:11</dateCreated>\n\t\t<colors>\n\t\t\t<hex>E6F0F7</hex>\n\t\t\t<hex>97A4B2</hex>\n\t\t\t<hex>5F0609</hex>\n\t\t\t<hex>766F59</hex>\n\t\t\t<hex>989383</hex>\n\t\t</colors>\n\t\t<description><![CDATA[I-MOO\r\n<div style="width: 300px; text-align: center;"><a href="http://www.colourlovers.com/contests/moo/minicard/2291466" target="_blank" style="display: block; margin-bottom: 5px; width: 300px; height: 120px; -moz-box-shadow: 0 1px 4px #d1d1d1; -webkit-box-shadow: 0 1px 4px #d1d1d1; box-shadow: 0 1px 4px #d1d1d1; filter: progid:DXImageTransform.Microsoft.Shadow(Strength=1, Direction=180, Color=]]></description>\n\t\t<url><![CDATA[http://www.colourlovers.com/palette/2323/On_the_lake]]></url>\n\t\t<imageUrl><![CDATA[http://www.colourlovers.com/paletteImg/E6F0F7/97A4B2/5F0609/766F59/989383/On_the_lake.png]]></imageUrl>\n\t\t<badgeUrl><![CDATA[http://www.colourlovers.com/images/badges/p/2/2323_On_the_lake.png]]></badgeUrl>\n\t\t<apiUrl>http://www.colourlovers.com/api/palette/2323</apiUrl>\n\t</palette>\n</palettes>'

To get the data in JSON format, a part from specifying that the response should be returned
as raw data by setting the first positional argument to ``True``, it is necessary to include
another keyword parameter in the query specifying that the format of the response should be
JSON (``format='json'``).

.. code:: python

>>> resp = cl.search_palette(True, id=2323, format='json')
>>> resp
'[
{
"id":2323,
"title":"On the lake",
"userName":"viatora",
"numViews":529,
"numVotes":12,
"numComments":3,
"numHearts":0,
"rank":0,
"dateCreated":"2005-08-24 10:44:11",
"colors":["E6F0F7","97A4B2","5F0609","766F59","989383"],
"description":"I-MOO\\r\\n<div style=\\"width: 300px; text-align: center;\\"><a href=\\"http:\\/\\/www.colourlovers.com\\/contests\\/moo\\/minicard\\/2291466\\" target=\\"_blank\\" style=\\"display: block; margin-bottom: 5px; width: 300px; height: 120px; -moz-box-shadow: 0 1px 4px #d1d1d1; -webkit-box-shadow: 0 1px 4px #d1d1d1; box-shadow: 0 1px 4px #d1d1d1; filter: progid:DXImageTransform.Microsoft.Shadow(Strength=1, Direction=180, Color=",
"url":"http:\\/\\/www.colourlovers.com\\/palette\\/2323\\/On_the_lake",
"imageUrl":"http:\\/\\/www.colourlovers.com\\/paletteImg\\/E6F0F7\\/97A4B2\\/5F0609\\/766F59\\/989383\\/On_the_lake.png",
"badgeUrl":"http:\\/\\/www.colourlovers.com\\/images\\/badges\\/p\\/2\\/2323_On_the_lake.png",
"apiUrl":"http:\\/\\/www.colourlovers.com\\/api\\/palette\\/2323"
}
]'

Finally, if the parameter raw data is not set or set to ``False`` then the data will be obtained
as a Python object (which is the recommended way). If the response of a query contains more than
The data will be obtained as a Python object. If the response of a query contains more than
one object - for example, when querying for palettes - then each object in the response will be
mapped to a Python object. Finally, when the response data is obtained as Python objects it will
always come inside of a list for consistency reasons among methods, even if the response contains
Expand Down
23 changes: 4 additions & 19 deletions colourlovers/clapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -224,18 +224,13 @@ def __public_api_method(self, search_type, data_container):
type and process and store the obtained data
"""

def _api_search(raw_data=False, **kwargs):
def _api_search(**kwargs):
"""
This method validates the request parameters and, if all
of them are valid, builds the request and posts it to the
API. It will return the data obtained from the response either
as raw data or as a Python object.

:param raw_data: Specifies how the data from the response
should be treated. If True, the data is returned as it is obtained
from the API response without any treatment. If False, the
corresponding Python object to store the obtained data is created and
returned
:param kwargs: query parameters for the specified type of request (Pattern,
Palette, ...). The keyword is the parameter name and the value is the parameter
value
Expand All @@ -245,14 +240,8 @@ def _api_search(raw_data=False, **kwargs):
# Validate the type of request (new, top, random, ...) taking into account the
# type of request (pattern, palette, colour, ...) that is to be performed
processed_request = self.__process_optional_requests(search_type, **kwargs)
if not isinstance(raw_data, bool):
raise ValueError("Invalid parameter " + str(raw_data))

if not raw_data:
# if user hasn't asked for the raw data of the API
# response build container objects. For that, we need
# the data in json format
processed_request.kwargs["format"] = "json"
processed_request.kwargs["format"] = "json"
# Once request type has been validated make the query to the API
api_response = self.__query(
search_type,
Expand All @@ -261,7 +250,7 @@ def _api_search(raw_data=False, **kwargs):
).decode()
# Process the data obtained from the query. We will build container
# objects by default unless otherwise specified
containers = self.__process_response(raw_data, api_response, data_container)
containers = self.__process_response(api_response, data_container)
if containers is not None:
return containers
else:
Expand Down Expand Up @@ -372,25 +361,21 @@ def __request(self, search_term, optional_request_term, **kwargs):
data = response.read()
return data

def __process_response(self, raw_data, api_response, request_type_class):
def __process_response(self, api_response, request_type_class):
"""
Once a request to the API has been made, process the obtained data
and parse it as requested.

Note: This method returns more than one type of object. It should be
refactored so that only one type of data is returned.

:param raw_data: Boolean specifying whether the obtained data should be
processed or returned as is
:param api_response: Raw data obtained from the API query
:param request_type_class: The type of data the request asked for
:return: Either None if no response was obtained, the raw data if
specified or the data wrapped in container objects
"""
if not api_response:
return None
if raw_data:
return api_response
else:
parsed_json = json.loads(api_response)
if isinstance(parsed_json, dict):
Expand Down