Upload
ortus-solutions-corp
View
103
Download
2
Embed Size (px)
Citation preview
RESTful-ly GoodnessSimplify common API Tasks
• Coldbox Routing
• Request Context:renderData();getHTTPHeader();getHTTPMethod();
• Coldbox Proxy• Modularity• Interceptors
Powerful Routing Configuration
// API Routingvar defaultAPIActions = {
"GET":"index","POST":"add","PUT":"onInvalidHTTPMethod","PATCH":"onInvalidHTTPMethod","DELETE":"onInvalidHTTPMethod"
};var defaultEntityActions = {
"GET":"get","PUT":"update","PATCH":"update","DELETE":"delete"
};
//Users API Routes addRoute(
pattern='/api/v1/users/:id',handler='api.v1.Users',action=defaultEntityActions
);
addRoute(pattern='/api/v1/users',handler='api.v1.Users',action=defaultAPIActions
);
Built-In Methods to Minimize Duplication of Effortfunction preHandler(event,action,eventArguments,rc,prc){
rc.format="JSON";//404 defaultrc.statusCode=STATUS.NOT_FOUND;rc.data = {};event.noLayout();
//handle any content payloadsif(
len(getHttpRequestData().content) and isJson(getHttpRequestData().content)
) structAppend(rc, deserializeJson(getHttpRequestData().content),true);
//set our access control allow origin header for cross-domain requestsheader name="Access-Control-Allow-Origin" value="*";
//process our login tokensprocessAuthenticationTokens(argumentCollection=arguments);prc.currentUser = SessionService.getCurrentUser();
}
preHandler()
Built-In Methods to Minimize Duplication of Effort
function postHandler(event,action,eventArguments,rc,prc){event.renderData(
type=rc.format, data=rc.data, statusCode=rc.statusCode, statusMessage=event.getValue(“statusMessage”, “Success”));
}
postHandler()
Pre-configured REST App Template
mkdir myapi --cdcoldbox create app skeleton=rest name=MyService --installColdBox server start --rewritesEnable
API Documentation• Helps to define coding conventions
• Allows API evolution to maintain consistency and standards
• Decreases onboarding time and increases adoption for API consumers
• Provides flexibility in development sprint planning by allowing members, owners, and stakeholders to visualize the product and ensure needs are met
RELAX
A business case for developers:
Next GenerationRELAX
• Open and transferrable documentation using OpenAPI (fka Swagger)
• Maintain backward compatibility for RelaxDSL (for a time)
• Minimize the need for duplication of effort
• Streamline the drill-down process to the response level
• Make documentation authoring and revisions pain-free
Next GenerationRELAX
Drill down quickly to paths, methods, schemas and samples
References and internal document links
Extend OpenAPI schema to (near)infinite
Next GenerationRELAX
Fully recursive to allow for automation and uniform consumption of documentation resources
Long-term…RELAX
Evaluate and evolve through innovation and by incorporating the best features of API Tools, integrating them into a single workflow
API DevelopmentLessons Learned:• Maintain consistency in responses to minimize conditionals
in consumption
• Implement your paging and throttling strategies *NOW*!
• Standardize error handling
• Less is *more* (Massive data dumps kill your API)
• Marshall deeply nested arrays into hashmap representations, whenever possible. Recursion kills.
API DevelopmentLessons Learned: GET
POSTPUTPATCHDELETEHEADOPTIONS
http://www.restapitutorial.com/lessons/httpmethods.html
API DevelopmentLessons Learned:200 - “You wanted it... Here you go, buddy:” 201 - “I've created something new for you!” 202 - “Your request is accepted, heres a URL so you can check the status:” 203 - “Here's a record, but we haven't confirmed persistence” 204 - “I deleted what you told me. Now it's nothing, so... Here's nothing!:” 205 - “A record has been updated while you were slacking off. Re-request the data and refresh your view.” 206 - “There are, like, a gazillion rows, man. Here's 25 of them so I don't break