Class: Courier::MessagesClient

Inherits:
Object
  • Object
show all
Defined in:
lib/trycourier/messages/client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(request_client:) ⇒ MessagesClient

Parameters:



16
17
18
19
# File 'lib/trycourier/messages/client.rb', line 16

def initialize(request_client:)
  # @type [RequestClient]
  @request_client = request_client
end

Instance Attribute Details

#request_clientObject (readonly)

Returns the value of attribute request_client.



12
13
14
# File 'lib/trycourier/messages/client.rb', line 12

def request_client
  @request_client
end

Instance Method Details

#archive(request_id:, request_options: nil) ⇒ Void

Parameters:

  • request_id (String)

    A unique identifier representing the request ID

  • request_options (RequestOptions) (defaults to: nil)

Returns:

  • (Void)


138
139
140
141
142
143
144
145
146
147
# File 'lib/trycourier/messages/client.rb', line 138

def archive(request_id:, request_options: nil)
  @request_client.conn.put("/requests/#{request_id}/archive") do |req|
    req.options.timeout = request_options.timeout_in_seconds unless request_options&.timeout_in_seconds.nil?
    unless request_options&.authorization_token.nil?
      req.headers["Authorization"] =
        request_options.authorization_token
    end
    req.headers = { **req.headers, **(request_options&.additional_headers || {}) }.compact
  end
end

#cancel(message_id:, request_options: nil) ⇒ Messages::MessageDetails

Cancel a message that is currently in the process of being delivered. A well-formatted API call to the cancel message API will return either ‘200` status code for a successful cancellation or `409` status code for an unsuccessful cancellation. Both cases will include the actual message record in the response body (see details below).

Parameters:

  • message_id (String)

    A unique identifier representing the message ID

  • request_options (IdempotencyRequestOptions) (defaults to: nil)

Returns:



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/trycourier/messages/client.rb', line 85

def cancel(message_id:, request_options: nil)
  response = @request_client.conn.post("/messages/#{message_id}/cancel") do |req|
    req.options.timeout = request_options.timeout_in_seconds unless request_options&.timeout_in_seconds.nil?
    unless request_options&.authorization_token.nil?
      req.headers["Authorization"] =
        request_options.authorization_token
    end
    req.headers["Idempotency-Key"] = request_options.idempotency_key unless request_options&.idempotency_key.nil?
    unless request_options&.idempotency_expiry.nil?
      req.headers["X-Idempotency-Expiration"] = request_options.idempotency_expiry
    end
    req.headers = { **req.headers, **(request_options&.additional_headers || {}) }.compact
  end
  Messages::MessageDetails.from_json(json_object: response.body)
end

#get(message_id:, request_options: nil) ⇒ Messages::MessageDetails

Fetch the status of a message you’ve previously sent.

Parameters:

  • message_id (String)

    A unique identifier associated with the message you wish to retrieve (results from a send).

  • request_options (RequestOptions) (defaults to: nil)

Returns:



68
69
70
71
72
73
74
75
76
77
78
# File 'lib/trycourier/messages/client.rb', line 68

def get(message_id:, request_options: nil)
  response = @request_client.conn.get("/messages/#{message_id}") do |req|
    req.options.timeout = request_options.timeout_in_seconds unless request_options&.timeout_in_seconds.nil?
    unless request_options&.authorization_token.nil?
      req.headers["Authorization"] =
        request_options.authorization_token
    end
    req.headers = { **req.headers, **(request_options&.additional_headers || {}) }.compact
  end
  Messages::MessageDetails.from_json(json_object: response.body)
end

#get_content(message_id:, request_options: nil) ⇒ Messages::RenderOutputResponse

Parameters:

  • message_id (String)

    A unique identifier associated with the message you wish to retrieve (results from a send).

  • request_options (RequestOptions) (defaults to: nil)

Returns:



123
124
125
126
127
128
129
130
131
132
133
# File 'lib/trycourier/messages/client.rb', line 123

def get_content(message_id:, request_options: nil)
  response = @request_client.conn.get("/messages/#{message_id}/output") do |req|
    req.options.timeout = request_options.timeout_in_seconds unless request_options&.timeout_in_seconds.nil?
    unless request_options&.authorization_token.nil?
      req.headers["Authorization"] =
        request_options.authorization_token
    end
    req.headers = { **req.headers, **(request_options&.additional_headers || {}) }.compact
  end
  Messages::RenderOutputResponse.from_json(json_object: response.body)
end

#get_history(message_id:, type: nil, request_options: nil) ⇒ Messages::MessageHistoryResponse

Fetch the array of events of a message you’ve previously sent.

Parameters:

  • message_id (String)

    A unique identifier representing the message ID

  • type (String) (defaults to: nil)

    A supported Message History type that will filter the events returned.

  • request_options (RequestOptions) (defaults to: nil)

Returns:



107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/trycourier/messages/client.rb', line 107

def get_history(message_id:, type: nil, request_options: nil)
  response = @request_client.conn.get("/messages/#{message_id}/history") do |req|
    req.options.timeout = request_options.timeout_in_seconds unless request_options&.timeout_in_seconds.nil?
    unless request_options&.authorization_token.nil?
      req.headers["Authorization"] =
        request_options.authorization_token
    end
    req.headers = { **req.headers, **(request_options&.additional_headers || {}) }.compact
    req.params = { **(request_options&.additional_query_parameters || {}), "type": type }.compact
  end
  Messages::MessageHistoryResponse.from_json(json_object: response.body)
end

#list(archived: nil, cursor: nil, event: nil, list: nil, message_id: nil, notification: nil, recipient: nil, status: nil, tags: nil, enqueued_after: nil, trace_id: nil, request_options: nil) ⇒ Messages::ListMessagesResponse

Fetch the statuses of messages you’ve previously sent.

Parameters:

  • archived (Boolean) (defaults to: nil)

    A boolean value that indicates whether archived messages should be included in the response.

  • cursor (String) (defaults to: nil)

    A unique identifier that allows for fetching the next set of message statuses.

  • event (String) (defaults to: nil)

    A unique identifier representing the event that was used to send the event.

  • list (String) (defaults to: nil)

    A unique identifier representing the list the message was sent to.

  • message_id (String) (defaults to: nil)

    A unique identifier representing the message_id returned from either /send or /send/list.

  • notification (String) (defaults to: nil)

    A unique identifier representing the notification that was used to send the event.

  • recipient (String) (defaults to: nil)

    A unique identifier representing the recipient associated with the requested profile.

  • status (String) (defaults to: nil)

    An indicator of the current status of the message. Multiple status values can be passed in.

  • tags (String) (defaults to: nil)

    A comma delimited list of ‘tags’. Messages will be returned if they match any of the tags passed in.

  • enqueued_after (String) (defaults to: nil)

    The enqueued datetime of a message to filter out messages received before.

  • trace_id (String) (defaults to: nil)

    The unique identifier used to trace the requests

  • request_options (RequestOptions) (defaults to: nil)

Returns:



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/trycourier/messages/client.rb', line 36

def list(archived: nil, cursor: nil, event: nil, list: nil, message_id: nil, notification: nil, recipient: nil,
         status: nil, tags: nil, enqueued_after: nil, trace_id: nil, request_options: nil)
  response = @request_client.conn.get("/messages") do |req|
    req.options.timeout = request_options.timeout_in_seconds unless request_options&.timeout_in_seconds.nil?
    unless request_options&.authorization_token.nil?
      req.headers["Authorization"] =
        request_options.authorization_token
    end
    req.headers = { **req.headers, **(request_options&.additional_headers || {}) }.compact
    req.params = {
      **(request_options&.additional_query_parameters || {}),
      "archived": archived,
      "cursor": cursor,
      "event": event,
      "list": list,
      "messageId": message_id,
      "notification": notification,
      "recipient": recipient,
      "status": status,
      "tags": tags,
      "enqueued_after": enqueued_after,
      "traceId": trace_id
    }.compact
  end
  Messages::ListMessagesResponse.from_json(json_object: response.body)
end