Skip to content

Instantly share code, notes, and snippets.

@filipebarcos
Last active March 2, 2021 11:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save filipebarcos/d4e8172b8adcf19f9cb7 to your computer and use it in GitHub Desktop.
Save filipebarcos/d4e8172b8adcf19f9cb7 to your computer and use it in GitHub Desktop.
Palestra "NAS COXA" GURU-CE 15 (Apr 18th, 2015)

“APIs, you're doing it wrong!”

What is an API?

From Wikipedia, we have “a set of Routines, protocols and tools for building software applications”.

Web API

REST

Re presentation S tate T ransfer is a software architecture style consisting of guidelines and best practices for creating scalable web services. REST is a coordinated set of constraints applied to the design of components in a distributed hypermedia system...

RESTful

Typically, communicate over HTTP, using same HTTP verbs used by web browsers to retrieve web pages and send data to remove servers.

Properties

  • Performance
  • Scalability

REST’s client-server separation of concerns simplifies component implementation, reduces the complexity of connector semantics, improves the effectiveness of performance tuning, and increases the scalability of pure server components. Layered system constraints allow intermediaries—proxies, gateways, and firewalls—to be introduced at various points in the communication without changing the interfaces between components, thus allowing them to assist in communication translation or improve performance via large-scale, shared caching. REST enables intermediate processing by constraining messages to be self-descriptive: interaction is stateless between requests, standard methods and media types are used to indicate semantics and exchange information, and responses explicitly indicate cacheability.

  • Simplicity
  • Modifiability
  • Portability
  • Reliability

Constraints

  • Client-server
  • Stateless (idempotent) -> No seesion: OAuth2
  • Cacheable: how to cache HTTP?
  • Layered System
  • Uniform Interface (Identification of resources, Manipulations of resources through representations, Self-descriptive messages, Hypermedia as the engine of application state)

Implementators should be aware that the software represents the user in their interactions over the Internet, and should be careful to allow the user to be aware of any actions they might take which may have an unexpected significance to themselves or others.

Safe and Idempontent

Safe methods means that we won’t make change on the server (just return data). Idempotency means that you can make the same request multiple times and you will always get the same result.

  • GET
    • “no side effects”: user cannot be accountable for them
    • Conditional GET (specific headers for caching)
    • Partial GET (range header)
  • HEAD: Similar to GET, but server MUST NOT return a message-body. Check for headers.
  • POST (not Idempotent??) -> https://www.masteringmodernpayments.com/blog/idempotent-stripe-requests
  • PUT/PATCH
  • DELETE (200, 202, 204, 404)
  • OPTIONS
  • TRACE: message sent == message returned
  • CONNECT: tunneling

Links

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment