-
Notifications
You must be signed in to change notification settings - Fork 468
/
admin.rb
129 lines (117 loc) · 4.39 KB
/
admin.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# typed: strict
# frozen_string_literal: true
module ShopifyAPI
module Clients
module Rest
class Admin < HttpClient
extend T::Sig
sig { params(session: T.nilable(Auth::Session), api_version: T.nilable(String)).void }
def initialize(session: nil, api_version: nil)
@api_version = T.let(api_version || Context.api_version, String)
if api_version
if api_version == Context.api_version
Context.logger.debug("Rest client has a redundant API version override "\
"to the default #{Context.api_version}")
else
Context.logger.debug("Rest client overriding default API version "\
"#{Context.api_version} with #{api_version}")
end
end
super(session: session, base_path: "/admin/api/#{@api_version}")
end
sig do
params(
path: String,
body: T.nilable(T::Hash[T.any(Symbol, String), T.untyped]),
query: T.nilable(T::Hash[T.any(Symbol, String), T.untyped]),
headers: T.nilable(T::Hash[T.any(Symbol, String), T.untyped]),
tries: T.nilable(Integer),
).returns(HttpResponse)
end
def get(path:, body: nil, query: nil, headers: nil, tries: 1)
request(
make_request(http_method: :get, path: path, body: body, query: query, headers: headers,
tries: T.must(tries)),
)
end
sig do
params(
path: String,
body: T.nilable(T::Hash[T.any(Symbol, String), T.untyped]),
query: T.nilable(T::Hash[T.any(Symbol, String), T.untyped]),
headers: T.nilable(T::Hash[T.any(Symbol, String), T.untyped]),
tries: T.nilable(Integer),
).returns(HttpResponse)
end
def delete(path:, body: nil, query: nil, headers: nil, tries: 1)
request(
make_request(http_method: :delete, path: path, body: body, query: query, headers: headers,
tries: T.must(tries)),
)
end
sig do
params(
path: String,
body: T::Hash[T.any(Symbol, String), T.untyped],
query: T.nilable(T::Hash[T.any(Symbol, String), T.untyped]),
headers: T.nilable(T::Hash[T.any(Symbol, String), T.untyped]),
tries: T.nilable(Integer),
).returns(HttpResponse)
end
def put(path:, body:, query: nil, headers: nil, tries: 1)
request(
make_request(http_method: :put, path: path, body: body, query: query, headers: headers,
tries: T.must(tries)),
)
end
sig do
params(
path: String,
body: T::Hash[T.any(Symbol, String), T.untyped],
query: T.nilable(T::Hash[T.any(Symbol, String), T.untyped]),
headers: T.nilable(T::Hash[T.any(Symbol, String), T.untyped]),
tries: T.nilable(Integer),
).returns(HttpResponse)
end
def post(path:, body:, query: nil, headers: nil, tries: 1)
request(
make_request(http_method: :post, path: path, body: body, query: query, headers: headers,
tries: T.must(tries)),
)
end
protected
sig { params(request: HttpRequest).returns(String) }
def request_url(request)
request_path = request.path.sub(%r/\A\//, "").sub(/\.json\z/, "") + ".json"
if request_path.start_with?("admin/")
"#{@base_uri}/#{request_path}"
else
"#{@base_uri_and_path}/#{request_path}"
end
end
private
sig do
params(
http_method: Symbol,
path: String,
body: T.nilable(T::Hash[T.any(Symbol, String), T.untyped]),
query: T.nilable(T::Hash[T.any(Symbol, String), T.untyped]),
headers: T.nilable(T::Hash[T.any(Symbol, String), T.untyped]),
tries: Integer,
).returns(HttpRequest)
end
def make_request(http_method:, path:, body:, query:, headers:, tries:)
HttpRequest.new(
http_method: http_method,
path: path,
body: body,
query: query,
extra_headers: headers,
body_type: body.nil? ? nil : "application/json",
tries: tries,
)
end
end
end
end
end