Empathic API
Design
CORNEIL DU PLESSIS
About Me
â—Ź Programmer since 1985
â—Ź Smallest to very large systems.
â—Ź Cobol, Pascal, Algol, C/C++, Java, Scala, Groovy and other JVM
languages.
â—Ź Scientific instrumentation, Sports event management, Mining, Banking,
Treasury and Insurance.
â—Ź Software Architect (coding included)
Quote
The Opposite of Empathy is Contempt.
Anonymous
Introduction
â—Ź What is an API?
â—Ź Considerations in API Design
â—Ź Why Empathy in API Design?
â—Ź Documenting your API
What is an API?
Definition:
Application Programming Interface
Examples:
– C stdlib.h
– Java Standard Library
– Spring Framework
– Google Maps API
– Facebook Graph API
Considerations in API Design
Quote:
Everything should be made as simple as possible, but no simpler
Albert Einstein
Considerations in API Design
Joshua Bloch
Principal Software Engineer at Google
How to Design a Good API and Why it Matters
– Easy to learn
– Easy to use, even without documentation
– Hard to misuse
– Easy to read and maintain code that uses it
– Sufficiently powerful to satisfy requirements
– Easy to extend
– Appropriate to audience
Considerations in API Design
â—Ź Simple
â—Ź Unambiguous
â—Ź Usage oriented
â—Ź Asset
â—Ź Discipline
Why Empathy in API Design?
● When your audience doesn’t have an alternative.
â—Ź When your audience is human.
â—Ź Because they depend on it.
â—Ź Because they gave you money.
Documenting your API
â—Ź Code is the document
â—Ź Tools
– Javadoc
– Doxygen
– Asciidoc
â—Ź Rest API
– RAML
– Swagger
– Spring Rest Docs
RAML
â—Ź RESTful API Modeling Language
â—Ź RAML 0.8
– YAML
– Like RFC
â—Ź RAML 1.0
– YAML 1.2
– Template URI
– Data types
– Annotations
– Security Schemes
RAML
#%RAML 1.0
title: Spring Data Rest Demo API
version: v1
protocols: [ HTTP ]
baseUri: http://localhost/api
mediaType: application/json
types:
RAML - Types
types:
User:
type: object
properties:
userId: string
emailAddress: string
fullName: string
dateOfBirth: date-only
hasImage: boolean
Group:
type: object
properties:
description: string
groupName: string
groupOwner: User
RAML – Operations
/users:
post:
body:
type: User
description: Create a User
responses:
200:
body:
type: User
RAML - Operations
/users/{id}:
get:
description: Get a User
responses:
200:
body:
type: User
404:
description: User doesn't exist
/users/search:
get:
queryParameters:
input:
description: Sub string to match
type: string
responses:
200:
body:
type: User[]
Swagger
â—Ź Swagger 2.0
â—Ź JSON / YAML
â—Ź JSON Schema
Swagger – Springfox
â—Ź Swagger from Annotations
â—Ź Spring MVC - Rest
â—Ź Swagger UI
Springfox - Configuration
@EnableSwagger2
@Configuration
public class SwaggerConfiguration {
@Bean
public Docket swaggerSpringMvcPlugin() {
return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo());
}
private ApiInfo apiInfo() {
return new ApiInfo("Spring Data Rest Demo",
"Demonstrate Spring Data RestController",
"2",
"/",
new Contact("Corneil du Plessis", "",
"corneil.duplessis@gmail.com"),
"",
"");
}
}
Springfox - Configuration
@EnableWebMvc
@Configuration
public class WebApplicationConfiguration
extends WebMvcConfigurerAdapter {
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry)
{
registry.addResourceHandler("/swagger-ui.html")
.addResourceLocations("classpath:/META-INF/resources/");
registry.addResourceHandler("/webjars/**")
.addResourceLocations(
"classpath:/META-INF/resources/webjars/");
}
}
Swagger - Springfox
@RequestMapping(path = "/users/{id}", method = RequestMethod.GET)
@ApiOperation(value = "Get User")
@ApiResponses({
@ApiResponse(code = 200, message = "Success", response =
User.class),
@ApiResponse(code = 404, message = "Entity Not Found")
})
public ResponseEntity<User> getUser(@PathVariable("id") Long id)
throws EntityNotFound {
User user = userRepository.findOne(id);
if (user == null) {
throw new EntityNotFound();
}
return ResponseEntity.ok(user);
}
Springfox - UI
Demo
Spring Rest Docs
â—Ź Documentation driven by Unit Tests
– Asciidoc
– Snippets
– Conventions
– Test Methods
– Feedback on completeness
Spring Rest Docs
Demo
Conclusion
Questions?
Resources
– https://github.com/corneil/spring-data-rest-angular-demo
– http://raml.org
– http://swagger.io
– http://springfox.github.io/springfox
– https://projects.spring.io/spring-restdocs

Empathic API-Design

  • 1.
  • 2.
    About Me â—Ź Programmersince 1985 â—Ź Smallest to very large systems. â—Ź Cobol, Pascal, Algol, C/C++, Java, Scala, Groovy and other JVM languages. â—Ź Scientific instrumentation, Sports event management, Mining, Banking, Treasury and Insurance. â—Ź Software Architect (coding included)
  • 3.
    Quote The Opposite ofEmpathy is Contempt. Anonymous
  • 4.
    Introduction â—Ź What isan API? â—Ź Considerations in API Design â—Ź Why Empathy in API Design? â—Ź Documenting your API
  • 5.
    What is anAPI? Definition: Application Programming Interface Examples: – C stdlib.h – Java Standard Library – Spring Framework – Google Maps API – Facebook Graph API
  • 6.
    Considerations in APIDesign Quote: Everything should be made as simple as possible, but no simpler Albert Einstein
  • 7.
    Considerations in APIDesign Joshua Bloch Principal Software Engineer at Google How to Design a Good API and Why it Matters – Easy to learn – Easy to use, even without documentation – Hard to misuse – Easy to read and maintain code that uses it – Sufficiently powerful to satisfy requirements – Easy to extend – Appropriate to audience
  • 8.
    Considerations in APIDesign â—Ź Simple â—Ź Unambiguous â—Ź Usage oriented â—Ź Asset â—Ź Discipline
  • 9.
    Why Empathy inAPI Design? ● When your audience doesn’t have an alternative. ● When your audience is human. ● Because they depend on it. ● Because they gave you money.
  • 10.
    Documenting your API ●Code is the document ● Tools – Javadoc – Doxygen – Asciidoc ● Rest API – RAML – Swagger – Spring Rest Docs
  • 11.
    RAML ● RESTful APIModeling Language ● RAML 0.8 – YAML – Like RFC ● RAML 1.0 – YAML 1.2 – Template URI – Data types – Annotations – Security Schemes
  • 12.
    RAML #%RAML 1.0 title: SpringData Rest Demo API version: v1 protocols: [ HTTP ] baseUri: http://localhost/api mediaType: application/json types:
  • 13.
    RAML - Types types: User: type:object properties: userId: string emailAddress: string fullName: string dateOfBirth: date-only hasImage: boolean Group: type: object properties: description: string groupName: string groupOwner: User
  • 14.
    RAML – Operations /users: post: body: type:User description: Create a User responses: 200: body: type: User
  • 15.
    RAML - Operations /users/{id}: get: description:Get a User responses: 200: body: type: User 404: description: User doesn't exist /users/search: get: queryParameters: input: description: Sub string to match type: string responses: 200: body: type: User[]
  • 16.
    Swagger â—Ź Swagger 2.0 â—ŹJSON / YAML â—Ź JSON Schema
  • 17.
    Swagger – Springfox ●Swagger from Annotations ● Spring MVC - Rest ● Swagger UI
  • 18.
    Springfox - Configuration @EnableSwagger2 @Configuration publicclass SwaggerConfiguration { @Bean public Docket swaggerSpringMvcPlugin() { return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo()); } private ApiInfo apiInfo() { return new ApiInfo("Spring Data Rest Demo", "Demonstrate Spring Data RestController", "2", "/", new Contact("Corneil du Plessis", "", "corneil.duplessis@gmail.com"), "", ""); } }
  • 19.
    Springfox - Configuration @EnableWebMvc @Configuration publicclass WebApplicationConfiguration extends WebMvcConfigurerAdapter { @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { registry.addResourceHandler("/swagger-ui.html") .addResourceLocations("classpath:/META-INF/resources/"); registry.addResourceHandler("/webjars/**") .addResourceLocations( "classpath:/META-INF/resources/webjars/"); } }
  • 20.
    Swagger - Springfox @RequestMapping(path= "/users/{id}", method = RequestMethod.GET) @ApiOperation(value = "Get User") @ApiResponses({ @ApiResponse(code = 200, message = "Success", response = User.class), @ApiResponse(code = 404, message = "Entity Not Found") }) public ResponseEntity<User> getUser(@PathVariable("id") Long id) throws EntityNotFound { User user = userRepository.findOne(id); if (user == null) { throw new EntityNotFound(); } return ResponseEntity.ok(user); }
  • 21.
  • 22.
    Spring Rest Docs ●Documentation driven by Unit Tests – Asciidoc – Snippets – Conventions – Test Methods – Feedback on completeness
  • 23.
  • 24.
    Conclusion Questions? Resources – https://github.com/corneil/spring-data-rest-angular-demo – http://raml.org –http://swagger.io – http://springfox.github.io/springfox – https://projects.spring.io/spring-restdocs