Loopback.Interceptors.Hackney (Loopback v0.1.0)

Copy Markdown View Source

An interceptor for hackney, and the clients built on it, like HTTPoison and Tesla.Adapter.Hackney.

It supports hackney 1.x and 4.x, and replies the way the installed version does. For example, hackney 1.x returns a reference to read the body from with :hackney.body/1, and hackney 4.x returns the body itself.

Implementation notes:

  • A request that needs a connection after it returns, like one whose body is read later, one whose request body is sent with :hackney.send_body/2, or an asynchronous one, gets a process that stands in for the connection. Its PID is the request's reference, also in hackney 1.x, which uses references for its own connections. The other calls, like :hackney.body/1, have to be made while that process runs: until the body is read, or the process that made the request exits.
  • Request bodies can be binaries, iolists, {:form, fields}, {:multipart, parts}, :stream, sent with :hackney.send_body/2, and in hackney 1.x {:file, path}. Other bodies, like functions, raise.
  • A request with a :stream body reaches the fakes in :hackney.start_response/1. A request passed through is sent then, and the body has to be read with the reference that :hackney.start_response/1 returns, like HTTPoison and Tesla do.
  • The headers that hackney adds itself, like content-length, aren't added to the request that fakes receive.
  • A fake's response arrives in one piece: :hackney.stream_body/1 returns the whole body as one chunk, and an asynchronous response sends it as one message.
  • Options that control the connection, like :recv_timeout, apply only to requests passed through.

Summary

Functions

body(conn)

body(conn, length_or_timeout)

close(conn)

finish_send_body(conn)

request(url)

request(method, url)

request(method, url, headers)

request(method, url, headers, body)

request(method, url, headers, body, options)

send_body(conn, data)

skip_body(conn)

start_response(conn)

stream_body(conn)

stream_next(conn)