Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 23 additions & 11 deletions lib/client-api/base.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require_relative 'request'
require 'byebug'

module ClientApi

Expand All @@ -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)
Expand All @@ -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
Expand Down Expand Up @@ -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

Expand Down
9 changes: 0 additions & 9 deletions spec/client/json_body_template.rb

This file was deleted.

19 changes: 19 additions & 0 deletions spec/client/json_body_template_spec.rb
Original file line number Diff line number Diff line change
@@ -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
92 changes: 92 additions & 0 deletions spec/client/named_parameter_spec.rb
Original file line number Diff line number Diff line change
@@ -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