Skip to content

Commit

Permalink
Merge pull request #5 from gettyimages/accept_language_header
Browse files Browse the repository at this point in the history
Add accept language header functionality
  • Loading branch information
ssterli2 committed Jul 26, 2018
2 parents 2874adf + b920452 commit 7df612f
Show file tree
Hide file tree
Showing 23 changed files with 292 additions and 34 deletions.
15 changes: 10 additions & 5 deletions lib/CustomRequest/CustomRequest.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

class CustomRequest < RequestBase

attr_accessor :method, :body, :route
attr_accessor :method, :body, :route, :headers
# @route = "/v3/"

def initialize(api_key, access_token)
Expand All @@ -13,13 +13,13 @@ def initialize(api_key, access_token)
def execute
case @method
when "GET"
return @http_helper.get(@route, @query_params)
return @http_helper.get(@route, @query_params, self.headers)
when "POST"
return @http_helper.post(@route, @query_params, self.body)
return @http_helper.post(@route, @query_params, self.body, self.headers)
when "PUT"
return @http_helper.put(@route, @query_params, self.body)
return @http_helper.put(@route, @query_params, self.body, self.headers)
when "DELETE"
return @http_helper.delete(@route, @query_params)
return @http_helper.delete(@route, @query_params, self.headers)
else
raise "No appropriate HTTP method found for this request."
end
Expand Down Expand Up @@ -60,4 +60,9 @@ def with_body(body)
return self
end

def with_headers(headers)
self.headers = headers
return self
end

end
10 changes: 8 additions & 2 deletions lib/Downloads/DownloadImages.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

class DownloadImages < RequestBase

attr_accessor :asset_id
attr_accessor :asset_id, :accept_language

API_ROUTE = "/v3/downloads/images" # mashery endpoint
QUERY_PARAMS_NAMES = ["file_type","height","product_id","product_type"]
Expand All @@ -23,10 +23,16 @@ def with_id(asset_id)
return self
end

public
def with_accept_language(language)
@accept_language = {"Accept-Language" => language}
return self
end

def execute
build_query_params("auto_download", "false")
uri = API_ROUTE + "/" + self.asset_id
return @http_helper.post(uri, @query_params, nil)
return @http_helper.post(uri, @query_params, nil, accept_language)
end

end
10 changes: 8 additions & 2 deletions lib/Downloads/DownloadVideos.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

class DownloadVideos < RequestBase

attr_accessor :asset_id
attr_accessor :asset_id, :accept_language

API_ROUTE = "/v3/downloads/videos" # mashery endpoint
QUERY_PARAMS_NAMES = ["product_id","size"]
Expand All @@ -23,10 +23,16 @@ def with_id(asset_id)
return self
end

public
def with_accept_language(language)
@accept_language = {"Accept-Language" => language}
return self
end

def execute
build_query_params("auto_download", "false")
uri = API_ROUTE + "/" + self.asset_id
return @http_helper.post(uri, @query_params, nil)
return @http_helper.post(uri, @query_params, nil, accept_language)
end

end
19 changes: 10 additions & 9 deletions lib/HttpHelper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def get_uri(path)
return URI.parse "#{Api_Host::API_BASE_URL}#{path}"
end

def get(endpoint_path, query_params)
def get(endpoint_path, query_params, headers = nil)

uri = get_uri(endpoint_path)
#puts uri
Expand All @@ -29,11 +29,11 @@ def get(endpoint_path, query_params)
end
#puts "REQUEST URI: #{uri.request_uri}"
req = Net::HTTP::Get.new uri.request_uri
return send uri, req, @api_key, @access_token
return send uri, req, @api_key, @access_token, headers

end

def post(endpoint_path, query_params, body)
def post(endpoint_path, query_params, body, headers = nil)

uri = get_uri(endpoint_path)
if !query_params.nil?
Expand All @@ -44,11 +44,11 @@ def post(endpoint_path, query_params, body)
req["Content-Type"] = "application/json"
req.body = body.to_json
end
return send uri, req, @api_key, @access_token
return send uri, req, @api_key, @access_token, headers

end

def put(endpoint_path, query_params, body)
def put(endpoint_path, query_params, body, headers = nil)

uri = get_uri(endpoint_path)
if !query_params.nil?
Expand All @@ -59,18 +59,18 @@ def put(endpoint_path, query_params, body)
req["Content-Type"] = "application/json"
req.body = body.to_json
end
return send uri, req, @api_key, @access_token
return send uri, req, @api_key, @access_token, headers

end

def delete(endpoint_path, query_params)
def delete(endpoint_path, query_params, headers = nil)

uri = get_uri(endpoint_path)
if !query_params.nil?
uri.query = URI.encode_www_form query_params
end
req = Net::HTTP::Delete.new uri.request_uri
return send uri, req, @api_key, @access_token
return send uri, req, @api_key, @access_token, headers

end

Expand All @@ -93,14 +93,15 @@ def os
end

private
def send(uri, request, api_key, bearer_token = "")
def send(uri, request, api_key, bearer_token = "", headers)

# define HTTPS connection
https = Net::HTTP.new(uri.host, uri.port)
https.use_ssl = true
https.verify_mode = OpenSSL::SSL::VERIFY_NONE

# define headers
request.initialize_http_header(headers)
request["User-Agent"] = "GettImagesApiSdk/#{GettyImagesApi::VERSION} (#{os} ; Ruby #{RUBY_VERSION})"
request["Api-Key"] = api_key
request["Authorization"] = "Bearer #{bearer_token}" unless bearer_token.empty?
Expand Down
10 changes: 8 additions & 2 deletions lib/Images/Images.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

class Images < RequestBase

attr_accessor :asset_id
attr_accessor :asset_id, :accept_language

API_ROUTE = "/v3/images" # mashery endpoint
QUERY_PARAMS_NAMES = ["ids","fields"]
Expand All @@ -24,10 +24,16 @@ def with_id(id)
return self
end

public
def with_accept_language(language)
@accept_language = {"Accept-Language" => language}
return self
end

public
def execute
self.asset_id.nil? ? uri = API_ROUTE : uri = API_ROUTE + "/" + self.asset_id
return @http_helper.get(uri, @query_params)
return @http_helper.get(uri, @query_params, @accept_language)
end

end
13 changes: 10 additions & 3 deletions lib/Search/SearchImages.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

class SearchImages < RequestBase

API_ROUTE = "/v3/search/images" # mashery endpoint
attr_accessor :accept_language

API_ROUTE = "/v3/search/images" # mashery endpoint
QUERY_PARAMS_NAMES = ["age_of_people","artists","collection_codes","collections_filter_type","color","compositions","embed_content_only","ethnicity","event_ids","exclude_nudity","fields",
"file_types","graphical_styles","keyword_ids","license_models","minimum_size","number_of_people","orientations","page","page_size","phrase","prestige_content_only","product_types",
"sort_order","specific_people"]
Expand All @@ -23,10 +25,15 @@ class SearchImages < RequestBase
end
return self
end
end
end

def with_accept_language(language)
@accept_language = {"Accept-Language" => language}
return self
end

def execute
return @http_helper.get(API_ROUTE, @query_params)
return @http_helper.get(API_ROUTE, @query_params, @accept_language)
end

end
Expand Down
11 changes: 9 additions & 2 deletions lib/Search/SearchImagesCreative.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

class SearchImagesCreative < RequestBase

attr_accessor :accept_language

API_ROUTE = "/v3/search/images/creative" # mashery endpoint
QUERY_PARAMS_NAMES = ["age_of_people","artists","collection_codes","collections_filter_type","color","compositions","embed_content_only","ethnicity","exclude_nudity","fields","file_types",
"graphical_styles","keyword_ids","license_models","minimum_size","number_of_people","orientations","page","page_size","phrase","prestige_content_only","product_types",
Expand All @@ -23,10 +25,15 @@ class SearchImagesCreative < RequestBase
end
return self
end
end
end

def with_accept_language(language)
@accept_language = {"Accept-Language" => language}
return self
end

def execute
return @http_helper.get(API_ROUTE, @query_params)
return @http_helper.get(API_ROUTE, @query_params, @accept_language)
end

end
11 changes: 9 additions & 2 deletions lib/Search/SearchImagesEditorial.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

class SearchImagesEditorial < RequestBase

attr_accessor :accept_language

API_ROUTE = "/v3/search/images/editorial" # mashery endpoint
QUERY_PARAMS_NAMES = ["age_of_people","artists","collection_codes","collections_filter_type","compositions","editorial_segments","embed_content_only","end_date","entity_uris","ethnicity",
"event_ids","exclude_nudity","fields","file_types","graphical_styles","keyword_ids","minimum_quality_rank","minimum_size","number_of_people","orientations","page","page_size","phrase",
Expand All @@ -23,10 +25,15 @@ class SearchImagesEditorial < RequestBase
end
return self
end
end
end

def with_accept_language(language)
@accept_language = {"Accept-Language" => language}
return self
end

def execute
return @http_helper.get(API_ROUTE, @query_params)
return @http_helper.get(API_ROUTE, @query_params, @accept_language)
end

end
11 changes: 9 additions & 2 deletions lib/Search/SearchVideos.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

class SearchVideos < RequestBase

attr_accessor :accept_language

API_ROUTE = "/v3/search/videos" # mashery endpoint
QUERY_PARAMS_NAMES = ["age_of_people","collection_codes","collections_filter_type","editorial_video_types","exclude_nudity","fields","format_available","frame_rates",
"keyword_ids","license_models","page","page_size","phrase","product_types","sort_order","specific_people"]
Expand All @@ -22,10 +24,15 @@ class SearchVideos < RequestBase
end
return self
end
end
end

def with_accept_language(language)
@accept_language = {"Accept-Language" => language}
return self
end

def execute
return @http_helper.get(API_ROUTE, @query_params)
return @http_helper.get(API_ROUTE, @query_params, @accept_language)
end

end
11 changes: 9 additions & 2 deletions lib/Search/SearchVideosCreative.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

class SearchVideosCreative < RequestBase

attr_accessor :accept_language

API_ROUTE = "/v3/search/videos/creative" # mashery endpoint
QUERY_PARAMS_NAMES = ["age_of_people","collection_codes","collections_filter_type","exclude_nudity","fields","format_available","frame_rates",
"keyword_ids","license_models","page","page_size","phrase","product_types","sort_order"]
Expand All @@ -22,10 +24,15 @@ class SearchVideosCreative < RequestBase
end
return self
end
end
end

def with_accept_language(language)
@accept_language = {"Accept-Language" => language}
return self
end

def execute
return @http_helper.get(API_ROUTE, @query_params)
return @http_helper.get(API_ROUTE, @query_params, @accept_language)
end

end
9 changes: 8 additions & 1 deletion lib/Search/SearchVideosEditorial.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

class SearchVideosEditorial < RequestBase

attr_accessor :accept_language

API_ROUTE = "/v3/search/videos/editorial" # mashery endpoint
QUERY_PARAMS_NAMES = ["age_of_people","collection_codes","collections_filter_type","editorial_video_types","entity_uris","exclude_nudity","fields","format_available","frame_rates",
"keyword_ids","page","page_size","phrase","product_types","sort_order","specific_people"]
Expand All @@ -24,8 +26,13 @@ class SearchVideosEditorial < RequestBase
end
end

def with_accept_language(language)
@accept_language = {"Accept-Language" => language}
return self
end

def execute
return @http_helper.get(API_ROUTE, @query_params)
return @http_helper.get(API_ROUTE, @query_params, @accept_language)
end

end
10 changes: 8 additions & 2 deletions lib/Videos/Videos.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

class Videos < RequestBase

attr_accessor :asset_id
attr_accessor :asset_id, :accept_language

API_ROUTE = "/v3/videos" # mashery endpoint
QUERY_PARAMS_NAMES = ["ids","fields"]
Expand All @@ -24,10 +24,16 @@ def with_id(id)
return self
end

public
def with_accept_language(language)
@accept_language = {"Accept-Language" => language}
return self
end

public
def execute
self.asset_id.nil? ? uri = API_ROUTE : uri = API_ROUTE + "/" + self.asset_id
return @http_helper.get(uri, @query_params)
return @http_helper.get(uri, @query_params, @accept_language)
end

end
Loading

0 comments on commit 7df612f

Please sign in to comment.