mailgun_ex v0.2.8 MailgunEx.Request View Source

A structure to capture the request parameters to send to HTTPoision, this allows us to test the request without actually havig to send it; for ease (and speed) of testing.

A %Request{} struct contains the following parts:

  • url - Where are we sending the request
  • body - What is the body of the request
  • headers - What headers are we sending
  • :mode - Defaults to :live, but can be set to :simulate for testing, or :ignore for dev
  • http_opts - All others configs, such as query :params

Link to this section Summary

Functions

Build a HTTP request based on the provided options, which comprise

Send an HTTP request, there are two modes of sending. If it’s mode: :live, then we will use HTTPoison under the hood, so take a look at their API for additional configuration options

Link to this section Functions

Build a HTTP request based on the provided options, which comprise

Example

iex> MailgunEx.Request.create().mode
:live

iex> MailgunEx.Request.create(mode: :simulate).mode
:simulate

iex> MailgunEx.Request.create(domain: "namedb.org", resource: "logs").url
"https://api.mailgun.net/v3/namedb.org/logs"

iex> MailgunEx.Request.create(body: "What is life?").body
"What is life?"

iex> MailgunEx.Request.create(api_key: "key-abc123").headers
[{"Authorization", "Basic YXBpOmtleS1hYmMxMjM="}]

iex> MailgunEx.Request.create(params: [limit: 10], timeout: 1000).http_opts
[params: [limit: 10], timeout: 1000]

Send an HTTP request, there are two modes of sending. If it’s mode: :live, then we will use HTTPoison under the hood, so take a look at their API for additional configuration options.

For example,

%Request{url: "https://mailgun.local/domains"} |> Request.send(:get)

On the other hand, if it’s in mode: :simulate then we will just store the result (in MailgunEx.Simulate) and return the result (also from) MailgunEx.Simulate.

To send a simulated request,

MailgunEx.Simulate.add_response(:x)
MailgunEx.Simulate.add_response({
  200,
  %{body: "[]", status_code: 200, headers: [{"Content-Type", "application/json"}]}
})

%Request{mode: :simulate, url: "https://mailgun.local/domains"}
|> Request.send(:get)

Or, if you don’t care about the result just set mode: :ignore and we will always a 200 response.