# Generated by API Builder - https://www.apibuilder.io # Service version: 0.16.74 # User agent: apibuilder app.apibuilder.io/apicollective/apibuilder-postman-collection-v2-1/0.16.74/ruby_client require 'cgi' require 'net/http' require 'net/https' require 'uri' require 'base64' require 'date' require 'rubygems' require 'json' require 'bigdecimal' # Specification of Postman Collection Format v2.1 using the ApiBuilder schema. # Further information about Postman Collection Format v2.1 can be found at # https://schema.getpostman.com/json/collection/v2.1.0/docs/index.html module Io module Apibuilder module Postman module Collection module V21 module V0 class Client module Constants BASE_URL = 'https://www.getpostman.com' unless defined?(Constants::BASE_URL) NAMESPACE = 'io.apibuilder.postman.collection.v21.v0' unless defined?(Constants::NAMESPACE) USER_AGENT = 'apibuilder app.apibuilder.io/apicollective/apibuilder-postman-collection-v2-1/0.16.74/ruby_client' unless defined?(Constants::USER_AGENT) VERSION = '0.16.74' unless defined?(Constants::VERSION) VERSION_MAJOR = 0 unless defined?(VERSION_MAJOR) end attr_reader :url def initialize(url, opts={}) @url = HttpClient::Preconditions.assert_class('url', url, String) @base_url = URI(url) @authorization = HttpClient::Preconditions.assert_class_or_nil('authorization', opts.delete(:authorization), HttpClient::Authorization) @default_headers = HttpClient::Preconditions.assert_class('default_headers', opts.delete(:default_headers) || {}, Hash) @http_handler = opts.delete(:http_handler) || HttpClient::DefaultHttpHandler.new HttpClient::Preconditions.assert_empty_opts(opts) HttpClient::Preconditions.check_state(url.match(/http.+/i), "URL[%s] must start with http" % url) end # Creates an instance of the client using the base url specified in the API spec. def Client.at_base_url(opts={}) Client.new(Constants::BASE_URL, opts) end def request(path=nil) HttpClient::Preconditions.assert_class_or_nil('path', path, String) request = HttpClient::Request.new(@http_handler, @base_url, path.to_s).with_header('User-Agent', Constants::USER_AGENT).with_header('X-Apidoc-Version', Constants::VERSION).with_header('X-Apidoc-Version-Major', Constants::VERSION_MAJOR) @default_headers.each do |key, value| request = request.with_header(key, value) end if @authorization request = request.with_auth(@authorization) end request end end module Clients end module Models class Items module Types ITEM = 'item' unless defined?(ITEM) FOLDER = 'folder' unless defined?(FOLDER) end attr_reader :type def initialize(incoming={}) opts = HttpClient::Helper.symbolize_keys(incoming) HttpClient::Preconditions.require_keys(opts, [:type], 'Items') @type = HttpClient::Preconditions.assert_class('type', opts.delete(:type), String) end def subtype_to_hash raise 'Cannot serialize an instance of items directly - must use one of the specific types: item, folder' end def to_hash subtype_to_hash.merge(:type => @type) end def Items.from_json(hash) HttpClient::Preconditions.assert_class('hash', hash, Hash) discriminator = HttpClient::Helper.symbolize_keys(hash)[:type].to_s.strip if discriminator.empty? raise "Union type[items] requires a field named 'type'" end case discriminator when Types::ITEM; Item.new(hash) when Types::FOLDER; Folder.new(hash) else ItemsUndefinedType.new(:type => discriminator) end end end class ItemsUndefinedType < Items attr_reader :name def initialize(incoming={}) super(:type => 'undefined_type') opts = HttpClient::Helper.symbolize_keys(incoming) @name = HttpClient::Preconditions.assert_class('name', opts.delete(:type), String) end def subtype_to_hash raise 'Unable to serialize undefined type to json' end def copy(incoming={}) raise 'Operation not supported for undefined type' end def to_hash raise 'Operation not supported for undefined type' end end class AuthEnum attr_reader :value def initialize(value) @value = HttpClient::Preconditions.assert_class('value', value, String) end # Returns the instance of AuthEnum for this value, creating a new instance for an unknown value def AuthEnum.apply(value) if value.instance_of?(AuthEnum) value else HttpClient::Preconditions.assert_class_or_nil('value', value, String) value.nil? ? nil : (from_string(value) || AuthEnum.new(value)) end end # Returns the instance of AuthEnum for this value, or nil if not found def AuthEnum.from_string(value) HttpClient::Preconditions.assert_class('value', value, String) AuthEnum.ALL.find { |v| v.value == value } end def AuthEnum.ALL @@all ||= [AuthEnum.awsv4, AuthEnum.basic, AuthEnum.bearer, AuthEnum.digest, AuthEnum.hawk, AuthEnum.noauth, AuthEnum.oauth1, AuthEnum.oauth2, AuthEnum.ntlm] end def AuthEnum.awsv4 @@_awsv4 ||= AuthEnum.new('awsv4') end def AuthEnum.basic @@_basic ||= AuthEnum.new('basic') end def AuthEnum.bearer @@_bearer ||= AuthEnum.new('bearer') end def AuthEnum.digest @@_digest ||= AuthEnum.new('digest') end def AuthEnum.hawk @@_hawk ||= AuthEnum.new('hawk') end def AuthEnum.noauth @@_noauth ||= AuthEnum.new('noauth') end def AuthEnum.oauth1 @@_oauth1 ||= AuthEnum.new('oauth1') end def AuthEnum.oauth2 @@_oauth2 ||= AuthEnum.new('oauth2') end def AuthEnum.ntlm @@_ntlm ||= AuthEnum.new('ntlm') end def to_hash value end end class BodyMode attr_reader :value def initialize(value) @value = HttpClient::Preconditions.assert_class('value', value, String) end # Returns the instance of BodyMode for this value, creating a new instance for an unknown value def BodyMode.apply(value) if value.instance_of?(BodyMode) value else HttpClient::Preconditions.assert_class_or_nil('value', value, String) value.nil? ? nil : (from_string(value) || BodyMode.new(value)) end end # Returns the instance of BodyMode for this value, or nil if not found def BodyMode.from_string(value) HttpClient::Preconditions.assert_class('value', value, String) BodyMode.ALL.find { |v| v.value == value } end def BodyMode.ALL @@all ||= [BodyMode.raw, BodyMode.urlencoded, BodyMode.formdata, BodyMode.file] end def BodyMode.raw @@_raw ||= BodyMode.new('raw') end def BodyMode.urlencoded @@_urlencoded ||= BodyMode.new('urlencoded') end def BodyMode.formdata @@_formdata ||= BodyMode.new('formdata') end def BodyMode.file @@_file ||= BodyMode.new('file') end def to_hash value end end class EventType attr_reader :value def initialize(value) @value = HttpClient::Preconditions.assert_class('value', value, String) end # Returns the instance of EventType for this value, creating a new instance for an unknown value def EventType.apply(value) if value.instance_of?(EventType) value else HttpClient::Preconditions.assert_class_or_nil('value', value, String) value.nil? ? nil : (from_string(value) || EventType.new(value)) end end # Returns the instance of EventType for this value, or nil if not found def EventType.from_string(value) HttpClient::Preconditions.assert_class('value', value, String) EventType.ALL.find { |v| v.value == value } end def EventType.ALL @@all ||= [EventType.test, EventType.prerequest] end def EventType.test @@_test ||= EventType.new('test') end def EventType.prerequest @@_prerequest ||= EventType.new('prerequest') end def to_hash value end end class Method attr_reader :value def initialize(value) @value = HttpClient::Preconditions.assert_class('value', value, String) end # Returns the instance of Method for this value, creating a new instance for an unknown value def Method.apply(value) if value.instance_of?(Method) value else HttpClient::Preconditions.assert_class_or_nil('value', value, String) value.nil? ? nil : (from_string(value) || Method.new(value)) end end # Returns the instance of Method for this value, or nil if not found def Method.from_string(value) HttpClient::Preconditions.assert_class('value', value, String) Method.ALL.find { |v| v.value == value } end def Method.ALL @@all ||= [Method.get, Method.put, Method.post, Method.patch, Method.delete, Method.copy, Method.head, Method.options, Method.link, Method.unlink, Method.purge, Method.lock, Method.unlock, Method.propfind, Method.view] end def Method.get @@_get ||= Method.new('GET') end def Method.put @@_put ||= Method.new('PUT') end def Method.post @@_post ||= Method.new('POST') end def Method.patch @@_patch ||= Method.new('PATCH') end def Method.delete @@_delete ||= Method.new('DELETE') end def Method.copy @@_copy ||= Method.new('COPY') end def Method.head @@_head ||= Method.new('HEAD') end def Method.options @@_options ||= Method.new('OPTIONS') end def Method.link @@_link ||= Method.new('LINK') end def Method.unlink @@_unlink ||= Method.new('UNLINK') end def Method.purge @@_purge ||= Method.new('PURGE') end def Method.lock @@_lock ||= Method.new('LOCK') end def Method.unlock @@_unlock ||= Method.new('UNLOCK') end def Method.propfind @@_propfind ||= Method.new('PROPFIND') end def Method.view @@_view ||= Method.new('VIEW') end def to_hash value end end class Auth attr_reader :type, :basic def initialize(incoming={}) opts = HttpClient::Helper.symbolize_keys(incoming) HttpClient::Preconditions.require_keys(opts, [:type], 'Auth') @type = (x = opts.delete(:type); x.is_a?(::Io::Apibuilder::Postman::Collection::V21::V0::Models::AuthEnum) ? x : ::Io::Apibuilder::Postman::Collection::V21::V0::Models::AuthEnum.apply(x)) @basic = (x = opts.delete(:basic); x.nil? ? nil : HttpClient::Preconditions.assert_class('basic', x, Array).map { |v| (x = v; x.is_a?(::Io::Apibuilder::Postman::Collection::V21::V0::Models::BasicAuth) ? x : ::Io::Apibuilder::Postman::Collection::V21::V0::Models::BasicAuth.new(x)) }) end def to_json JSON.dump(to_hash) end def copy(incoming={}) Auth.new(to_hash.merge(HttpClient::Helper.symbolize_keys(incoming))) end def to_hash { :type => type.value, :basic => basic.nil? ? nil : basic.map { |o| o.to_hash } } end end class BasicAuth attr_reader :key, :value, :type def initialize(incoming={}) opts = HttpClient::Helper.symbolize_keys(incoming) HttpClient::Preconditions.require_keys(opts, [:key, :value], 'BasicAuth') @key = HttpClient::Preconditions.assert_class('key', opts.delete(:key), String) @value = HttpClient::Preconditions.assert_class('value', opts.delete(:value), String) @type = (x = opts.delete(:type); x.nil? ? nil : HttpClient::Preconditions.assert_class('type', x, String)) end def to_json JSON.dump(to_hash) end def copy(incoming={}) BasicAuth.new(to_hash.merge(HttpClient::Helper.symbolize_keys(incoming))) end def to_hash { :key => key, :value => value, :type => type } end end class Body attr_reader :raw, :mode, :urlencoded, :file, :formdata def initialize(incoming={}) opts = HttpClient::Helper.symbolize_keys(incoming) @raw = (x = opts.delete(:raw); x.nil? ? nil : HttpClient::Preconditions.assert_class('raw', x, String)) @mode = (x = opts.delete(:mode); x.nil? ? nil : (x = x; x.is_a?(::Io::Apibuilder::Postman::Collection::V21::V0::Models::BodyMode) ? x : ::Io::Apibuilder::Postman::Collection::V21::V0::Models::BodyMode.apply(x))) @urlencoded = (x = opts.delete(:urlencoded); x.nil? ? nil : HttpClient::Preconditions.assert_class('urlencoded', x, Array).map { |v| (x = v; x.is_a?(::Io::Apibuilder::Postman::Collection::V21::V0::Models::UrlEncoded) ? x : ::Io::Apibuilder::Postman::Collection::V21::V0::Models::UrlEncoded.new(x)) }) @file = (x = opts.delete(:file); x.nil? ? nil : (x = x; x.is_a?(::Io::Apibuilder::Postman::Collection::V21::V0::Models::File) ? x : ::Io::Apibuilder::Postman::Collection::V21::V0::Models::File.new(x))) @formdata = (x = opts.delete(:formdata); x.nil? ? nil : HttpClient::Preconditions.assert_class('formdata', x, Array).map { |v| (x = v; x.is_a?(::Io::Apibuilder::Postman::Collection::V21::V0::Models::FormParameter) ? x : ::Io::Apibuilder::Postman::Collection::V21::V0::Models::FormParameter.new(x)) }) end def to_json JSON.dump(to_hash) end def copy(incoming={}) Body.new(to_hash.merge(HttpClient::Helper.symbolize_keys(incoming))) end def to_hash { :raw => raw, :mode => mode.nil? ? nil : mode.value, :urlencoded => urlencoded.nil? ? nil : urlencoded.map { |o| o.to_hash }, :file => file.nil? ? nil : file.to_hash, :formdata => formdata.nil? ? nil : formdata.map { |o| o.to_hash } } end end class Collection attr_reader :info, :item, :event, :variable, :auth def initialize(incoming={}) opts = HttpClient::Helper.symbolize_keys(incoming) HttpClient::Preconditions.require_keys(opts, [:info, :item, :event, :variable], 'Collection') @info = (x = opts.delete(:info); x.is_a?(::Io::Apibuilder::Postman::Collection::V21::V0::Models::Info) ? x : ::Io::Apibuilder::Postman::Collection::V21::V0::Models::Info.new(x)) @item = HttpClient::Preconditions.assert_class('item', opts.delete(:item), Array).map { |v| (x = v; x.is_a?(::Io::Apibuilder::Postman::Collection::V21::V0::Models::Items) ? x : ::Io::Apibuilder::Postman::Collection::V21::V0::Models::Items.from_json(x)) } @event = HttpClient::Preconditions.assert_class('event', opts.delete(:event), Array).map { |v| (x = v; x.is_a?(::Io::Apibuilder::Postman::Collection::V21::V0::Models::Event) ? x : ::Io::Apibuilder::Postman::Collection::V21::V0::Models::Event.new(x)) } @variable = HttpClient::Preconditions.assert_class('variable', opts.delete(:variable), Array).map { |v| (x = v; x.is_a?(::Io::Apibuilder::Postman::Collection::V21::V0::Models::Variable) ? x : ::Io::Apibuilder::Postman::Collection::V21::V0::Models::Variable.new(x)) } @auth = (x = opts.delete(:auth); x.nil? ? nil : (x = x; x.is_a?(::Io::Apibuilder::Postman::Collection::V21::V0::Models::Auth) ? x : ::Io::Apibuilder::Postman::Collection::V21::V0::Models::Auth.new(x))) end def to_json JSON.dump(to_hash) end def copy(incoming={}) Collection.new(to_hash.merge(HttpClient::Helper.symbolize_keys(incoming))) end def to_hash { :info => info.to_hash, :item => item.map { |o| o.to_hash }, :event => event.map { |o| o.to_hash }, :variable => variable.map { |o| o.to_hash }, :auth => auth.nil? ? nil : auth.to_hash } end end class Description attr_reader :content, :type def initialize(incoming={}) opts = HttpClient::Helper.symbolize_keys(incoming) @content = (x = opts.delete(:content); x.nil? ? nil : HttpClient::Preconditions.assert_class('content', x, String)) @type = (x = opts.delete(:type); x.nil? ? nil : HttpClient::Preconditions.assert_class('type', x, String)) end def to_json JSON.dump(to_hash) end def copy(incoming={}) Description.new(to_hash.merge(HttpClient::Helper.symbolize_keys(incoming))) end def to_hash { :content => content, :type => type } end end class Event attr_reader :listen, :script, :id, :disabled def initialize(incoming={}) opts = HttpClient::Helper.symbolize_keys(incoming) HttpClient::Preconditions.require_keys(opts, [:listen], 'Event') @listen = (x = opts.delete(:listen); x.is_a?(::Io::Apibuilder::Postman::Collection::V21::V0::Models::EventType) ? x : ::Io::Apibuilder::Postman::Collection::V21::V0::Models::EventType.apply(x)) @script = (x = opts.delete(:script); x.nil? ? nil : (x = x; x.is_a?(::Io::Apibuilder::Postman::Collection::V21::V0::Models::Script) ? x : ::Io::Apibuilder::Postman::Collection::V21::V0::Models::Script.new(x))) @id = (x = opts.delete(:id); x.nil? ? nil : HttpClient::Preconditions.assert_class('id', x, String)) @disabled = (x = opts.delete(:disabled); x.nil? ? nil : HttpClient::Preconditions.assert_boolean('disabled', x)) end def to_json JSON.dump(to_hash) end def copy(incoming={}) Event.new(to_hash.merge(HttpClient::Helper.symbolize_keys(incoming))) end def to_hash { :listen => listen.value, :script => script.nil? ? nil : script.to_hash, :id => id, :disabled => disabled } end end class File attr_reader :src, :content def initialize(incoming={}) opts = HttpClient::Helper.symbolize_keys(incoming) @src = (x = opts.delete(:src); x.nil? ? nil : HttpClient::Preconditions.assert_class('src', x, String)) @content = (x = opts.delete(:content); x.nil? ? nil : HttpClient::Preconditions.assert_class('content', x, String)) end def to_json JSON.dump(to_hash) end def copy(incoming={}) File.new(to_hash.merge(HttpClient::Helper.symbolize_keys(incoming))) end def to_hash { :src => src, :content => content } end end class Folder < Items attr_reader :name, :description, :variable, :item, :event, :auth def initialize(incoming={}) super(:type => Items::Types::FOLDER) opts = HttpClient::Helper.symbolize_keys(incoming) HttpClient::Preconditions.require_keys(opts, [:name, :item], 'Folder') @name = HttpClient::Preconditions.assert_class('name', opts.delete(:name), String) @description = (x = opts.delete(:description); x.nil? ? nil : (x = x; x.is_a?(::Io::Apibuilder::Postman::Collection::V21::V0::Models::Description) ? x : ::Io::Apibuilder::Postman::Collection::V21::V0::Models::Description.new(x))) @variable = (x = opts.delete(:variable); x.nil? ? nil : HttpClient::Preconditions.assert_class('variable', x, Array).map { |v| (x = v; x.is_a?(::Io::Apibuilder::Postman::Collection::V21::V0::Models::Variable) ? x : ::Io::Apibuilder::Postman::Collection::V21::V0::Models::Variable.new(x)) }) @item = HttpClient::Preconditions.assert_class('item', opts.delete(:item), Array).map { |v| (x = v; x.is_a?(::Io::Apibuilder::Postman::Collection::V21::V0::Models::Item) ? x : ::Io::Apibuilder::Postman::Collection::V21::V0::Models::Item.new(x)) } @event = (x = opts.delete(:event); x.nil? ? nil : HttpClient::Preconditions.assert_class('event', x, Array).map { |v| (x = v; x.is_a?(::Io::Apibuilder::Postman::Collection::V21::V0::Models::Event) ? x : ::Io::Apibuilder::Postman::Collection::V21::V0::Models::Event.new(x)) }) @auth = (x = opts.delete(:auth); x.nil? ? nil : (x = x; x.is_a?(::Io::Apibuilder::Postman::Collection::V21::V0::Models::Auth) ? x : ::Io::Apibuilder::Postman::Collection::V21::V0::Models::Auth.new(x))) end def to_json JSON.dump(to_hash) end def copy(incoming={}) Folder.new(subtype_to_hash.merge(HttpClient::Helper.symbolize_keys(incoming))) end def subtype_to_hash { :name => name, :description => description.nil? ? nil : description.to_hash, :variable => variable.nil? ? nil : variable.map { |o| o.to_hash }, :item => item.map { |o| o.to_hash }, :event => event.nil? ? nil : event.map { |o| o.to_hash }, :auth => auth.nil? ? nil : auth.to_hash } end end class FormParameter attr_reader :key, :value, :disabled, :description, :contentType, :type def initialize(incoming={}) opts = HttpClient::Helper.symbolize_keys(incoming) HttpClient::Preconditions.require_keys(opts, [:key], 'FormParameter') @key = HttpClient::Preconditions.assert_class('key', opts.delete(:key), String) @value = (x = opts.delete(:value); x.nil? ? nil : HttpClient::Preconditions.assert_class('value', x, String)) @disabled = (x = opts.delete(:disabled); x.nil? ? nil : HttpClient::Preconditions.assert_boolean('disabled', x)) @description = (x = opts.delete(:description); x.nil? ? nil : (x = x; x.is_a?(::Io::Apibuilder::Postman::Collection::V21::V0::Models::Description) ? x : ::Io::Apibuilder::Postman::Collection::V21::V0::Models::Description.new(x))) @contentType = (x = opts.delete(:contentType); x.nil? ? nil : HttpClient::Preconditions.assert_class('contentType', x, String)) @type = (x = opts.delete(:type); x.nil? ? nil : HttpClient::Preconditions.assert_class('type', x, String)) end def to_json JSON.dump(to_hash) end def copy(incoming={}) FormParameter.new(to_hash.merge(HttpClient::Helper.symbolize_keys(incoming))) end def to_hash { :key => key, :value => value, :disabled => disabled, :description => description.nil? ? nil : description.to_hash, :contentType => contentType, :type => type } end end class Header attr_reader :key, :value, :disabled, :description def initialize(incoming={}) opts = HttpClient::Helper.symbolize_keys(incoming) HttpClient::Preconditions.require_keys(opts, [:key, :value], 'Header') @key = HttpClient::Preconditions.assert_class('key', opts.delete(:key), String) @value = HttpClient::Preconditions.assert_class('value', opts.delete(:value), String) @disabled = (x = opts.delete(:disabled); x.nil? ? nil : HttpClient::Preconditions.assert_boolean('disabled', x)) @description = (x = opts.delete(:description); x.nil? ? nil : (x = x; x.is_a?(::Io::Apibuilder::Postman::Collection::V21::V0::Models::Description) ? x : ::Io::Apibuilder::Postman::Collection::V21::V0::Models::Description.new(x))) end def to_json JSON.dump(to_hash) end def copy(incoming={}) Header.new(to_hash.merge(HttpClient::Helper.symbolize_keys(incoming))) end def to_hash { :key => key, :value => value, :disabled => disabled, :description => description.nil? ? nil : description.to_hash } end end class Info attr_reader :name, :postman_id, :description, :version, :schema def initialize(incoming={}) opts = HttpClient::Helper.symbolize_keys(incoming) HttpClient::Preconditions.require_keys(opts, [:name, :schema], 'Info') @name = HttpClient::Preconditions.assert_class('name', opts.delete(:name), String) @postman_id = (x = opts.delete(:postman_id); x.nil? ? nil : HttpClient::Preconditions.assert_class('postman_id', x, String)) @description = (x = opts.delete(:description); x.nil? ? nil : (x = x; x.is_a?(::Io::Apibuilder::Postman::Collection::V21::V0::Models::Description) ? x : ::Io::Apibuilder::Postman::Collection::V21::V0::Models::Description.new(x))) @version = (x = opts.delete(:version); x.nil? ? nil : HttpClient::Preconditions.assert_class('version', x, String)) @schema = HttpClient::Preconditions.assert_class('schema', opts.delete(:schema), String) end def to_json JSON.dump(to_hash) end def copy(incoming={}) Info.new(to_hash.merge(HttpClient::Helper.symbolize_keys(incoming))) end def to_hash { :name => name, :postman_id => postman_id, :description => description.nil? ? nil : description.to_hash, :version => version, :schema => schema } end end class Item < Items attr_reader :id, :name, :description, :variable, :event, :request, :response def initialize(incoming={}) super(:type => Items::Types::ITEM) opts = HttpClient::Helper.symbolize_keys(incoming) HttpClient::Preconditions.require_keys(opts, [:request], 'Item') @id = (x = opts.delete(:id); x.nil? ? nil : HttpClient::Preconditions.assert_class('id', x, String)) @name = (x = opts.delete(:name); x.nil? ? nil : HttpClient::Preconditions.assert_class('name', x, String)) @description = (x = opts.delete(:description); x.nil? ? nil : (x = x; x.is_a?(::Io::Apibuilder::Postman::Collection::V21::V0::Models::Description) ? x : ::Io::Apibuilder::Postman::Collection::V21::V0::Models::Description.new(x))) @variable = (x = opts.delete(:variable); x.nil? ? nil : HttpClient::Preconditions.assert_class('variable', x, Array).map { |v| (x = v; x.is_a?(::Io::Apibuilder::Postman::Collection::V21::V0::Models::Variable) ? x : ::Io::Apibuilder::Postman::Collection::V21::V0::Models::Variable.new(x)) }) @event = (x = opts.delete(:event); x.nil? ? nil : HttpClient::Preconditions.assert_class('event', x, Array).map { |v| (x = v; x.is_a?(::Io::Apibuilder::Postman::Collection::V21::V0::Models::Event) ? x : ::Io::Apibuilder::Postman::Collection::V21::V0::Models::Event.new(x)) }) @request = (x = opts.delete(:request); x.is_a?(::Io::Apibuilder::Postman::Collection::V21::V0::Models::Request) ? x : ::Io::Apibuilder::Postman::Collection::V21::V0::Models::Request.new(x)) @response = (x = opts.delete(:response); x.nil? ? nil : HttpClient::Preconditions.assert_class('response', x, Array).map { |v| (x = v; x.is_a?(::Io::Apibuilder::Postman::Collection::V21::V0::Models::Response) ? x : ::Io::Apibuilder::Postman::Collection::V21::V0::Models::Response.new(x)) }) end def to_json JSON.dump(to_hash) end def copy(incoming={}) Item.new(subtype_to_hash.merge(HttpClient::Helper.symbolize_keys(incoming))) end def subtype_to_hash { :id => id, :name => name, :description => description.nil? ? nil : description.to_hash, :variable => variable.nil? ? nil : variable.map { |o| o.to_hash }, :event => event.nil? ? nil : event.map { |o| o.to_hash }, :request => request.to_hash, :response => response.nil? ? nil : response.map { |o| o.to_hash } } end end class QueryParam attr_reader :key, :value, :disabled, :description def initialize(incoming={}) opts = HttpClient::Helper.symbolize_keys(incoming) @key = (x = opts.delete(:key); x.nil? ? nil : HttpClient::Preconditions.assert_class('key', x, String)) @value = (x = opts.delete(:value); x.nil? ? nil : HttpClient::Preconditions.assert_class('value', x, String)) @disabled = (x = opts.delete(:disabled); x.nil? ? nil : HttpClient::Preconditions.assert_boolean('disabled', x)) @description = (x = opts.delete(:description); x.nil? ? nil : (x = x; x.is_a?(::Io::Apibuilder::Postman::Collection::V21::V0::Models::Description) ? x : ::Io::Apibuilder::Postman::Collection::V21::V0::Models::Description.new(x))) end def to_json JSON.dump(to_hash) end def copy(incoming={}) QueryParam.new(to_hash.merge(HttpClient::Helper.symbolize_keys(incoming))) end def to_hash { :key => key, :value => value, :disabled => disabled, :description => description.nil? ? nil : description.to_hash } end end class Request attr_reader :url, :auth, :method, :description, :header, :body def initialize(incoming={}) opts = HttpClient::Helper.symbolize_keys(incoming) @url = (x = opts.delete(:url); x.nil? ? nil : (x = x; x.is_a?(::Io::Apibuilder::Postman::Collection::V21::V0::Models::Url) ? x : ::Io::Apibuilder::Postman::Collection::V21::V0::Models::Url.new(x))) @auth = (x = opts.delete(:auth); x.nil? ? nil : (x = x; x.is_a?(::Io::Apibuilder::Postman::Collection::V21::V0::Models::Auth) ? x : ::Io::Apibuilder::Postman::Collection::V21::V0::Models::Auth.new(x))) @method = (x = opts.delete(:method); x.nil? ? nil : (x = x; x.is_a?(::Io::Apibuilder::Postman::Collection::V21::V0::Models::Method) ? x : ::Io::Apibuilder::Postman::Collection::V21::V0::Models::Method.apply(x))) @description = (x = opts.delete(:description); x.nil? ? nil : (x = x; x.is_a?(::Io::Apibuilder::Postman::Collection::V21::V0::Models::Description) ? x : ::Io::Apibuilder::Postman::Collection::V21::V0::Models::Description.new(x))) @header = (x = opts.delete(:header); x.nil? ? nil : HttpClient::Preconditions.assert_class('header', x, Array).map { |v| (x = v; x.is_a?(::Io::Apibuilder::Postman::Collection::V21::V0::Models::Header) ? x : ::Io::Apibuilder::Postman::Collection::V21::V0::Models::Header.new(x)) }) @body = (x = opts.delete(:body); x.nil? ? nil : (x = x; x.is_a?(::Io::Apibuilder::Postman::Collection::V21::V0::Models::Body) ? x : ::Io::Apibuilder::Postman::Collection::V21::V0::Models::Body.new(x))) end def to_json JSON.dump(to_hash) end def copy(incoming={}) Request.new(to_hash.merge(HttpClient::Helper.symbolize_keys(incoming))) end def to_hash { :url => url.nil? ? nil : url.to_hash, :auth => auth.nil? ? nil : auth.to_hash, :method => method.nil? ? nil : method.value, :description => description.nil? ? nil : description.to_hash, :header => header.nil? ? nil : header.map { |o| o.to_hash }, :body => body.nil? ? nil : body.to_hash } end end class Response attr_reader :id, :name, :originalRequest, :responseTime, :header, :body, :status, :code def initialize(incoming={}) opts = HttpClient::Helper.symbolize_keys(incoming) @id = (x = opts.delete(:id); x.nil? ? nil : HttpClient::Preconditions.assert_class('id', x, String)) @name = (x = opts.delete(:name); x.nil? ? nil : HttpClient::Preconditions.assert_class('name', x, String)) @originalRequest = (x = opts.delete(:originalRequest); x.nil? ? nil : (x = x; x.is_a?(::Io::Apibuilder::Postman::Collection::V21::V0::Models::Request) ? x : ::Io::Apibuilder::Postman::Collection::V21::V0::Models::Request.new(x))) @responseTime = (x = opts.delete(:responseTime); x.nil? ? nil : HttpClient::Preconditions.assert_class('responseTime', x, String)) @header = (x = opts.delete(:header); x.nil? ? nil : HttpClient::Preconditions.assert_class('header', x, Array).map { |v| (x = v; x.is_a?(::Io::Apibuilder::Postman::Collection::V21::V0::Models::Header) ? x : ::Io::Apibuilder::Postman::Collection::V21::V0::Models::Header.new(x)) }) @body = (x = opts.delete(:body); x.nil? ? nil : HttpClient::Preconditions.assert_class('body', x, String)) @status = (x = opts.delete(:status); x.nil? ? nil : HttpClient::Preconditions.assert_class('status', x, String)) @code = (x = opts.delete(:code); x.nil? ? nil : HttpClient::Preconditions.assert_class('code', x, Integer)) end def to_json JSON.dump(to_hash) end def copy(incoming={}) Response.new(to_hash.merge(HttpClient::Helper.symbolize_keys(incoming))) end def to_hash { :id => id, :name => name, :originalRequest => originalRequest.nil? ? nil : originalRequest.to_hash, :responseTime => responseTime, :header => header.nil? ? nil : header.map { |o| o.to_hash }, :body => body, :status => status, :code => code } end end class Script attr_reader :exec, :type, :id, :src, :name def initialize(incoming={}) opts = HttpClient::Helper.symbolize_keys(incoming) HttpClient::Preconditions.require_keys(opts, [:exec], 'Script') @exec = HttpClient::Preconditions.assert_class('exec', opts.delete(:exec), Array).map { |v| HttpClient::Preconditions.assert_class('exec', v, String) } @type = HttpClient::Preconditions.assert_class('type', (x = opts.delete(:type); x.nil? ? "text/javascript" : x), String) @id = (x = opts.delete(:id); x.nil? ? nil : HttpClient::Preconditions.assert_class('id', x, String)) @src = (x = opts.delete(:src); x.nil? ? nil : HttpClient::Preconditions.assert_class('src', x, String)) @name = (x = opts.delete(:name); x.nil? ? nil : HttpClient::Preconditions.assert_class('name', x, String)) end def to_json JSON.dump(to_hash) end def copy(incoming={}) Script.new(to_hash.merge(HttpClient::Helper.symbolize_keys(incoming))) end def to_hash { :exec => exec, :type => type, :id => id, :src => src, :name => name } end end class Url attr_reader :raw, :protocol, :host, :path, :port, :query, :hash, :variable def initialize(incoming={}) opts = HttpClient::Helper.symbolize_keys(incoming) @raw = (x = opts.delete(:raw); x.nil? ? nil : HttpClient::Preconditions.assert_class('raw', x, String)) @protocol = (x = opts.delete(:protocol); x.nil? ? nil : HttpClient::Preconditions.assert_class('protocol', x, String)) @host = (x = opts.delete(:host); x.nil? ? nil : HttpClient::Preconditions.assert_class('host', x, Array).map { |v| HttpClient::Preconditions.assert_class('host', v, String) }) @path = (x = opts.delete(:path); x.nil? ? nil : HttpClient::Preconditions.assert_class('path', x, Array).map { |v| HttpClient::Preconditions.assert_class('path', v, String) }) @port = (x = opts.delete(:port); x.nil? ? nil : HttpClient::Preconditions.assert_class('port', x, String)) @query = (x = opts.delete(:query); x.nil? ? nil : HttpClient::Preconditions.assert_class('query', x, Array).map { |v| (x = v; x.is_a?(::Io::Apibuilder::Postman::Collection::V21::V0::Models::QueryParam) ? x : ::Io::Apibuilder::Postman::Collection::V21::V0::Models::QueryParam.new(x)) }) @hash = (x = opts.delete(:hash); x.nil? ? nil : HttpClient::Preconditions.assert_class('hash', x, String)) @variable = (x = opts.delete(:variable); x.nil? ? nil : HttpClient::Preconditions.assert_class('variable', x, Array).map { |v| (x = v; x.is_a?(::Io::Apibuilder::Postman::Collection::V21::V0::Models::Variable) ? x : ::Io::Apibuilder::Postman::Collection::V21::V0::Models::Variable.new(x)) }) end def to_json JSON.dump(to_hash) end def copy(incoming={}) Url.new(to_hash.merge(HttpClient::Helper.symbolize_keys(incoming))) end def to_hash { :raw => raw, :protocol => protocol, :host => host.nil? ? nil : host, :path => path.nil? ? nil : path, :port => port, :query => query.nil? ? nil : query.map { |o| o.to_hash }, :hash => hash, :variable => variable.nil? ? nil : variable.map { |o| o.to_hash } } end end class UrlEncoded attr_reader :key, :value, :disabled, :description def initialize(incoming={}) opts = HttpClient::Helper.symbolize_keys(incoming) HttpClient::Preconditions.require_keys(opts, [:key], 'UrlEncoded') @key = HttpClient::Preconditions.assert_class('key', opts.delete(:key), String) @value = (x = opts.delete(:value); x.nil? ? nil : HttpClient::Preconditions.assert_class('value', x, String)) @disabled = (x = opts.delete(:disabled); x.nil? ? nil : HttpClient::Preconditions.assert_boolean('disabled', x)) @description = (x = opts.delete(:description); x.nil? ? nil : (x = x; x.is_a?(::Io::Apibuilder::Postman::Collection::V21::V0::Models::Description) ? x : ::Io::Apibuilder::Postman::Collection::V21::V0::Models::Description.new(x))) end def to_json JSON.dump(to_hash) end def copy(incoming={}) UrlEncoded.new(to_hash.merge(HttpClient::Helper.symbolize_keys(incoming))) end def to_hash { :key => key, :value => value, :disabled => disabled, :description => description.nil? ? nil : description.to_hash } end end # Using variables in your Postman requests eliminates the need to duplicate # requests, which can save a lot of time. Variables can be defined, and # referenced to from any part of a request. class Variable attr_reader :id, :key, :value, :type, :name, :description, :system, :disabled def initialize(incoming={}) opts = HttpClient::Helper.symbolize_keys(incoming) @id = (x = opts.delete(:id); x.nil? ? nil : HttpClient::Preconditions.assert_class('id', x, String)) @key = (x = opts.delete(:key); x.nil? ? nil : HttpClient::Preconditions.assert_class('key', x, String)) @value = (x = opts.delete(:value); x.nil? ? nil : HttpClient::Preconditions.assert_class('value', x, String)) @type = (x = opts.delete(:type); x.nil? ? nil : HttpClient::Preconditions.assert_class('type', x, String)) @name = (x = opts.delete(:name); x.nil? ? nil : HttpClient::Preconditions.assert_class('name', x, String)) @description = (x = opts.delete(:description); x.nil? ? nil : (x = x; x.is_a?(::Io::Apibuilder::Postman::Collection::V21::V0::Models::Description) ? x : ::Io::Apibuilder::Postman::Collection::V21::V0::Models::Description.new(x))) @system = (x = opts.delete(:system); x.nil? ? nil : HttpClient::Preconditions.assert_boolean('system', x)) @disabled = (x = opts.delete(:disabled); x.nil? ? nil : HttpClient::Preconditions.assert_boolean('disabled', x)) end def to_json JSON.dump(to_hash) end def copy(incoming={}) Variable.new(to_hash.merge(HttpClient::Helper.symbolize_keys(incoming))) end def to_hash { :id => id, :key => key, :value => value, :type => type, :name => name, :description => description.nil? ? nil : description.to_hash, :system => system, :disabled => disabled } end end end # ===== END OF SERVICE DEFINITION ===== module HttpClient class HttpHandler # Returns a client instance to use # # @param base_uri The base URI for this API # @param path the Requested full http path (including any query strings) def instance(base_uri, path) raise "Override in subclass" end end class HttpHandlerInstance # Executes a request. The provided request object will be an # instance of Net::HTTP (e.g. Net::HTTP::Get) def execute(request) raise "Override in subclass" end end class DefaultHttpHandler < HttpHandler def instance(base_uri, path) DefaultHttpHandlerInstance.new(base_uri) end end class DefaultHttpHandlerInstance < HttpHandlerInstance attr_reader :client def initialize(base_uri) @base_uri = Preconditions.assert_class('base_uri', base_uri, URI) @client = Net::HTTP.new(@base_uri.host, @base_uri.port) if @base_uri.scheme == "https" configure_ssl end end def execute(request) response = begin @client.request(request) rescue SocketError => e raise StandardError.new("Error accessing uri[#{full_uri(request.path)}]: #{e}") end case response when Net::HTTPSuccess response.body else body = response.body rescue nil raise HttpClient::ServerError.new(response.code.to_i, response.message, :body => body, :uri => full_uri(request.path).to_s) end end def full_uri(path) path.start_with?(@base_uri.to_s) ? path : File.join(@base_uri.to_s, path) end # Called to configure SSL if the base uri requires it def configure_ssl @client.use_ssl = true @client.verify_mode = OpenSSL::SSL::VERIFY_PEER @client.cert_store = OpenSSL::X509::Store.new @client.cert_store.set_default_paths end end class Request attr_reader :base_uri, :path, :full_uri def initialize(http_handler, base_uri, path) @http_handler = http_handler @base_uri = Preconditions.assert_class('base_uri', base_uri, URI) @path = Preconditions.assert_class('path', path, String) @full_uri = @base_uri.to_s + @path @params = nil @body = nil @auth = nil @headers = {} @header_keys_lower_case = [] end def with_header(name, value) Preconditions.check_not_blank('name', name, "Header name is required") Preconditions.check_not_blank('value', value, "Header value is required") Preconditions.check_state(!@headers.has_key?(name), "Duplicate header named[%s]" % name) @headers[name] = value @header_keys_lower_case << name.downcase self end def with_auth(auth) Preconditions.assert_class('auth', auth, HttpClient::Authorization) Preconditions.check_state(@auth.nil?, "auth previously set") if auth.scheme.name == AuthScheme::BASIC.name @auth = auth else raise "Auth Scheme[#{auth.scheme.name}] not supported" end self end def with_query(params) Preconditions.assert_class('params', params, Hash) Preconditions.check_state(@params.nil?, "Already have query parameters") @params = params self end # Wrapper to set Content-Type header to application/json and set # the provided json document as the body def with_json(json) @headers['Content-Type'] ||= 'application/json; charset=UTF-8' with_body(json) end def with_body(body) Preconditions.check_not_blank('body', body) @body = body self end def get(&block) do_request(Net::HTTP::Get, &block) end def delete(&block) do_request(Net::HTTP::Delete, &block) end def options(&block) do_request(Net::HTTP::Options, &block) end def post(&block) do_request(Net::HTTP::Post, &block) end def put(&block) do_request(Net::HTTP::Put, &block) end class PATCH < Net::HTTP::Put METHOD = "PATCH" end def patch(&block) do_request(PATCH, &block) end def do_request(klass) Preconditions.assert_class('klass', klass, Class) uri = @full_uri.dup if q = to_query(@params) uri += "?%s" % q end request = klass.send(:new, uri) # DEBUG curl = ['curl'] # DEBUG if klass != Net::HTTP::Get # DEBUG curl << "-X%s" % klass.name.split("::").last.upcase # DEBUG end if @body # DEBUG tmpfile = "/tmp/rest_client.tmp" # DEBUG File.open(tmpfile, "w") { |os| os << @body.to_s } # DEBUG curl << "-d@%s" % tmpfile request.body = @body end if @auth # DEBUG curl << "-u \"%s:%s\"" % [@auth.username, @auth.password] Preconditions.check_state(!@header_keys_lower_case.include?("authorization"), "Cannot specify both an Authorization header and an auth instance") user_pass = "%s:%s" % [@auth.username, @auth.password] encoded = Base64.encode64(user_pass).to_s.split("\n").map(&:strip).join request.add_field("Authorization", "Basic %s" % encoded) end @headers.each { |key, value| # DEBUG curl << "-H \"%s: %s\"" % [key, value] request.add_field(key, value) } # DEBUG curl << "'%s%s'" % [@base_uri, path] # DEBUG puts curl.join(" ") raw_response = @http_handler.instance(@base_uri, request.path).execute(request) response = raw_response.to_s == "" ? nil : JSON.parse(raw_response) if block_given? yield response else response end end private def to_query(params={}) parts = (params || {}).map { |k,v| if v.is_a?(Enumerable) v.map { |el| "%s=%s" % [k, CGI.escape(el.to_s)] } else "%s=%s" % [k, CGI.escape(v.to_s)] end } parts.empty? ? nil : parts.join("&") end end class ServerError < StandardError attr_reader :code, :details, :body, :uri def initialize(code, details, incoming={}) opts = HttpClient::Helper.symbolize_keys(incoming) @code = HttpClient::Preconditions.assert_class('code', code, Integer) @details = HttpClient::Preconditions.assert_class('details', details, String) @body = HttpClient::Preconditions.assert_class_or_nil('body', opts.delete(:body), String) @uri = HttpClient::Preconditions.assert_class_or_nil('uri', opts.delete(:uri), String) HttpClient::Preconditions.assert_empty_opts(opts) super(self.message) end def message m = "%s %s" % [@code, @details] if @body m << ": %s" % @body end m end def body_json @body ? JSON.parse(@body) : nil end end class PreconditionException < StandardError attr_reader :message def initialize(message) super(message) @message = message end end module Preconditions def Preconditions.check_argument(expression, error_message=nil) if !expression raise PreconditionException.new(error_message || "check_argument failed") end nil end def Preconditions.check_state(expression, error_message=nil) if !expression raise PreconditionException.new(error_message || "check_state failed") end nil end def Preconditions.check_not_nil(field_name, reference, error_message=nil) if reference.nil? raise PreconditionException.new(error_message || "argument for %s cannot be nil" % field_name) end reference end def Preconditions.check_not_blank(field_name, reference, error_message=nil) if reference.to_s.strip == "" raise PreconditionException.new(error_message || "argument for %s cannot be blank" % field_name) end reference end # Throws an error if opts is not empty. Useful when parsing # arguments to a function def Preconditions.assert_empty_opts(opts) if !opts.empty? raise PreconditionException.new("Invalid opts: #{opts.keys.inspect}\n#{opts.inspect}") end end # Requires that the provided hash has the specified keys. # @param fields A list of symbols def Preconditions.require_keys(hash, fields, error_prefix=nil) missing = fields.select { |f| !hash.has_key?(f) } if !missing.empty? msg = "Missing required fields: " + missing.join(", ") raise PreconditionException.new(error_prefix.empty? ? msg : "#{error_prefix}: #{msg}") end end # Asserts that value is not nill and is_?(klass). Returns # value. Common use is # # amount = Preconditions.assert_class('amount', amount, BigDecimal) def Preconditions.assert_class(field_name, value, klass) Preconditions.check_not_nil('field_name', field_name) Preconditions.check_not_nil('klass', klass) Preconditions.check_not_nil('value', value, "Value for %s cannot be nil. Expected an instance of class %s" % [field_name, klass.name]) Preconditions.check_state(value.is_a?(klass), "Value for #{field_name} is of type[#{value.class}] - class[#{klass}] is required. value[#{value.inspect.to_s}]") value end def Preconditions.assert_class_or_nil(field_name, value, klass) if !value.nil? Preconditions.assert_class(field_name, value, klass) end end def Preconditions.assert_boolean(field_name, value) Preconditions.check_not_nil('field_name', field_name) Preconditions.check_not_nil('value', value, "Value for %s cannot be nil. Expected an instance of TrueClass or FalseClass" % field_name) Preconditions.check_state(value.is_a?(TrueClass) || value.is_a?(FalseClass), "Value for #{field_name} is of type[#{value.class}] - class[TrueClass or FalseClass] is required. value[#{value.inspect.to_s}]") value end def Preconditions.assert_boolean_or_nil(field_name, value) if !value.nil? Preconditions.assert_boolean(field_name, value) end end def Preconditions.assert_collection_of_class(field_name, values, klass) Preconditions.assert_class(field_name, values, Array) values.each { |v| Preconditions.assert_class(field_name, v, klass) } end def Preconditions.assert_hash_of_class(field_name, hash, klass) Preconditions.assert_class(field_name, hash, Hash) values.each { |k, v| Preconditions.assert_class(field_name, v, klass) } end end class AuthScheme attr_reader :name def initialize(name) @name = HttpClient::Preconditions.check_not_blank('name', name) end BASIC = AuthScheme.new("basic") unless defined?(BASIC) end class Authorization attr_reader :scheme, :username, :password def initialize(scheme, username, opts={}) @scheme = HttpClient::Preconditions.assert_class('schema', scheme, AuthScheme) @username = HttpClient::Preconditions.check_not_blank('username', username, "username is required") @password = HttpClient::Preconditions.assert_class_or_nil('password', opts.delete(:password), String) HttpClient::Preconditions.assert_empty_opts(opts) end def Authorization.basic(username, password=nil) Authorization.new(AuthScheme::BASIC, username, :password => password) end end module Helper def Helper.symbolize_keys(hash) Preconditions.assert_class('hash', hash, Hash) new_hash = {} hash.each { |k, v| new_hash[k.to_sym] = v } new_hash end def Helper.to_big_decimal(value) value ? BigDecimal(value.to_s) : nil end def Helper.to_object(value) value ? (value.is_a?(Hash) ? value : JSON.parse(value)) : nil end def Helper.to_uuid(value) Preconditions.check_state(value.nil? || value.match(/^\w\w\w\w\w\w\w\w\-\w\w\w\w\-\w\w\w\w\-\w\w\w\w\-\w\w\w\w\w\w\w\w\w\w\w\w$/), "Invalid guid[%s]" % value) value end def Helper.to_date_iso8601(value) if value.is_a?(Date) value elsif value Date.parse(value.to_s) else nil end end def Helper.to_date_time_iso8601(value) if value.is_a?(DateTime) value elsif value DateTime.parse(value.to_s) else nil end end def Helper.date_iso8601_to_string(value) value.nil? ? nil : value.strftime('%Y-%m-%d') end def Helper.date_time_iso8601_to_string(value) value.nil? ? nil : value.strftime('%Y-%m-%dT%H:%M:%S%z') end TRUE_STRINGS = ['t', 'true', 'y', 'yes', 'on', '1', 'trueclass'] unless defined?(TRUE_STRINGS) FALSE_STRINGS = ['f', 'false', 'n', 'no', 'off', '0', 'falseclass'] unless defined?(FALSE_STRINGS) def Helper.to_boolean(field_name, value) string = value.to_s.strip.downcase if TRUE_STRINGS.include?(string) true elsif FALSE_STRINGS.include?(string) false elsif string != "" raise PreconditionException.new("Unsupported boolean value[#{string}]. For true, must be one of: #{TRUE_STRINGS.inspect}. For false, must be one of: #{FALSE_STRINGS.inspect}") else nil end end end end end end end end end end