Skip to content

Commit

Permalink
Opened up the HTTP Client, added repeat method
Browse files Browse the repository at this point in the history
  • Loading branch information
bo-oz committed Apr 11, 2018
1 parent f57b5b7 commit 6237fe7
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 13 deletions.
2 changes: 2 additions & 0 deletions lib/vimeo_me2/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ def get_object
end

def request(url=@base_uri, method:'get', code: 200, headers:nil, body:nil, query:nil)
# Moved responsibility to reset the request to the beginning of a new call
@client.reset_request
@client.body = body
# Vimeo API version 3.4 contains some breaking changes to things like upload
# The endpoints implemented by VimeoMe2 all seem to work up to at least version 3.2
Expand Down
33 changes: 20 additions & 13 deletions lib/vimeo_me2/http/http_request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,29 +8,36 @@ module Http
class HttpRequest
include VimeoMe2::Http::OAuth::Verify

attr_reader :last_request, :ratelimit
attr_reader :method, :end_point, :last_request, :ratelimit, :allowed_status
attr_accessor :body, :headers, :query, :debug

def initialize(token=nil)
@token = token
reset_request
end

def make_http_request(method, endpoint, allowed_status=200)
log "#{method.upcase} #{prefix_endpoint(endpoint)} #{allowed_status}"
call = HTTParty.public_send(method, prefix_endpoint(endpoint), http_request)
def make_http_request(method, end_point, allowed_status=200)
@method = method
@end_point = end_point
@allowed_status = allowed_status
log "#{method.upcase} #{prefix_endpoint(@end_point)} #{allowed_status}"
call = HTTParty.public_send(@method, prefix_endpoint(@end_point), http_request)
@last_request = call
@ratelimit = {
'limit' => call.headers['x-ratelimit-limit'],
'remaining' => call.headers['x-ratelimit-remaining'],
'reset' => call.headers['x-ratelimit-reset']
}
validate_response!(call, allowed_status)
reset_request
validate_response!(call, @allowed_status)
return nil if call.response.body.nil?
call.response.body.empty? ? call.response.body : JSON.parse(call.response.body)
end

# convenience method to replay the latest request
def repeat_http_request
make_http_request(@method, @end_point, @allowed_status)
end

def set_token token
set_auth_header(token)
end
Expand All @@ -51,19 +58,19 @@ def add_queries(additional)
@query.merge!(additional) if additional.instance_of? Hash
end

def reset_request
@headers = { 'Content-Type' => 'application/json' }
@body = {}
@query = {}
set_auth_header(@token) unless @token.nil?
end

private

def request_uri uri=nil
"#{@base_uri}#{uri}"
end

def reset_request
@headers = { 'Content-Type' => 'application/json' }
@body = {}
@query = {}
set_auth_header(@token) unless @token.nil?
end

def set_auth_header token
add_header('authorization', "Bearer #{token}")
end
Expand Down

0 comments on commit 6237fe7

Please sign in to comment.