File: endpoint.rb

package info (click to toggle)
ruby-aws-sdk-core 3.212.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,232 kB
  • sloc: ruby: 17,533; makefile: 4
file content (98 lines) | stat: -rw-r--r-- 3,042 bytes parent folder | download
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
# frozen_string_literal: true

require 'uri'

module Aws
  module Rest
    module Request
      class Endpoint

        # @param [Seahorse::Model::Shapes::ShapeRef] rules
        # @param [String] request_uri_pattern
        def initialize(rules, request_uri_pattern)
          @rules = rules
          request_uri_pattern.split('?').tap do |path_part, query_part|
            @path_pattern = path_part
            @query_prefix = query_part
          end
        end

        # @param [URI::HTTPS,URI::HTTP] base_uri
        # @param [Hash,Struct] params
        # @return [URI::HTTPS,URI::HTTP]
        def uri(base_uri, params)
          uri = URI.parse(base_uri.to_s)
          apply_path_params(uri, params)
          apply_querystring_params(uri, params)
          uri
        end

        private

        def apply_path_params(uri, params)
          path = uri.path.sub(%r{/$}, '')
          # handle trailing slash
          path += @path_pattern.split('?')[0] if path.empty? || @path_pattern != '/'
          uri.path = path.gsub(/{.+?}/) do |placeholder|
            param_value_for_placeholder(placeholder, params)
          end
        end

        def param_value_for_placeholder(placeholder, params)
          name = param_name(placeholder)
          param_shape = @rules.shape.member(name).shape
          value =
            case param_shape
            when Seahorse::Model::Shapes::TimestampShape
              timestamp(param_shape, params[name]).to_s
            else
              params[name].to_s
            end

          raise ArgumentError, ":#{name} must not be blank" if value.empty?

          if placeholder.include?('+')
            value.gsub(%r{[^/]+}) { |v| escape(v) }
          else
            escape(value)
          end
        end

        def param_name(placeholder)
          location_name = placeholder.gsub(/[{}+]/, '')
          param_name, _ = @rules.shape.member_by_location_name(location_name)
          param_name
        end

        def timestamp(ref, value)
          case ref['timestampFormat']
          when 'unixTimestamp' then value.to_i
          when 'rfc822' then value.utc.httpdate
          else
            # serializing as RFC 3399 date-time is the default
            value.utc.iso8601
          end
        end

        def apply_querystring_params(uri, params)
          # collect params that are supposed to be part of the query string
          parts = @rules.shape.members.inject([]) do |prts, (member_name, member_ref)|
            if member_ref.location == 'querystring' && !params[member_name].nil?
              prts << [member_ref, params[member_name]]
            end
            prts
          end
          querystring = QuerystringBuilder.new.build(parts)
          querystring = [@query_prefix, querystring == '' ? nil : querystring].compact.join('&')
          querystring = nil if querystring == ''
          uri.query = querystring
        end

        def escape(string)
          Seahorse::Util.uri_escape(string)
        end

      end
    end
  end
end