Skip to content

Commit

Permalink
add 'change_launch_mode' method
Browse files Browse the repository at this point in the history
  • Loading branch information
pixel365 committed Jul 9, 2024
1 parent c482c39 commit 818c82b
Show file tree
Hide file tree
Showing 7 changed files with 202 additions and 23 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ Reyden-X is an automated service for promoting live broadcasts on external sites
```elixir
def deps do
[
{:reydenx, "~> 0.1.0"}
{:reydenx, "~> 0.1.1"}
]
end
```
Expand Down
182 changes: 174 additions & 8 deletions lib/reydenx/action.ex
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,17 @@ defmodule Reydenx.Action do
iex> {:ok, token} = Reydenx.new()
iex> Reydenx.Action.run(token, 123456)
{:ok, %Reydenx.Model.ActionResult{}}
{:ok, %Reydenx.Model.ActionResult{
request_id: "",
order_id: 123456,
action: "run",
value: nil,
task: %Reydenx.Model.Task{
id: "h_Hg_ZWcjqRN2sd9GScBqg",
url: "https://api.reyden-x.com/v1/orders/123456/task/h_Hg_ZWcjqRN2sd9GScBqg/status/",
expires_at: "2024-07-09T09:21:29.577065+00:00"
}
}}
### Fail:
Expand All @@ -51,7 +61,17 @@ defmodule Reydenx.Action do
iex> {:ok, token} = Reydenx.new()
iex> Reydenx.Action.stop(token, 123456)
{:ok, %Reydenx.Model.ActionResult{}}
{:ok, %Reydenx.Model.ActionResult{
request_id: "",
order_id: 123456,
action: "stop",
value: nil,
task: %Reydenx.Model.Task{
id: "h_Hg_ZWcjqRN2sd9GScBqg",
url: "https://api.reyden-x.com/v1/orders/123456/task/h_Hg_ZWcjqRN2sd9GScBqg/status/",
expires_at: "2024-07-09T09:21:29.577065+00:00"
}
}}
### Fail:
Expand All @@ -78,7 +98,17 @@ defmodule Reydenx.Action do
iex> {:ok, token} = Reydenx.new()
iex> Reydenx.Action.cancel(token, 123456)
{:ok, %Reydenx.Model.ActionResult{}}
{:ok, %Reydenx.Model.ActionResult{
request_id: "",
order_id: 123456,
action: "cancel",
value: nil,
task: %Reydenx.Model.Task{
id: "h_Hg_ZWcjqRN2sd9GScBqg",
url: "https://api.reyden-x.com/v1/orders/123456/task/h_Hg_ZWcjqRN2sd9GScBqg/status/",
expires_at: "2024-07-09T09:21:29.577065+00:00"
}
}}
### Fail:
Expand All @@ -105,7 +135,17 @@ defmodule Reydenx.Action do
iex> {:ok, token} = Reydenx.new()
iex> Reydenx.Action.change_online(token, 123456, 1000)
{:ok, %Reydenx.Model.ActionResult{}}
{:ok, %Reydenx.Model.ActionResult{
request_id: "",
order_id: 123456,
action: "change:online",
value: nil,
task: %Reydenx.Model.Task{
id: "h_Hg_ZWcjqRN2sd9GScBqg",
url: "https://api.reyden-x.com/v1/orders/123456/task/h_Hg_ZWcjqRN2sd9GScBqg/status/",
expires_at: "2024-07-09T09:21:29.577065+00:00"
}
}}
### Fail:
Expand Down Expand Up @@ -133,7 +173,17 @@ defmodule Reydenx.Action do
iex> {:ok, token} = Reydenx.new()
iex> Reydenx.Action.change_increase(token, 123456, 1000)
{:ok, %Reydenx.Model.ActionResult{}}
{:ok, %Reydenx.Model.ActionResult{
request_id: "",
order_id: 123456,
action: "change:increase:value",
value: nil,
task: %Reydenx.Model.Task{
id: "h_Hg_ZWcjqRN2sd9GScBqg",
url: "https://api.reyden-x.com/v1/orders/123456/task/h_Hg_ZWcjqRN2sd9GScBqg/status/",
expires_at: "2024-07-09T09:21:29.577065+00:00"
}
}}
### Fail:
Expand Down Expand Up @@ -161,7 +211,17 @@ defmodule Reydenx.Action do
iex> {:ok, token} = Reydenx.new()
iex> Reydenx.Action.increase_on(token, 123456, 1000)
{:ok, %Reydenx.Model.ActionResult{}}
{:ok, %Reydenx.Model.ActionResult{
request_id: "",
order_id: 123456,
action: "increase:on",
value: nil,
task: %Reydenx.Model.Task{
id: "h_Hg_ZWcjqRN2sd9GScBqg",
url: "https://api.reyden-x.com/v1/orders/123456/task/h_Hg_ZWcjqRN2sd9GScBqg/status/",
expires_at: "2024-07-09T09:21:29.577065+00:00"
}
}}
### Fail:
Expand Down Expand Up @@ -189,7 +249,17 @@ defmodule Reydenx.Action do
iex> {:ok, token} = Reydenx.new()
iex> Reydenx.Action.increase_off(token, 123456)
{:ok, %Reydenx.Model.ActionResult{}}
{:ok, %Reydenx.Model.ActionResult{
request_id: "",
order_id: 123456,
action: "increase:off",
value: nil,
task: %Reydenx.Model.Task{
id: "h_Hg_ZWcjqRN2sd9GScBqg",
url: "https://api.reyden-x.com/v1/orders/123456/task/h_Hg_ZWcjqRN2sd9GScBqg/status/",
expires_at: "2024-07-09T09:21:29.577065+00:00"
}
}}
### Fail:
Expand All @@ -216,7 +286,17 @@ defmodule Reydenx.Action do
iex> {:ok, token} = Reydenx.new()
iex> Reydenx.Action.add_views(token, 123456, 1000)
{:ok, %Reydenx.Model.ActionResult{}}
{:ok, %Reydenx.Model.ActionResult{
request_id: "",
order_id: 123456,
action: "add:views",
value: nil,
task: %Reydenx.Model.Task{
id: "h_Hg_ZWcjqRN2sd9GScBqg",
url: "https://api.reyden-x.com/v1/orders/123456/task/h_Hg_ZWcjqRN2sd9GScBqg/status/",
expires_at: "2024-07-09T09:21:29.577065+00:00"
}
}}
### Fail:
Expand All @@ -233,6 +313,86 @@ defmodule Reydenx.Action do
def add_views(token, order_id, value) when is_gt(value),
do: r(token, order_id, "action/add/views/#{value}/")

@doc """
Change Launch Parameters
[API Documentation](https://api.reyden-x.com/docs#/Orders/change_launch_params_v1_orders__order_id__action_change_launch__patch)
## Examples:
### Success:
iex> {:ok, token} = Reydenx.new()
iex> Reydenx.Action.change_launch_mode(token, 123456, :auto)
{:ok, %Reydenx.Model.ActionResult{
request_id: "",
order_id: 123456,
action: "change:launch",
value: nil,
task: %Reydenx.Model.Task{
id: "h_Hg_ZWcjqRN2sd9GScBqg",
url: "https://api.reyden-x.com/v1/orders/123456/task/h_Hg_ZWcjqRN2sd9GScBqg/status/",
expires_at: "2024-07-09T09:21:29.577065+00:00"
}
}}
iex> {:ok, token} = Reydenx.new()
iex> Reydenx.Action.change_launch_mode(token, 123456, :manual)
{:ok, %Reydenx.Model.ActionResult{
request_id: "",
order_id: 123456,
action: "change:launch",
value: nil,
task: %Reydenx.Model.Task{
id: "h_Hg_ZWcjqRN2sd9GScBqg",
url: "https://api.reyden-x.com/v1/orders/123456/task/h_Hg_ZWcjqRN2sd9GScBqg/status/",
expires_at: "2024-07-09T09:21:29.577065+00:00"
}
}}
iex> {:ok, token} = Reydenx.new()
iex> Reydenx.Action.change_launch_mode(token, 123456, :delay, 60)
{:ok, %Reydenx.Model.ActionResult{
request_id: "",
order_id: 123456,
action: "change:launch",
value: 60,
task: %Reydenx.Model.Task{
id: "h_Hg_ZWcjqRN2sd9GScBqg",
url: "https://api.reyden-x.com/v1/orders/123456/task/h_Hg_ZWcjqRN2sd9GScBqg/status/",
expires_at: "2024-07-09T09:21:29.577065+00:00"
}
}}
### Fail:
iex> {:ok, token} = Reydenx.new()
iex> Reydenx.Action.change_launch_mode(token, 123456, :auto)
{:error, %Reydenx.Model.ResponseError{
path: "/",
status_code: 404,
message: "Not Found"
}}
"""
@doc since: "0.1.1"
@spec change_launch_mode(token :: %Token{}, order_id :: p(), mode :: :auto | :manual) :: t()
def change_launch_mode(token, order_id, mode)
when is_valid_token(token) and is_gt(order_id) and mode in [:auto, :manual] do
params = %{"mode" => mode, "value" => 0}

patch(token, order_id, params)
end

@doc since: "0.1.1"
@spec change_launch_mode(token :: %Token{}, order_id :: p(), mode :: :delay, value :: p()) ::
t()
def change_launch_mode(token, order_id, mode, value)
when is_valid_token(token) and is_gt(order_id) and is_gt(value) and mode == :delay do
params = %{"mode" => "delay", "value" => 0}

patch(token, order_id, params)
end

@doc """
Check the Task Status
Expand Down Expand Up @@ -264,6 +424,12 @@ defmodule Reydenx.Action do
Client.get(token, "#{@prefix}/#{order_id}/task/#{task_id}/status/", TaskStatus)
end

@doc false
defp patch(token, order_id, params) do
Client.patch(token, "#{@prefix}/#{order_id}/action/change/launch/", params, ActionResult)
|> Response.handler(Task)
end

@doc false
defp r(token, order_id, path) when is_valid_token(token) and is_gt(order_id) do
Client.patch(token, "#{@prefix}/#{order_id}/#{path}", ActionResult)
Expand Down
23 changes: 18 additions & 5 deletions lib/reydenx/client.ex
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,13 @@ defmodule Reydenx.Client do

@type t ::
{:error, %ResponseError{}} | {:ok, %ActionResult{} | %Result{} | %User{} | %Balance{}}
@type s :: String.t()

@doc """
GET Request
"""
@doc since: "0.1.0"
@spec get(token :: Token, path :: String.t(), to :: Result | any()) :: t()
@spec get(token :: Token, path :: s(), to :: Result | any()) :: t()
def get(token, path, to \\ Result)
when is_valid_token(token) and is_valid_string(path) do
http_client().get("#{@base_url}#{path}", Authorization: "Bearer #{token.access_token}")
Expand All @@ -28,18 +29,30 @@ defmodule Reydenx.Client do
PATCH Request
"""
@doc since: "0.1.0"
@spec patch(token :: Token, path :: String.t(), to :: ActionResult | any()) :: t()
def patch(token, path, to \\ ActionResult)
when is_valid_token(token) and is_valid_string(path) do
@spec patch(token :: Token, path :: s(), to :: ActionResult | any()) :: t()
def patch(token, path, to) when is_valid_token(token) and is_valid_string(path) do
http_client().patch("#{@base_url}#{path}", nil, Authorization: "Bearer #{token.access_token}")
|> Response.handler(path, to)
end

@doc since: "0.1.1"
@spec patch(token :: Token, path :: s(), body :: map(), to :: ActionResult | any()) ::
t()
def patch(token, path, body, to) when is_valid_token(token) and is_valid_string(path) do
{_, body} = Jason.encode(body)

http_client().patch("#{@base_url}#{path}", body, %{
Authorization: "Bearer #{token.access_token}",
"Content-Type": "application/json"
})
|> Response.handler(path, to)
end

@doc """
POST Request
"""
@doc since: "0.1.0"
@spec post(token :: Token, path :: String.t(), body :: map()) :: t()
@spec post(token :: Token, path :: s(), body :: map()) :: t()
def post(token, path, body)
when is_valid_token(token) and is_valid_string(path) and is_map(body) do
{_, body} = Jason.encode(body)
Expand Down
4 changes: 1 addition & 3 deletions lib/reydenx/model/action_result.ex
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
defmodule Reydenx.Model.ActionResult do
alias Reydenx.Model.Task

@derive {Jason.Encoder, only: [:request_id, :order_id, :action, :value, :task]}
defstruct [
:request_id,
:order_id,
:action,
:value,
task: %Task{}
:task
]
end
3 changes: 3 additions & 0 deletions lib/reydenx/response.ex
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ defmodule Reydenx.Response do
@unauthorized 401
@forbidden 403
@not_found 404
@method_not_allowed 405
@unprocessable_entity 422
@too_many_requests 429
@internal_error 500
Expand All @@ -16,6 +17,7 @@ defmodule Reydenx.Response do
@unauthorized,
@forbidden,
@not_found,
@method_not_allowed,
@unprocessable_entity,
@internal_error
]
Expand All @@ -24,6 +26,7 @@ defmodule Reydenx.Response do
"#{@unauthorized}" => "Unautorized",
"#{@forbidden}" => "Forbidden",
"#{@not_found}" => "Not Found",
"#{@method_not_allowed}" => "Method Not Allowed",
"#{@unprocessable_entity}" => "Unprocessable Entity",
"#{@internal_error}" => "Internal Error"
}
Expand Down
9 changes: 4 additions & 5 deletions lib/reydenx/utils.ex
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
defmodule Reydenx.Utils do
alias Reydenx.Model.{MinMaxStep, Task}
alias Reydenx.Model.MinMaxStep

@doc since: "0.1.0"
@spec http_client() :: HTTPoison
def http_client do
Application.get_env(:reydenx, :http_client)
end

@doc false
def map({k, v}) do
case k do
"views" ->
Expand All @@ -19,10 +22,6 @@ defmodule Reydenx.Utils do
{String.to_atom(k),
struct(MinMaxStep, Enum.map(v, fn {k1, v1} -> {String.to_atom(k1), v1} end))}

"task" ->
{String.to_atom(k),
struct(Task, Enum.map(v, fn {k1, v1} -> {String.to_atom(k1), v1} end))}

"created_at" ->
{String.to_atom(k), str_to_datetime(v)}

Expand Down
2 changes: 1 addition & 1 deletion mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ defmodule Reydenx.MixProject do
use Mix.Project

@source_url "https://github.com/pixel365/reydenx_elixir"
@version "0.1.0"
@version "0.1.1"

def project do
[
Expand Down

0 comments on commit 818c82b

Please sign in to comment.