13. 13
Intuitive URI Structure
URI Description
/group/{id} A Facebook group
/group/{id}/feed This group’s feed
/group/{id}/files Files uploaded to this group
/group/{id}/events This group’s events
17. 17
Partial Responses
Get Full Response
/feeds/api/users/default/uploads
Get Partial Response
/feeds/api/users/default/uploads?
fields=entry(title,gd:comments,yt:statistics)
18. 18
Result Filtering
Get List of Videos
/feeds/api/videos?q=surfing&max-‐results=10
Get Videos with 1,000,000+ Views
/feeds/api/videos?q=surfing&max-‐results=10
&fields=entry[yt:statistics/@viewCount
>
1000000]
19. 19
Customized Responses
ItemLookup - Default
ItemId=B00008OE6I
ItemLookup - Default With Reviews
ItemId=B00008OE6I
&ResponseGroup=Reviews
ItemLookup - Large With Reviews and Offers
ItemId=B00008OE6I
&ResponseGroup=Large,Reviews,Offers
29. Phone Shopping App
API Features
Complete
vs.
Compact
representations
!
Including
related
entities
!
Linking
to
other
resources
30. Phone API Endpoints
URI Verb Description
/phones GET Get a list of phones
/phones/{id} GET Get phone details
/phones/{id}/manufacturer GET Get phone’s manufacturer
/phones/{id}/variations GET Get variations of a phone
42. API Features
• Accept Header Versioning
• Predictable Response Codes
• JSON and XML
• RESTful URIs
• Multiple Representations
• Pagination
• Custom Responses
• Related Links
43. Out of the Box APIs
@Resource(uri="/phones",
formats=["json",
"xml"])
class
Phone
{
…
}
44. @Resource URL Mappings
URL Mapping Verb Action
/phones GET List of phones
/phones POST Create new phone
/phones/{id} GET Get single phone
/phones/{id} PUT Update phone
/phones/{id} DELETE Delete phone
45. Read-Only URL Mappings
@Resource(uri="/phones",
formats=["json",
"xml"],
readOnly=true)
URL Mapping Verb Action
/phones GET List of phones
/phones/{id} GET Get single phone
59. Out of the Box Customization
include
attributes exclude
attributes
beans
{
bookRenderer(XmlRenderer,
Book)
{
includes
=
[‘title’]
}
}
beans
{
bookRenderer(XmlRenderer,
Book)
{
excludes
=
[‘title’]
}
}
This can be useful for very!
simple customization
60. Simplified Picture of the Response Flow
Controller Renderer Converter Marshaller
as
JSON/XML
respond
render
value
marshal
61. Creating a Custom Marshaller
class
PhoneMarshallerJson
extends
ClosureObjectMarshaller<JSON>
{
!
static
final
marshal
=
{
Phone
phone
-‐>
def
map
=
[:]
…
map
}
!
public
PhoneMarshallerJson()
{
super(Phone,
marshal)
}
}
62. Creating a Custom Marshaller
class
PhoneMarshallerXml
extends
ClosureObjectMarshaller<XML>{
!
def
static
final
marshal
=
{
Phone
phone,
XML
xml
-‐>
xml.build
{
name(phone.name)
}
}
!
public
PhoneMarshallerXml()
{
super(Phone,
marshal)
}
}
63. Registering a Custom Marshaller
beans
=
{
customPhoneJsonMarshaller(ObjectMarshallerRegisterer)
{
marshaller
=
new
PhoneMarshallerJson()
converterClass
=
JSON
priority
=
1
}
!
customPhoneXmlMarshaller(ObjectMarshallerRegisterer)
{
marshaller
=
new
PhoneMarshallerXml()
converterClass
=
XML
priority
=
1
}
}