-
Notifications
You must be signed in to change notification settings - Fork 127
Expand file tree
/
Copy pathapi.pebble
More file actions
194 lines (184 loc) · 10.1 KB
/
api.pebble
File metadata and controls
194 lines (184 loc) · 10.1 KB
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
{# @pebvariable name="operations" type="org.openapitools.codegen.model.OperationMap" -#}
{# @pebvariable name="authMethods" type="java.util.ArrayList<org.openapitools.codegen.CodegenSecurity>" -#}
{# @pebvariable name="packageName" type="String" #}
{%- include "./license.pebble" %}
require 'json'
require 'line/bot/v2/http_client'
require 'line/bot/v2/reserved_words'
require 'line/bot/v2/utils'
module Line
module Bot
module V2
module {{ packageName | camelize }}
class Api{{ operations.classname.indexOf('Blob') != -1 ? 'Blob' : '' }}Client
# Initializes a new {Line::Bot::V2::{{ packageName | camelize }}::Api{{ operations.classname.indexOf('Blob') != -1 ? 'Blob' : '' }}Client} instance.
#
# @param base_url [String] The base URL for requests (optional).
# Defaults to '{{endpoint(operations.classname)}}' if none is provided.
# You can override this for testing or to use a mock server.
{%- if packageName == 'module_attach' %}
# @param channel_id [String] The channel ID for Basic authentication.
# @param channel_secret [String] The channel secret for Basic authentication.
{%- elseif packageName != 'channel_access_token' %}
# @param channel_access_token [String] The channel access token for authorization.
{%- endif %}
# @param http_options [Hash] HTTP options (same as Net::HTTP options).
# See: https://docs.ruby-lang.org/en/3.4/Net/HTTP.html to understand the options.
#
# @example
# @client ||= Line::Bot::V2::{{ packageName | camelize }}::Api{{ operations.classname.indexOf('Blob') != -1 ? 'Blob' : '' }}Client.new(
{%- if packageName == 'module_attach' %}
# channel_id: "YOUR_CHANNEL_ID",
# channel_secret: "YOUR_CHANNEL_SECRET",
{%- elseif packageName != 'channel_access_token' %}
# channel_access_token: "YOUR_CHANNEL_ACCESS_TOKEN",
{%- endif %}
# http_options: {
# open_timeout: 5,
# read_timeout: 5,
# }
# )
def initialize(base_url: nil{% if packageName == 'channel_access_token' %}{% elseif packageName == 'module_attach' %}, channel_id:, channel_secret:{% else %}, channel_access_token:{% endif %}, http_options: {})
@http_client = HttpClient.new(
base_url: base_url || '{{endpoint(operations.classname)}}'{% if packageName == 'channel_access_token' %}{% elseif packageName == 'module_attach' %},
http_headers: {
Authorization: 'Basic ' + Base64.encode64("#{channel_id}:#{channel_secret}")
}{% else %},
http_headers: {
Authorization: "Bearer #{channel_access_token}"
}{% endif %},
http_options: http_options
)
end
{%- for op in operations.operation %}
{% if op.nickname == "issue_stateless_channel_token" -%}
# @deprecated
# This is deprecated.
# Please use {{ '{' }}#issue_stateless_channel_token_by_jwt_assertion_with_http_info} or {{ '{' }}#issue_stateless_channel_token_by_client_secret_with_http_info} instead.
#
{% endif -%}
# {{ op.notes }}
# This requests to <code>{{ op.httpMethod }} {{ endpoint(operations.classname) }}{{ op.path }}</code>
# This returns an array containing response, HTTP status code, and header in order. Please specify all header keys in lowercase.
#
{% for param in op.allParams -%}
# @param {{param.paramName}} [{{ formatDataType(param) }}{{ param.required ? '' : ', nil' }}] {{param.description}}
{% endfor -%}
{% if op.isDeprecated -%}
# @deprecated
{% endif -%}
{% if op.externalDocs.description != null -%}
# {{op.externalDocs.description}}
{% endif -%}
{% if op.externalDocs.url != null -%}
# @see {{op.externalDocs.url}}
{%- endif %}
{%- for response in op.responses %}
{%- if response.content['application/json'].schema.complexType != null %}
# @return [Array(Line::Bot::V2::{{ packageName | camelize }}::{{ response.content['application/json'].schema.complexType }}, Integer, Hash{String => String})] when HTTP status code is {{ response.code }}
{%- else %}
# @return [Array((String|nil), Integer, Hash{String => String})] when HTTP status code is {{ response.code }}
{%- endif %}
{%- endfor %}
# @return [Array((String|nil), Integer, Hash{String => String})] when other HTTP status code is returned. String is HTTP response body itself.
{#- Remove this steep:ignore after https://github.com/soutaro/steep/issues/1603 is resolved #}
def {{op.nickname}}_with_http_info( # steep:ignore MethodBodyTypeMismatch
{%- for param in op.allParams %}
{{param.paramName}}:{{ param.required ? '' : ' nil' }}{{ loop.last ? '' : ',' -}}
{%- endfor %}
)
path = "{{ op.path }}"{% for param in op.pathParams %}
.gsub(/{{ "{" + param.baseName + "}" }}/, {{ param.paramName }}.to_s)
{%- endfor %}{% if op.hasFormParams %}
form_params = {{ '{' }}{% for param in op.formParams %}
"{{ param.baseName }}": {{ param.paramName }}{{ loop.last ? '' : ',' -}}
{% endfor %}
}.compact{% elseif op.hasQueryParams %}
query_params = {{ '{' }}{% for param in op.queryParams %}
{%- if param.isArray %}
"{{ param.baseName }}": {{ param.paramName }}&.join(','){{ loop.last ? '' : ',' -}}
{%- else %}
"{{ param.baseName }}": {{ param.paramName }}{{ loop.last ? '' : ',' -}}
{%- endif -%}
{% endfor %}
}.compact{% endif %}{% if op.hasHeaderParams %}
header_params = {{ '{' }}{% for param in op.headerParams %}
"{{ param.baseName}}": {{ param.paramName }}{{ loop.last ? '' : ',' -}}
{% endfor %}
}.compact{% endif %}
response = @http_client.{{ op.httpMethod | lower }}{% if op.hasFormParams %}_form{% endif %}{% if op.isMultipart %}_multipart{% endif %}(
path: path,{% if op.hasBodyParam %}
body_params: {{ op.bodyParam.paramName }},{% elseif op.hasFormParams %}
form_params: form_params,{% elseif op.hasQueryParams %}
query_params: query_params,{% endif %}{% if op.hasHeaderParams %}
headers: header_params{% endif %}
)
case response.code.to_i
{%- for response in op.responses %}
when {{ response.code }}
{%- if response.content['application/json'].schema.complexType != null %}
json = Line::Bot::V2::Utils.deep_underscore(JSON.parse(response.body))
json.transform_keys! do |key|
Line::Bot::V2::RESERVED_WORDS.include?(key) ? "_#{key}".to_sym : key
end
response_body = Line::Bot::V2::{{ packageName | camelize }}::{{ response.content['application/json'].schema.complexType }}.create(json) # steep:ignore InsufficientKeywordArguments
[response_body, {{ response.code }}, response.each_header.to_h]
{%- else %}
[response.body, {{ response.code }}, response.each_header.to_h]
{%- endif %}
{%- endfor %}
else
[response.body, response.code.to_i, response.each_header.to_h]
end
end
{% if op.nickname == "issue_stateless_channel_token" -%}
# @deprecated
# This is deprecated.
# Please use {{ '{' }}#issue_stateless_channel_token_by_jwt_assertion} or {{ '{' }}#issue_stateless_channel_token_by_client_secret} instead.
#
{% endif -%}
# {{ op.notes }}
# This requests to <code>{{ op.httpMethod }} {{ endpoint(operations.classname) }}{{ op.path }}</code>
# When you want to get HTTP status code or response headers, use {{ '{#' }}{{op.nickname}}_with_http_info} instead of this.
#
{% for param in op.allParams -%}
# @param {{param.paramName}} [{{ formatDataType(param) }}{{ param.required ? '' : ', nil' }}] {{param.description}}
{% endfor -%}
{% if op.isDeprecated -%}
# @deprecated
{% endif -%}
{% if op.externalDocs.description != null -%}
# {{op.externalDocs.description}}
{% endif -%}
{% if op.externalDocs.url != null -%}
# @see {{op.externalDocs.url}}
{%- endif %}
{%- for response in op.responses %}
{%- if response.content['application/json'].schema.complexType != null %}
# @return [Line::Bot::V2::{{ packageName | camelize }}::{{ response.content['application/json'].schema.complexType }}] when HTTP status code is {{ response.code }}
{%- else %}
# @return [String, nil] when HTTP status code is {{ response.code }}
{%- endif %}
{%- endfor %}
# @return [String, nil] when other HTTP status code is returned. This String is HTTP response body itself.
def {{op.nickname}}({% for param in op.allParams %}
{{param.paramName}}:{{ param.required ? '' : ' nil' }}{{ loop.last ? '' : ',' -}}
{% endfor %}
)
response_body, _status_code, _headers = {{op.nickname}}_with_http_info({% if op.nickname == "issue_stateless_channel_token" %} # steep:ignore DeprecatedReference{% endif %}{% for param in op.allParams %}
{{param.paramName}}: {{param.paramName}}{{ loop.last ? '' : ',' -}}
{% endfor %}
)
response_body
end{% endfor %}
{%- if ["ChannelAccessTokenClient"] contains operations.classname %}
{% include "./body/api/" + operations.classname + ".rb" %}
{%- endif %}
end
end
end
end
end
{%- macro formatDataType(param) %}
{%- if param.isArray -%}Array[{{ param.items.dataType }}]{%- else -%}{{ param.dataType }}{%- endif -%}
{%- endmacro %}