Class: Line::Bot::V2::HttpClient
- Inherits:
-
Object
- Object
- Line::Bot::V2::HttpClient
- Defined in:
- lib/line/bot/v2/http_client.rb,
sig/line/bot/v2/http_client.rbs
Overview
This class is not intended for line-bot-sdk-ruby users. Breaking changes may occur; use at your own risk.
Instance Method Summary collapse
- #build_form_request(http_class:, path:, query_params:, form_params:, headers:) ⇒ Object
- #build_headers(headers: nil) ⇒ Object
- #build_multipart_request(http_class:, path:, query_params:, form_params:, headers:) ⇒ Object
- #build_request(http_class:, path:, query_params:, headers:, body_params: nil) ⇒ Object
- #build_url(path:, query_params:) ⇒ Object
-
#delete(path:, query_params: nil, headers: nil) ⇒ Object
NOTE: line-bot-sdk-ruby users should not use this.
- #determine_content_type(file:) ⇒ Object
-
#get(path:, query_params: nil, headers: nil) ⇒ Object
NOTE: line-bot-sdk-ruby users should not use this.
-
#initialize(base_url:, http_headers: {}, http_options: {}) ⇒ HttpClient
constructor
Initializes a new HttpClient instance.
- #normalize_headers(headers:) ⇒ Object
- #perform_request(request:) ⇒ Object
-
#post(path:, query_params: nil, body_params: nil, headers: nil) ⇒ Object
NOTE: line-bot-sdk-ruby users should not use this.
-
#post_form(path:, query_params: nil, form_params: nil, headers: nil) ⇒ Object
NOTE: line-bot-sdk-ruby users should not use this.
-
#post_form_multipart(path:, query_params: nil, form_params: nil, headers: nil) ⇒ Object
NOTE: line-bot-sdk-ruby users should not use this.
-
#put(path:, query_params: nil, body_params: nil, headers: nil) ⇒ Object
NOTE: line-bot-sdk-ruby users should not use this.
-
#put_form_multipart(path:, query_params: nil, form_params: nil, headers: nil) ⇒ Object
NOTE: line-bot-sdk-ruby users should not use this.
Constructor Details
#initialize(base_url:, http_headers: {}, http_options: {}) ⇒ HttpClient
Initializes a new HttpClient instance.
https://docs.ruby-lang.org/en/3.4/Net/HTTP.html#method-i-options
NOTE: line-bot-sdk-ruby users should not use this. Breaking changes may occur, so use at your own risk.
21 22 23 24 25 |
# File 'lib/line/bot/v2/http_client.rb', line 21 def initialize(base_url:, http_headers: {}, http_options: {}) @base_url = base_url @http_headers = { 'User-Agent': "LINE-BotSDK-Ruby/#{Line::Bot::VERSION}" }.merge(normalize_headers(headers: http_headers)) @http_options = end |
Instance Method Details
#build_form_request(http_class:, path:, query_params:, form_params:, headers:) ⇒ Object
93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/line/bot/v2/http_client.rb', line 93 def build_form_request(http_class:, path:, query_params:, form_params:, headers:) request_url = build_url(path: path, query_params: query_params) request_headers = build_headers(headers: headers) request = http_class.new(request_url, request_headers) if form_params request['Content-Type'] = 'application/x-www-form-urlencoded' request.body = URI.encode_www_form(Line::Bot::V2::Utils.deep_to_hash(form_params)) end request end |
#build_headers(headers: nil) ⇒ Object
124 125 126 127 128 |
# File 'lib/line/bot/v2/http_client.rb', line 124 def build_headers(headers: nil) return @http_headers if headers.nil? @http_headers.merge(normalize_headers(headers: headers)) end |
#build_multipart_request(http_class:, path:, query_params:, form_params:, headers:) ⇒ Object
106 107 108 109 110 111 112 113 114 115 116 |
# File 'lib/line/bot/v2/http_client.rb', line 106 def build_multipart_request(http_class:, path:, query_params:, form_params:, headers:) request_url = build_url(path: path, query_params: query_params) request_headers = build_headers(headers: headers) file_params, non_file_params = form_params.partition { |_, value| value.is_a?(File) }.map(&:to_h) # steep:ignore NoMethod params = Line::Bot::V2::Utils.deep_to_hash(non_file_params).merge( file_params.transform_values { |value| UploadIO.new(value, 'text/plain', File.basename(value.path)) } ) http_class.new(request_url, params, request_headers) end |
#build_request(http_class:, path:, query_params:, headers:, body_params: nil) ⇒ Object
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/line/bot/v2/http_client.rb', line 71 def build_request(http_class:, path:, query_params:, headers:, body_params: nil) request_url = build_url(path: path, query_params: query_params) request_headers = build_headers(headers: headers) request = http_class.new(request_url, request_headers) if body_params if body_params.is_a?(File) request['Content-Type'] = determine_content_type(file: body_params) request.body = body_params.read else request['Content-Type'] = 'application/json' request.body = Line::Bot::V2::Utils.deep_compact( Line::Bot::V2::Utils.deep_camelize( Line::Bot::V2::Utils.deep_to_hash(body_params) ) ).to_json end end request end |
#build_url(path:, query_params:) ⇒ Object
118 119 120 121 122 |
# File 'lib/line/bot/v2/http_client.rb', line 118 def build_url(path:, query_params:) uri = URI.join(@base_url, path) uri.query = URI.encode_www_form(query_params) unless query_params.nil? uri end |
#delete(path:, query_params: nil, headers: nil) ⇒ Object
NOTE: line-bot-sdk-ruby users should not use this. Breaking changes may occur, so use at your own risk.
46 47 48 49 |
# File 'lib/line/bot/v2/http_client.rb', line 46 def delete(path:, query_params: nil, headers: nil) request = build_request(http_class: Net::HTTP::Delete, path: path, query_params: query_params, headers: headers) perform_request(request: request) end |
#determine_content_type(file:) ⇒ Object
140 141 142 143 144 145 146 147 148 149 150 151 152 153 |
# File 'lib/line/bot/v2/http_client.rb', line 140 def determine_content_type(file:) case File.extname(path=file.path).downcase when '.txt' 'text/plain' when '.jpg', '.jpeg' 'image/jpeg' when '.png' 'image/png' when '.gif' 'image/gif' else 'application/octet-stream' end end |
#get(path:, query_params: nil, headers: nil) ⇒ Object
NOTE: line-bot-sdk-ruby users should not use this. Breaking changes may occur, so use at your own risk.
28 29 30 31 |
# File 'lib/line/bot/v2/http_client.rb', line 28 def get(path:, query_params: nil, headers: nil) request = build_request(http_class: Net::HTTP::Get, path: path, query_params: query_params, headers: headers) perform_request(request: request) end |
#normalize_headers(headers:) ⇒ Object
155 156 157 158 159 160 161 |
# File 'lib/line/bot/v2/http_client.rb', line 155 def normalize_headers(headers:) return {} if headers.nil? headers .transform_keys(&:to_sym) .transform_values(&:to_s) end |
#perform_request(request:) ⇒ Object
130 131 132 133 134 135 136 137 138 |
# File 'lib/line/bot/v2/http_client.rb', line 130 def perform_request(request:) Net::HTTP.start(request.uri.hostname, request.uri.port, use_ssl: request.uri.scheme == 'https') do |http| # steep:ignore ArgumentTypeMismatch @http_options.each do |key, value| http.send("#{key}=", value) end if @http_options http.request(request) end end |
#post(path:, query_params: nil, body_params: nil, headers: nil) ⇒ Object
NOTE: line-bot-sdk-ruby users should not use this. Breaking changes may occur, so use at your own risk.
34 35 36 37 |
# File 'lib/line/bot/v2/http_client.rb', line 34 def post(path:, query_params: nil, body_params: nil, headers: nil) request = build_request(http_class: Net::HTTP::Post, path: path, query_params: query_params, headers: headers, body_params: body_params) perform_request(request: request) end |
#post_form(path:, query_params: nil, form_params: nil, headers: nil) ⇒ Object
NOTE: line-bot-sdk-ruby users should not use this. Breaking changes may occur, so use at your own risk.
52 53 54 55 |
# File 'lib/line/bot/v2/http_client.rb', line 52 def post_form(path:, query_params: nil, form_params: nil, headers: nil) request = build_form_request(http_class: Net::HTTP::Post, path: path, query_params: query_params, form_params: form_params, headers: headers) perform_request(request: request) end |
#post_form_multipart(path:, query_params: nil, form_params: nil, headers: nil) ⇒ Object
NOTE: line-bot-sdk-ruby users should not use this. Breaking changes may occur, so use at your own risk.
58 59 60 61 |
# File 'lib/line/bot/v2/http_client.rb', line 58 def post_form_multipart(path:, query_params: nil, form_params: nil, headers: nil) request = build_multipart_request(http_class: Net::HTTP::Post::Multipart, path: path, query_params: query_params, form_params: form_params, headers: headers) perform_request(request: request) end |
#put(path:, query_params: nil, body_params: nil, headers: nil) ⇒ Object
NOTE: line-bot-sdk-ruby users should not use this. Breaking changes may occur, so use at your own risk.
40 41 42 43 |
# File 'lib/line/bot/v2/http_client.rb', line 40 def put(path:, query_params: nil, body_params: nil, headers: nil) request = build_request(http_class: Net::HTTP::Put, path: path, query_params: query_params, headers: headers, body_params: body_params) perform_request(request: request) end |
#put_form_multipart(path:, query_params: nil, form_params: nil, headers: nil) ⇒ Object
NOTE: line-bot-sdk-ruby users should not use this. Breaking changes may occur, so use at your own risk.
64 65 66 67 |
# File 'lib/line/bot/v2/http_client.rb', line 64 def put_form_multipart(path:, query_params: nil, form_params: nil, headers: nil) request = build_multipart_request(http_class: Net::HTTP::Put::Multipart, path: path, query_params: query_params, form_params: form_params, headers: headers) perform_request(request: request) end |