zendesk / zendesk_api_client_rb

Official Ruby Zendesk API Client
http://developer.zendesk.com/
Apache License 2.0
387 stars 183 forks source link

feat(client): follow redirects #351

Closed Jerska closed 5 years ago

Jerska commented 7 years ago

I'm using https://github.com/zendesk/zendesk_api_client_rb/issues/175 to access help center API endpoints. Following redirects seems to be needed in some cases for the help center.

Will probably not interest you, but just in case you want to move forward on the help center resources (which would be cool, just know that this is needed).

Logging http requests using the httplog gem, we used to get:

D, [2017-10-17T18:57:36.245813 #57535] DEBUG -- : [httplog] Connecting: my-help-center.zendesk.com:443
D, [2017-10-17T18:57:36.744735 #57535] DEBUG -- : [httplog] Sending: GET http://my-help-center.zendesk.com:443/api/v2/help_center/articles
D, [2017-10-17T18:57:36.744814 #57535] DEBUG -- : [httplog] Data:
D, [2017-10-17T18:57:36.977728 #57535] DEBUG -- : [httplog] Status: 302
D, [2017-10-17T18:57:36.977845 #57535] DEBUG -- : [httplog] Benchmark: 0.221957 seconds
D, [2017-10-17T18:57:36.977924 #57535] DEBUG -- : [httplog] Response:
<html><body>You are being <a href="https://my-help-center.zendesk.com/api/v2/help_center/en-us/articles">redirected</a>.</body></html>
/Users/jerska/.rvm/gems/ruby-2.3.1/gems/zendesk_api-1.14.0/lib/zendesk_api/middleware/response/raise_error.rb:20:in `on_complete': the server responded with status 302 (ZendeskAPI::Error::NetworkError)
    from /Users/jerska/.rvm/gems/ruby-2.3.1/gems/faraday-0.9.2/lib/faraday/response.rb:9:in `block in call'
    from /Users/jerska/.rvm/gems/ruby-2.3.1/gems/faraday-0.9.2/lib/faraday/response.rb:57:in `on_complete'
    from /Users/jerska/.rvm/gems/ruby-2.3.1/gems/faraday-0.9.2/lib/faraday/response.rb:8:in `call'
    from /Users/jerska/.rvm/gems/ruby-2.3.1/gems/zendesk_api-1.14.0/lib/zendesk_api/middleware/response/raise_error.rb:8:in `call'
    from /Users/jerska/.rvm/gems/ruby-2.3.1/gems/faraday-0.9.2/lib/faraday/rack_builder.rb:139:in `build_response'
    from /Users/jerska/.rvm/gems/ruby-2.3.1/gems/faraday-0.9.2/lib/faraday/connection.rb:377:in `run_request'
    from /Users/jerska/.rvm/gems/ruby-2.3.1/gems/faraday-0.9.2/lib/faraday/connection.rb:140:in `get'
    from /Users/jerska/.rvm/gems/ruby-2.3.1/gems/zendesk_api-1.14.0/lib/zendesk_api/collection.rb:400:in `get_response'
    from /Users/jerska/.rvm/gems/ruby-2.3.1/gems/zendesk_api-1.14.0/lib/zendesk_api/collection.rb:189:in `fetch!'
    from /Users/jerska/.rvm/gems/ruby-2.3.1/gems/zendesk_api-1.14.0/lib/zendesk_api/collection.rb:112:in `count!'
    from /Users/jerska/algolia/zendesk/crawler/crawler.rb:41:in `crawl_and_index'
    from ./run:6:in `<main>'

Using this patch you get:

D, [2017-10-17T19:25:40.910346 #63165] DEBUG -- : [httplog] Connecting: teachable.zendesk.com:443
D, [2017-10-17T19:25:41.437716 #63165] DEBUG -- : [httplog] Sending: GET http://teachable.zendesk.com:443/api/v2/help_center/articles
D, [2017-10-17T19:25:41.437803 #63165] DEBUG -- : [httplog] Data:
D, [2017-10-17T19:25:41.661800 #63165] DEBUG -- : [httplog] Status: 302
D, [2017-10-17T19:25:41.661913 #63165] DEBUG -- : [httplog] Benchmark: 0.213368 seconds
D, [2017-10-17T19:25:41.661990 #63165] DEBUG -- : [httplog] Response:
<html><body>You are being <a href="https://teachable.zendesk.com/api/v2/help_center/en-us/articles">redirected</a>.</body></html>
D, [2017-10-17T19:25:41.672379 #63165] DEBUG -- : [httplog] Connecting: teachable.zendesk.com:443
D, [2017-10-17T19:25:42.141488 #63165] DEBUG -- : [httplog] Sending: GET http://teachable.zendesk.com:443/api/v2/help_center/en-us/articles
D, [2017-10-17T19:25:42.141577 #63165] DEBUG -- : [httplog] Data:
D, [2017-10-17T19:25:42.817823 #63165] DEBUG -- : [httplog] Status: 200
D, [2017-10-17T19:25:42.817891 #63165] DEBUG -- : [httplog] Benchmark: 0.676201 seconds
D, [2017-10-17T19:25:42.821855 #63165] DEBUG -- : [httplog] Response:/* Body content not needed */
nogates commented 5 years ago

Hello @Jerska! First of all, sorry for replying to this almost 2 years late 😬

Also, zendesk api client is built on top of Faraday, which means is very easy to use extra middleware, like the one you suggested here: https://github.com/lostisland/faraday_middleware

# define your zendesk client
client = ZendeskAPI::Client.new { |client| ... }
# add faray middlewares
client.connection.builder.use FaradayMiddleware::FollowRedirects

I think this might be a better option instead of adding more code to our library.

I am close this for now, but do not hesitate to open it again if you have more questions