Skip to content

Commit

Permalink
allow passing custom headers to requests
Browse files Browse the repository at this point in the history
  • Loading branch information
mackuba committed Mar 31, 2024
1 parent 32abbc5 commit f25f633
Showing 1 changed file with 9 additions and 6 deletions.
15 changes: 9 additions & 6 deletions lib/minisky/requests.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ def auto_manage_tokens
instance_variable_defined?('@auto_manage_tokens') ? @auto_manage_tokens : true
end

alias progress default_progress
alias progress= default_progress=

def base_url
Expand All @@ -49,10 +50,10 @@ def user
@user ||= User.new(config)
end

def get_request(method, params = nil, auth: default_auth_mode)
def get_request(method, params = nil, auth: default_auth_mode, headers: nil)
check_access if auto_manage_tokens && auth == true

headers = authentication_header(auth)
headers = authentication_header(auth).merge(headers || {})
url = URI("#{base_url}/#{method}")

if params && !params.empty?
Expand All @@ -65,26 +66,28 @@ def get_request(method, params = nil, auth: default_auth_mode)
handle_response(response)
end

def post_request(method, params = nil, auth: default_auth_mode)
def post_request(method, params = nil, auth: default_auth_mode, headers: nil)
check_access if auto_manage_tokens && auth == true

headers = authentication_header(auth).merge({ "Content-Type" => "application/json" })
headers = authentication_header(auth).merge(headers || {})
headers["Content-Type"] = "application/json" unless headers.keys.any? { |k| k.downcase == 'content-type' }

body = params ? params.to_json : ''

response = Net::HTTP.post(URI("#{base_url}/#{method}"), body, headers)
handle_response(response)
end

def fetch_all(method, params = nil, field:,
auth: default_auth_mode, break_when: nil, max_pages: nil, progress: @default_progress)
auth: default_auth_mode, break_when: nil, max_pages: nil, headers: nil, progress: @default_progress)
data = []
params = {} if params.nil?
pages = 0

loop do
print(progress) if progress

response = get_request(method, params, auth: auth)
response = get_request(method, params, auth: auth, headers: headers)
records = response[field]
cursor = response['cursor']

Expand Down

0 comments on commit f25f633

Please sign in to comment.