1
|
|
|
require "certifi" |
2
|
|
|
require "httparty" |
3
|
|
|
|
4
|
|
|
module Sandcage |
5
|
|
|
API_ROOT = "https://api.sandcage.com/" |
6
|
|
|
API_VERSION = "0.2" |
7
|
|
|
|
8
|
|
|
class Client |
9
|
|
|
TIMEOUT = 30 |
10
|
|
|
include HTTParty |
11
|
|
|
|
12
|
|
|
# Default connection adapter options |
13
|
|
|
# Specify CA certificate location, which also sets |
14
|
|
|
# http.verify_mode = OpenSSL::SSL::VERIFY_PEER |
15
|
|
|
# see https://github.com/jnunemaker/httparty/blob/master/lib/httparty/connection_adapter.rb |
16
|
|
|
ssl_ca_file Certifi.where |
17
|
|
|
default_timeout TIMEOUT |
18
|
|
|
headers "User-Agent" => "SandCage - #{Sandcage::API_VERSION}" |
19
|
|
|
|
20
|
|
|
# Give your SandCage API Key as a constructor parameter |
21
|
|
|
# This can be retrieved from https://www.sandcage.com/panel/api_key |
22
|
|
|
def initialize(api_key=nil) |
23
|
|
|
@api_key = api_key |
24
|
|
|
@sandcage_api_endpoint = "#{Sandcage::API_ROOT}/#{Sandcage::API_VERSION}/" |
25
|
|
|
end |
26
|
|
|
|
27
|
|
|
# The call_endpoint method. |
28
|
|
|
# This method sends http request to the endpoint. |
29
|
|
|
# |
30
|
|
|
# First parameter "endpoint" (string) is the url of the endpoint. |
31
|
|
|
# Second parameter "post" (boolean) indicates the http method type. |
32
|
|
|
# true => POST |
33
|
|
|
# false => GET |
34
|
|
|
# Third parameter "data" (hash) is the data to be sent. |
35
|
|
|
# Method's return type is HTTParty::Response object. |
36
|
|
|
def call_endpoint(endpoint, post=false, data=nil) |
37
|
|
|
if post |
38
|
|
|
r = self.class.post(endpoint, body: data.to_json) |
39
|
|
|
else |
40
|
|
|
r = self.class.get(endpoint) |
41
|
|
|
end |
42
|
|
|
end |
43
|
|
|
|
44
|
|
|
# The "get-info" service |
45
|
|
|
# |
46
|
|
|
# First parameter "payload" (hash) contains the data to be sent. |
47
|
|
|
# Method's return type is HTTParty::Response object. |
48
|
|
|
def get_info_service(payload=nil) |
49
|
|
|
call_endpoint("#{@sandcage_api_endpoint}get-info", |
50
|
|
|
post=true, |
51
|
|
|
data=get_post_data(payload)) |
52
|
|
|
end |
53
|
|
|
|
54
|
|
|
# The "list-files" service |
55
|
|
|
# |
56
|
|
|
# First parameter "payload" (dictionary) contains the data to be sent. |
57
|
|
|
# Method's return type is HTTParty::Response object. |
58
|
|
|
def list_files_service(payload=nil) |
59
|
|
|
call_endpoint("#{@sandcage_api_endpoint}list-files", |
60
|
|
|
post=true, |
61
|
|
|
data=get_post_data(payload)) |
62
|
|
|
end |
63
|
|
|
|
64
|
|
|
# The "destroy-files" service |
65
|
|
|
# |
66
|
|
|
# First parameter "payload" (hash) contains the data to be sent. |
67
|
|
|
# Second parameter "callback_endpoint" (string) is the url, where the |
68
|
|
|
# callback should be send to. |
69
|
|
|
# Method's return type is HTTParty::Response object. |
70
|
|
View Code Duplication |
def destroy_files_service(payload=nil, callback_endpoint=nil) |
|
|
|
|
71
|
|
|
data = get_post_data(payload) |
72
|
|
|
update_callback data, callback_endpoint |
73
|
|
|
call_endpoint("#{@sandcage_api_endpoint}destroy-files", |
74
|
|
|
post=true, |
75
|
|
|
data=data) |
76
|
|
|
end |
77
|
|
|
|
78
|
|
|
# The "schedule-tasks" service |
79
|
|
|
# |
80
|
|
|
# First parameter "payload" (hash) contains the data to be sent. |
81
|
|
|
# Second parameter "callback_endpoint" (string) is the url, where the |
82
|
|
|
# callback should be send to. |
83
|
|
|
# Method's return type is HTTParty::Response object. |
84
|
|
View Code Duplication |
def schedule_files_service(payload=nil, callback_endpoint=nil) |
|
|
|
|
85
|
|
|
data = get_post_data(payload) |
86
|
|
|
update_callback data, callback_endpoint |
87
|
|
|
call_endpoint("#{@sandcage_api_endpoint}schedule-tasks", |
88
|
|
|
post=true, |
89
|
|
|
data=data) |
90
|
|
|
end |
91
|
|
|
|
92
|
|
|
private |
93
|
|
|
|
94
|
|
|
# Combine payload with API KEY and return the dictionary. |
95
|
|
|
def get_post_data(payload=nil) |
96
|
|
|
d = { key: @api_key } |
97
|
|
|
if payload |
98
|
|
|
d.update payload |
99
|
|
|
end |
100
|
|
|
return d |
101
|
|
|
end |
102
|
|
|
|
103
|
|
|
# Update data with callback_endpoint address. |
104
|
|
|
def update_callback(data, callback_endpoint=nil) |
105
|
|
|
if callback_endpoint |
106
|
|
|
data[:callback_endpoint] = callback_endpoint |
107
|
|
|
end |
108
|
|
|
end |
109
|
|
|
end |
110
|
|
|
end |
111
|
|
|
|