diff --git a/lib/client-api/base.rb b/lib/client-api/base.rb index 6b78584..77d7a5c 100644 --- a/lib/client-api/base.rb +++ b/lib/client-api/base.rb @@ -1,4 +1,5 @@ require_relative 'request' +require 'byebug' module ClientApi @@ -11,18 +12,21 @@ def initialize end def get(url, headers = nil) + headers = url[:headers] unless url.is_a? String @output = get_request(url_generator(url), :headers => headers) self.post_logger if $logger self.output_json_body if json_output end def get_with_body(url, body = nil, headers = nil) + headers = url[:headers] unless url.is_a? String @output = get_with_body_request(url_generator(url), :body => body, :headers => headers) self.post_logger if $logger self.output_json_body if json_output end - def post(url, body, headers = nil) + def post(url, body = nil, headers = nil) + headers = url[:headers] unless url.is_a? String if body.is_a? Hash if body['type'] && body['data'] @output = post_request_x(url_generator(url), :body => body, :headers => headers) @@ -38,18 +42,21 @@ def post(url, body, headers = nil) end def delete(url, headers = nil) + headers = url[:headers] unless url.is_a? String @output = delete_request(url_generator(url), :headers => headers) self.post_logger if $logger self.output_json_body if json_output end def put(url, body, headers = nil) + headers = url[:headers] unless url.is_a? String @output = put_request(url_generator(url), :body => body, :headers => headers) self.post_logger if $logger self.output_json_body if json_output end def patch(url, body, headers = nil) + headers = url[:headers] unless url.is_a? String @output = patch_request(url_generator(url), :body => body, :headers => headers) self.post_logger if $logger self.output_json_body if json_output @@ -127,20 +134,25 @@ def payload(args) end def url_generator(url) - begin - if url.count == 2 - raise('":url"'.green + ' field is missing in url'.red) if url[:url].nil? - raise('":query"'.green + ' field is missing in url'.red) if url[:query].nil? - query = url[:url].include?('?') ? [url[:url]] : [url[:url].concat('?')] + return url if url.is_a? String - url[:query].map do |val| - query << val[0].to_s + "=" + val[1].gsub(' ','%20') + "&" + # url params + query = url[:query] unless url[:query].nil? + query = nil if url[:query].nil? + + if url[:url].is_a? String + if query.nil? + return url[:url] + else + url = url[:url].include?('?') ? [url[:url]] : [url[:url].concat('?')] + query.map do |val| + url << val[0].to_s + "=" + val[1].to_s.gsub(' ','%20') + "&" end - return query.join.delete_suffix('&') + return url.join.delete_suffix('&') end - rescue ArgumentError - url + else + raise "give a valid url; say., " + ":url => 'https://reqres.in?'".green end end diff --git a/spec/client/json_body_template.rb b/spec/client/json_body_template.rb deleted file mode 100644 index a012152..0000000 --- a/spec/client/json_body_template.rb +++ /dev/null @@ -1,9 +0,0 @@ -describe 'JSON template as body' do - - it "{POST request} json template as body", :post do - api = ClientApi::Api.new - api.post('/api/users', payload("./data/request/post.json")) - - expect(api.status).to eq(201) - end -end \ No newline at end of file diff --git a/spec/client/json_body_template_spec.rb b/spec/client/json_body_template_spec.rb new file mode 100644 index 0000000..e2ab790 --- /dev/null +++ b/spec/client/json_body_template_spec.rb @@ -0,0 +1,19 @@ +describe 'JSON template as body' do + + it "{POST request} json template as body" do + api = ClientApi::Api.new + api.post('/api/users', payload("./data/request/post.json")) + + expect(api.status).to eq(201) + end + + it "{POST request} json template as body x2" do + api = ClientApi::Api.new + api.post('/api/users', + { + "name": "prashanth sams" + } + ) + expect(api.status).to eq(201) + end +end \ No newline at end of file diff --git a/spec/client/named_parameter_spec.rb b/spec/client/named_parameter_spec.rb new file mode 100644 index 0000000..e80aaf8 --- /dev/null +++ b/spec/client/named_parameter_spec.rb @@ -0,0 +1,92 @@ +# require 'byebug' +# describe 'Named parameter', :focus do +# +# it "Named parameter - GET" do +# api = ClientApi::Api.new +# +# api.get("/api/users?page=2") +# expect(api.status).to eq(200) +# +# api.get( +# :url => "/api/users?", +# :query => { +# "page": 2 +# } +# ) +# expect(api.status).to eq(200) +# +# api.get( +# :url => "/api/users?", +# :query => { +# "page": "2" +# } +# ) +# expect(api.status).to eq(200) +# +# api.get( +# :url => "/api/users?", +# :query => { +# } +# ) +# expect(api.status).to eq(200) +# +# api.get( +# :url => "/api/users?", +# :query => { +# "page": 2 +# }, +# :headers => { +# 'Accept' => 'application/json' +# } +# ) +# +# api.get( +# :url => "/api/users?", +# :headers => { +# 'Accept' => 'application/json' +# }, +# :query => { +# "page": 2 +# } +# ) +# expect(api.status).to eq(200) +# +# api.get( +# :query => { +# "page": 2 +# }, +# :headers => { +# 'Accept' => 'application/json' +# }, +# :url => "/api/users?", +# ) +# expect(api.status).to eq(200) +# +# api.get( +# :url => "/api/users?", +# :query => { +# "page": 2, +# "per_page": "3" +# }, +# :headers => { +# 'Accept' => 'application/json' +# } +# ) +# expect(api.status).to eq(200) +# end +# +# # it "Named parameter - POST" do +# # api = ClientApi::Api.new +# # +# # api.post("/api/users?page=2", {}) +# # +# # expect(api.status).to eq(200) +# # api.post( +# # :url => "/api/users?", +# # :body => {} +# # ) +# # expect(api.status).to eq(200) +# # end +# +# +# end