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
31. Phone API Versioning
Header Based
Accept-ÂâVersion:
1.0
!
Accept-ÂâVersion:
2.0
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
}
}