API Design Process

API Design 을 ν•˜κΈ° μœ„ν•΄μ„œλŠ” 보톡 λ‹€μŒμ˜ 단계λ₯Ό λ”°λ₯Έλ‹€.

  1. Review API requirements
  2. Identify main resource / entity
  3. Use HTTP methods to assigin action on resource
Step 1: Review API Requirements

boss λ‘œλΆ€ν„° λ‹€μŒ API requirements λ₯Ό μš”κ΅¬λ°›μ•˜λ‹€κ³  κ°€μ •ν•΄λ³΄μž.

Create a RET API for the Employee Directory

REST clients should be able to

  • get a list of employees
  • get a single employee by id
  • add a new employee
  • update an employee
  • delete an employee
Step 2: Identify main resource / entity

그리고 Service μ „λ°˜μ— κ±Έμ³μ„œ μ‚¬μš©λ˜λŠ” main resource / entity 의 name 을 μ •ν•΄μ•Ό ν•œλ‹€. μœ„μ˜ requirements 의 경우 employee λΌλŠ” noun 이 κ°€μž₯ μ€‘μš”ν•œ keyword κ°€ λœλ‹€.

convention 은 보톡 resource / entity 의 plural, 즉 λ³΅μˆ˜ν˜• ν˜•νƒœλ₯Ό μ‚¬μš©ν•œλ‹€. μ—¬κΈ°μ„œλŠ” employees κ°€ 될 것이닀. μ‹€μ œλ‘œλŠ” /api/employees 와 같이 endpoint 둜 μ‚¬μš©ν•œλ‹€.

Step 3: Use HTTP methods to assigin action on resource

POST, GET, PUT, DELETE method λ₯Ό μ‚¬μš©ν•˜μ—¬ resource 에 μ–΄λ– ν•œ λ™μž‘μ„ μˆ˜ν–‰ν•˜λ„λ‘ λ§Œλ“ λ‹€.

HTTP MethodEndpointCRUD Action
POST/api/employeesCreate a new employee
GET/api/employeesRead a list of employees
GET/api/employees/{employeeId}Read a single employee
PUT/api/employeesUpdate an existing employee
DELETE/api/employees/{employeeId}Delete an existing employee
μœ„μ™€ 같이 λ™μΌν•œ endpoint λ₯Ό μ‚¬μš©ν•˜κ³  HTTP method λ₯Ό μ΄μš©ν•˜μ—¬ λ‹€λ₯Έ λ™μž‘μ„ μˆ˜ν–‰ν•˜λ„λ‘ ν•˜λŠ” 것이 μ’‹λ‹€.

/api/employeesList, /api/addEmployee 와 같이 μ–΄λ– ν•œ action 을 μˆ˜ν–‰ν•˜λŠ”μ§€λ₯Ό endpoint name 으둜 μ •ν•˜λŠ” 것은 anti-pattern 으둜 λΆ„λ₯˜λ˜μ–΄ μ§€μ–‘ν•΄μ•Ό ν•œλ‹€.

Paypal, GitHub λ“± μ‹€μ œ Real-Time Service μ—μ„œ μ‚¬μš©λ˜λŠ” REST API 도 λͺ¨λ‘ λ™μΌν•œ endpoint 에 λŒ€ν•˜μ—¬ λ‹€λ₯Έ HTTP method λ₯Ό μ‚¬μš©ν•¨μœΌλ‘œμ¨ λ‹€λ₯Έ λ™μž‘μ„ μˆ˜ν–‰ν•˜λ„λ‘ design ν•˜μ˜€λ‹€.