Class: ShedCloud::PartnerApi::HttpClient
- Inherits:
-
Object
- Object
- ShedCloud::PartnerApi::HttpClient
- Defined in:
- lib/shedcloud/partner_api/http_client.rb
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(base_url:, get_access_token:, transport: HttpTransport.new, user_agent: 'shedcloud-ruby/partner-api') ⇒ HttpClient
constructor
A new instance of HttpClient.
- #request(method, path, query: nil, body: nil, headers: {}, skip_auth: false, content_type: 'application/json') ⇒ Object
Constructor Details
#initialize(base_url:, get_access_token:, transport: HttpTransport.new, user_agent: 'shedcloud-ruby/partner-api') ⇒ HttpClient
Returns a new instance of HttpClient.
9 10 11 12 13 14 |
# File 'lib/shedcloud/partner_api/http_client.rb', line 9 def initialize(base_url:, get_access_token:, transport: HttpTransport.new, user_agent: 'shedcloud-ruby/partner-api') @base_url = Hosts.trim_trailing_slashes(base_url) @get_access_token = get_access_token @transport = transport @user_agent = user_agent end |
Class Method Details
.encode_query(params) ⇒ Object
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/shedcloud/partner_api/http_client.rb', line 61 def self.encode_query(params) out = {} params.each do |key, value| key = key.to_s if value.is_a?(QueryValue) out[key] = format_query_value(value.value) next end next if value.nil? || value == '' || value == false || value == 0 || value == 0.0 || value == [] if value.is_a?(Array) out[key] = value.map { |item| format_query_value(item) }.join(',') next end out[key] = format_query_value(value) end URI.encode_www_form(out) end |
.format_query_value(value) ⇒ Object
85 86 87 88 89 90 |
# File 'lib/shedcloud/partner_api/http_client.rb', line 85 def self.format_query_value(value) case value when TrueClass, FalseClass then value ? 'true' : 'false' else value.to_s end end |
Instance Method Details
#request(method, path, query: nil, body: nil, headers: {}, skip_auth: false, content_type: 'application/json') ⇒ Object
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 |
# File 'lib/shedcloud/partner_api/http_client.rb', line 16 def request(method, path, query: nil, body: nil, headers: {}, skip_auth: false, content_type: 'application/json') url = @base_url + path if query && !query.empty? encoded = self.class.encode_query(query) url += "?#{encoded}" unless encoded.empty? end request_headers = { 'Accept' => 'application/json', 'User-Agent' => @user_agent }.merge(headers) payload = nil unless body.nil? payload = if content_type == 'application/x-www-form-urlencoded' URI.encode_www_form(body) elsif body.empty? '{}' else JSON.generate(body) end request_headers['Content-Type'] = content_type if content_type end unless skip_auth token = @get_access_token.call raise PartnerApiError.new('no access token available', status: 401) if token.to_s.empty? request_headers['Authorization'] = "Bearer #{token}" end response = @transport.send(method, url, request_headers, payload) parsed = parse_body(response[:body]) if response[:status] < 200 || response[:status] >= 300 , code = extract_error(parsed, response[:status].to_s) raise PartnerApiError.new(, status: response[:status], body: parsed, code: code) end return nil if parsed.nil? raise PartnerApiError.new('partner-api: decode response: expected JSON object', status: 500) unless parsed.is_a?(Hash) parsed end |