Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support query parameters in GET requests #409

Merged
merged 2 commits into from
Aug 19, 2018

Conversation

stanhu
Copy link
Contributor

@stanhu stanhu commented Aug 16, 2018

This makes it possible to send some extra headers (e.g. response-content-disposition):
https://cloud.google.com/storage/docs/xml-api/reference-headers

:host => @host,
:method => "GET",
:path => "#{bucket_name}/#{object_name}"
}), expires)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Layout/IndentHash: Indent the right brace the same as the first position after the preceding left parenthesis.

}, expires)

https_url(options.merge({
:headers => {},

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Layout/IndentHash: Use 2 spaces for indentation in a hash, relative to the first position after the preceding left parenthesis.

:path => "#{bucket_name}/#{object_name}"
}, expires)

https_url(options.merge({

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Style/BracesAroundHashParameters: Redundant curly braces around a hash parameter.

query = [params[:query]].compact
query = if params[:query]
params[:query].compact.map { |k, v| [k.to_s, CGI.escape(v)].join("=") }
else

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Layout/ElseAlignment: Align else with if.

@@ -31,7 +31,12 @@ def url(params, expires)
def host_path_query(params, expires)
params[:headers]["Date"] = expires.to_i
params[:path] = Fog::Google.escape(params[:path]).gsub("%2F", "/")
query = [params[:query]].compact
query = if params[:query]

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Style/ConditionalAssignment: Assign variables inside of conditionals

raise ArgumentError.new("bucket_name is required") unless bucket_name
raise ArgumentError.new("object_name is required") unless object_name
https_url({

https_url(options.merge({

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Style/BracesAroundHashParameters: Redundant curly braces around a hash parameter.

:headers => {},
:host => @host,
:method => "GET",
:path => "#{bucket_name}/#{object_name}"
}, expires)
}), expires)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Layout/IndentHash: Indent the right brace the same as the first position after the preceding left parenthesis.

raise ArgumentError.new("bucket_name is required") unless bucket_name
raise ArgumentError.new("object_name is required") unless object_name

https_url({
https_url(options.merge({

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Style/BracesAroundHashParameters: Redundant curly braces around a hash parameter.

:headers => {},
:host => @host,
:method => "GET",
:path => "#{bucket_name}/#{object_name}"
}, expires)
}), expires)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Layout/IndentHash: Indent the right brace the same as the first position after the preceding left parenthesis.

raise ArgumentError.new("bucket_name is required") unless bucket_name
raise ArgumentError.new("object_name is required") unless object_name
http_url({
http_url(options.merge({

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Style/BracesAroundHashParameters: Redundant curly braces around a hash parameter.

@stanhu stanhu force-pushed the sh-support-query-params branch from 9a8831a to 78840c4 Compare August 16, 2018 18:50
url = @client.get_object_url("bucket",
"just some file.json",
Time.now + 2 * 60,
{ query: { "Response-Content-Disposition" => 'inline; filename="test.json"' } })

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Style/BracesAroundHashParameters: Redundant curly braces around a hash parameter.

@stanhu stanhu force-pushed the sh-support-query-params branch 3 times, most recently from c677d00 to af6301e Compare August 16, 2018 19:08
@stanhu
Copy link
Contributor Author

stanhu commented Aug 17, 2018

@Temikus Would you mind taking a look at this? This is affecting lots of people trying to use GitLab with Google Cloud Storage (https://gitlab.com/gitlab-org/gitlab-ce/issues/49957#note_95023022).

@codecov
Copy link

codecov bot commented Aug 18, 2018

Codecov Report

Merging #409 into master will decrease coverage by 0.03%.
The diff coverage is 66.66%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #409      +/-   ##
==========================================
- Coverage   84.14%   84.11%   -0.04%     
==========================================
  Files         339      339              
  Lines        5766     5771       +5     
==========================================
+ Hits         4852     4854       +2     
- Misses        914      917       +3
Impacted Files Coverage Δ
lib/fog/storage/google_json/models/files.rb 97.56% <100%> (ø) ⬆️
...orage/google_json/requests/get_object_https_url.rb 100% <100%> (ø) ⬆️
...torage/google_json/requests/get_object_http_url.rb 100% <100%> (ø) ⬆️
lib/fog/storage/google_json/models/file.rb 87.14% <50%> (ø) ⬆️
lib/fog/storage/google_json/utils.rb 82.6% <50%> (-7.4%) ⬇️
...fog/storage/google_json/requests/get_object_url.rb 63.63% <50%> (ø) ⬆️
lib/fog/storage/google_json/mock.rb 75% <50%> (-5%) ⬇️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update e505e2b...b54b192. Read the comment docs.

Copy link
Member

@Temikus Temikus left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall looks good, please, check the escape method though. If I'm missing something - just let me know!


if params[:query]
filtered = params[:query].reject { |k, v| k.nil? || v.nil? }
query = filtered.map { |k, v| [k.to_s, CGI.escape(v)].join("=") }
Copy link
Member

@Temikus Temikus Aug 18, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Be aware that CGI.escape inserts + instead of %20 when escaping whitespaces which breaks certain behaviours.

If you plan on whitespace being in the query - check if you need to use Fog::Google.escape instead.
See #357


if params[:query]
filtered = params[:query].reject { |k, v| k.nil? || v.nil? }
query = filtered.map { |k, v| [k.to_s, CGI.escape(v)].join("=") }
Copy link
Member

@Temikus Temikus Aug 18, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ditto

Be aware that CGI.escape inserts + instead of %20 when escaping whitespaces which breaks certain behaviours.

If you plan on whitespace being in the query - check if you need to use Fog::Google.escape instead.
See #357

@@ -12,6 +12,16 @@ def teardown
Fog.unmock!
end

def test_get_url_path_has_query_params
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for the tests ❤️

Copy link
Contributor Author

@stanhu stanhu Aug 18, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had an equivalent test for the JSON request API, but I saw this error:

NoMethodError:         NoMethodError: undefined method `issuer' for nil:NilClass
            /Users/stanhu/github/fog-google/lib/fog/storage/google_json/utils.rb:30:in `host_path_query'
            /Users/stanhu/github/fog-google/lib/fog/storage/google_json/utils.rb:10:in `https_url'
            /Users/stanhu/github/fog-google/lib/fog/storage/google_json/requests/get_object_https_url.rb:9:in `get_object_https_url'
            /Users/stanhu/github/fog-google/lib/fog/storage/google_json/requests/get_object_url.rb:16:in `get_object_url'
            /Users/stanhu/github/fog-google/test/unit/storage/test_json_requests.rb:18:in `test_get_url_path_has_query_params'

I assume client.issuer comes from Google::Auth::ServiceAccountCredentials#make_creds, but I punted on further investigation.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, @client is nil in the test. Not sure where that's supposed to come from.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, I did some simple mocking. Not sure if it's ideal. Take a look.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Our mocking is not ideal either as I've just started redoing unit tests after 1.0 upgrade, so no worries :)

Thank you for the tests, I'll refactor the client into something more idiomatic as a part 2.0 refactor.

raise ArgumentError.new("bucket_name is required") unless bucket_name
raise ArgumentError.new("object_name is required") unless object_name

https_url({
https_url(options.merge({
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: Make this consistent with the other options.merge

@Temikus
Copy link
Member

Temikus commented Aug 18, 2018

@stanhu And I do see you're blocked on this so I'm willing to work with you to push a release right away.

@stanhu stanhu force-pushed the sh-support-query-params branch from 3cb2bd1 to 588fb5c Compare August 18, 2018 20:46
@stanhu
Copy link
Contributor Author

stanhu commented Aug 18, 2018

@Temikus Good catch. I'm not sure why in my end-to-end testing the escaping of the + wasn't an issue, but I think avoiding that is the right behavior.

@stanhu stanhu force-pushed the sh-support-query-params branch from 588fb5c to 574dce1 Compare August 18, 2018 20:54
def initialize(options = {})
shared_initialize(options[:google_project], GOOGLE_STORAGE_JSON_API_VERSION, GOOGLE_STORAGE_JSON_BASE_URL)
@client = MockClient.new('test')

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Style/StringLiterals: Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping.

Copy link
Member

@Temikus Temikus left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thank you!

@Temikus
Copy link
Member

Temikus commented Aug 19, 2018

Merging this in, I'll prep a release by EOD tomorrow (Sydney time)

@Temikus Temikus merged commit 490b3e4 into fog:master Aug 19, 2018
stanhu added a commit to gitlabhq/gitlabhq that referenced this pull request Aug 28, 2018
This is needed to support query parameters in `Fog::Storage::Google`.
See fog/fog-google#409.

Relates to https://gitlab.com/gitlab-org/gitlab-ce/issues/49957
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants