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

Allow Faraday to follow redirects #83

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open

Allow Faraday to follow redirects #83

wants to merge 1 commit into from

Conversation

wwboynton
Copy link

@wwboynton wwboynton commented Jun 1, 2017

Apparently some BitBucket endpoints will serve a 302 redirect. This was causing an error.

For example:

irb(main):035:0> pp b.pull_requests.diff('my_user', 'my_repo', '1')
RuntimeError: can't modify frozen NilClass
	from /Users/wboynton/.rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/bitbucket_rest_api-0.1.7/lib/bitbucket_rest_api/response/helpers.rb:17:in `block in on_complete'
	from /Users/wboynton/.rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/bitbucket_rest_api-0.1.7/lib/bitbucket_rest_api/response/helpers.rb:17:in `instance_eval'
	from /Users/wboynton/.rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/bitbucket_rest_api-0.1.7/lib/bitbucket_rest_api/response/helpers.rb:17:in `on_complete'
	from /Users/wboynton/.rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/faraday-0.9.2/lib/faraday/response.rb:9:in `block in call'
	from /Users/wboynton/.rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/faraday-0.9.2/lib/faraday/response.rb:57:in `on_complete'
	from /Users/wboynton/.rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/faraday-0.9.2/lib/faraday/response.rb:8:in `call'
	from /Users/wboynton/.rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/faraday_middleware-0.9.2/lib/faraday_middleware/request/encode_json.rb:23:in `call'
	from /Users/wboynton/.rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/bitbucket_rest_api-0.1.7/lib/bitbucket_rest_api/request/basic_auth.rb:14:in `call'
	from /Users/wboynton/.rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/faraday-0.9.2/lib/faraday/request/url_encoded.rb:15:in `call'
	from /Users/wboynton/.rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/faraday-0.9.2/lib/faraday/request/multipart.rb:14:in `call'
	from /Users/wboynton/.rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/faraday-0.9.2/lib/faraday/rack_builder.rb:139:in `build_response'
	from /Users/wboynton/.rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/faraday-0.9.2/lib/faraday/connection.rb:377:in `run_request'
	from /Users/wboynton/.rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/faraday-0.9.2/lib/faraday/connection.rb:140:in `get'
	from /Users/wboynton/.rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/bitbucket_rest_api-0.1.7/lib/bitbucket_rest_api/request.rb:42:in `request'
	from /Users/wboynton/.rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/bitbucket_rest_api-0.1.7/lib/bitbucket_rest_api/repos/pull_request.rb:100:in `diff'
	from (irb):35
	from /Users/wboynton/.rbenv/versions/2.4.1/bin/irb:11:in `<main>'

Following the directions from this Faraday issue, it seemed like all I needed to do was use the FaradayMiddleware::FollowRedirects and explicitly mount an adapter. I did that and the same code began working. You can scrutinize and change this at your leisure, but GET-ting data from (at least) that endpoint will be broken until Faraday is using redirects.

@wwboynton
Copy link
Author

Note, this does not solve the issue that follows that -- the /diff endpoint sends a non-JSON response body, which fails in /response/jsonize.rb. I wasn't sure of the most appropriate way to deal with that in context of the whole library, so I'm patching it for myself and leave you to determine how non-JSON bodies should be handled.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant