faraday 0.17.1 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (134) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +69 -9
  3. data/LICENSE.md +1 -1
  4. data/README.md +17 -347
  5. data/Rakefile +1 -7
  6. data/examples/client_spec.rb +65 -0
  7. data/examples/client_test.rb +79 -0
  8. data/lib/faraday.rb +94 -176
  9. data/lib/faraday/adapter.rb +83 -22
  10. data/lib/faraday/adapter/em_http.rb +143 -100
  11. data/lib/faraday/adapter/em_http_ssl_patch.rb +24 -18
  12. data/lib/faraday/adapter/em_synchrony.rb +104 -60
  13. data/lib/faraday/adapter/em_synchrony/parallel_manager.rb +18 -15
  14. data/lib/faraday/adapter/excon.rb +98 -56
  15. data/lib/faraday/adapter/httpclient.rb +83 -59
  16. data/lib/faraday/adapter/net_http.rb +130 -63
  17. data/lib/faraday/adapter/net_http_persistent.rb +51 -28
  18. data/lib/faraday/adapter/patron.rb +80 -43
  19. data/lib/faraday/adapter/rack.rb +30 -13
  20. data/lib/faraday/adapter/test.rb +86 -53
  21. data/lib/faraday/adapter/typhoeus.rb +4 -1
  22. data/lib/faraday/adapter_registry.rb +30 -0
  23. data/lib/faraday/autoload.rb +47 -36
  24. data/lib/faraday/connection.rb +312 -182
  25. data/lib/faraday/dependency_loader.rb +39 -0
  26. data/lib/faraday/encoders/flat_params_encoder.rb +105 -0
  27. data/lib/faraday/encoders/nested_params_encoder.rb +176 -0
  28. data/lib/faraday/error.rb +46 -25
  29. data/lib/faraday/file_part.rb +128 -0
  30. data/lib/faraday/logging/formatter.rb +105 -0
  31. data/lib/faraday/middleware.rb +12 -28
  32. data/lib/faraday/middleware_registry.rb +129 -0
  33. data/lib/faraday/options.rb +38 -193
  34. data/lib/faraday/options/connection_options.rb +22 -0
  35. data/lib/faraday/options/env.rb +181 -0
  36. data/lib/faraday/options/proxy_options.rb +28 -0
  37. data/lib/faraday/options/request_options.rb +22 -0
  38. data/lib/faraday/options/ssl_options.rb +59 -0
  39. data/lib/faraday/param_part.rb +53 -0
  40. data/lib/faraday/parameters.rb +4 -197
  41. data/lib/faraday/rack_builder.rb +77 -65
  42. data/lib/faraday/request.rb +86 -44
  43. data/lib/faraday/request/authorization.rb +44 -30
  44. data/lib/faraday/request/basic_authentication.rb +14 -7
  45. data/lib/faraday/request/instrumentation.rb +45 -27
  46. data/lib/faraday/request/multipart.rb +86 -48
  47. data/lib/faraday/request/retry.rb +197 -171
  48. data/lib/faraday/request/token_authentication.rb +15 -10
  49. data/lib/faraday/request/url_encoded.rb +43 -23
  50. data/lib/faraday/response.rb +24 -14
  51. data/lib/faraday/response/logger.rb +22 -69
  52. data/lib/faraday/response/raise_error.rb +49 -18
  53. data/lib/faraday/utils.rb +38 -247
  54. data/lib/faraday/utils/headers.rb +139 -0
  55. data/lib/faraday/utils/params_hash.rb +61 -0
  56. data/spec/external_adapters/faraday_specs_setup.rb +14 -0
  57. data/spec/faraday/adapter/em_http_spec.rb +47 -0
  58. data/spec/faraday/adapter/em_synchrony_spec.rb +16 -0
  59. data/spec/faraday/adapter/excon_spec.rb +49 -0
  60. data/spec/faraday/adapter/httpclient_spec.rb +73 -0
  61. data/spec/faraday/adapter/net_http_persistent_spec.rb +57 -0
  62. data/spec/faraday/adapter/net_http_spec.rb +64 -0
  63. data/spec/faraday/adapter/patron_spec.rb +18 -0
  64. data/spec/faraday/adapter/rack_spec.rb +8 -0
  65. data/spec/faraday/adapter/test_spec.rb +260 -0
  66. data/spec/faraday/adapter/typhoeus_spec.rb +7 -0
  67. data/spec/faraday/adapter_registry_spec.rb +28 -0
  68. data/spec/faraday/adapter_spec.rb +55 -0
  69. data/spec/faraday/composite_read_io_spec.rb +80 -0
  70. data/spec/faraday/connection_spec.rb +691 -0
  71. data/spec/faraday/error_spec.rb +0 -57
  72. data/spec/faraday/middleware_spec.rb +26 -0
  73. data/spec/faraday/options/env_spec.rb +70 -0
  74. data/spec/faraday/options/options_spec.rb +297 -0
  75. data/spec/faraday/options/proxy_options_spec.rb +37 -0
  76. data/spec/faraday/options/request_options_spec.rb +19 -0
  77. data/spec/faraday/params_encoders/flat_spec.rb +42 -0
  78. data/spec/faraday/params_encoders/nested_spec.rb +142 -0
  79. data/spec/faraday/rack_builder_spec.rb +345 -0
  80. data/spec/faraday/request/authorization_spec.rb +88 -0
  81. data/spec/faraday/request/instrumentation_spec.rb +76 -0
  82. data/spec/faraday/request/multipart_spec.rb +302 -0
  83. data/spec/faraday/request/retry_spec.rb +242 -0
  84. data/spec/faraday/request/url_encoded_spec.rb +83 -0
  85. data/spec/faraday/request_spec.rb +120 -0
  86. data/spec/faraday/response/logger_spec.rb +220 -0
  87. data/spec/faraday/response/middleware_spec.rb +68 -0
  88. data/spec/faraday/response/raise_error_spec.rb +60 -16
  89. data/spec/faraday/response_spec.rb +75 -0
  90. data/spec/faraday/utils/headers_spec.rb +82 -0
  91. data/spec/faraday/utils_spec.rb +56 -0
  92. data/spec/faraday_spec.rb +37 -0
  93. data/spec/spec_helper.rb +63 -35
  94. data/spec/support/disabling_stub.rb +14 -0
  95. data/spec/support/fake_safe_buffer.rb +15 -0
  96. data/spec/support/helper_methods.rb +133 -0
  97. data/spec/support/shared_examples/adapter.rb +104 -0
  98. data/spec/support/shared_examples/params_encoder.rb +18 -0
  99. data/spec/support/shared_examples/request_method.rb +234 -0
  100. data/spec/support/streaming_response_checker.rb +35 -0
  101. data/spec/support/webmock_rack_app.rb +68 -0
  102. metadata +80 -37
  103. data/lib/faraday/deprecate.rb +0 -101
  104. data/lib/faraday/upload_io.rb +0 -67
  105. data/spec/faraday/deprecate_spec.rb +0 -69
  106. data/test/adapters/default_test.rb +0 -14
  107. data/test/adapters/em_http_test.rb +0 -30
  108. data/test/adapters/em_synchrony_test.rb +0 -32
  109. data/test/adapters/excon_test.rb +0 -30
  110. data/test/adapters/httpclient_test.rb +0 -34
  111. data/test/adapters/integration.rb +0 -263
  112. data/test/adapters/logger_test.rb +0 -136
  113. data/test/adapters/net_http_persistent_test.rb +0 -114
  114. data/test/adapters/net_http_test.rb +0 -79
  115. data/test/adapters/patron_test.rb +0 -40
  116. data/test/adapters/rack_test.rb +0 -38
  117. data/test/adapters/test_middleware_test.rb +0 -157
  118. data/test/adapters/typhoeus_test.rb +0 -38
  119. data/test/authentication_middleware_test.rb +0 -65
  120. data/test/composite_read_io_test.rb +0 -109
  121. data/test/connection_test.rb +0 -738
  122. data/test/env_test.rb +0 -268
  123. data/test/helper.rb +0 -75
  124. data/test/live_server.rb +0 -67
  125. data/test/middleware/instrumentation_test.rb +0 -88
  126. data/test/middleware/retry_test.rb +0 -282
  127. data/test/middleware_stack_test.rb +0 -260
  128. data/test/multibyte.txt +0 -1
  129. data/test/options_test.rb +0 -333
  130. data/test/parameters_test.rb +0 -157
  131. data/test/request_middleware_test.rb +0 -126
  132. data/test/response_middleware_test.rb +0 -72
  133. data/test/strawberry.rb +0 -2
  134. data/test/utils_test.rb +0 -98
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 16a3bb1447dde55604486c5a3a912281318ca8d0a211008623bcafb270a104ea
4
- data.tar.gz: 966aaefe77900eab1bd1687bf7ce57950e0dbc8fb8dcd7a062fddf6be4393c4f
3
+ metadata.gz: 47236f354f9fda55254c8bd2b9e3552de12e8894fcecea0035ffbeb3ccc46b9c
4
+ data.tar.gz: f6eb36d3da02314fe799368c1ff4c17c5841f0badd18c5a89772e00bae43065c
5
5
  SHA512:
6
- metadata.gz: b016c9c3ea03bba4a9a40325f0494ef8a096e6c67d46ced7cb13da8e2bfeafca92107bc971f276ea883249219e13fa3f5f67e6c73d43c6ce66c6827108457e86
7
- data.tar.gz: 23e0a83830645895da998a9961d43747de7da1ee9428ea15a68776e1edb346f67c1b66d9403f3e4e0b5c40790e6adbf9bfa8dbb26db0cf79d4fafcb292c9e7ff
6
+ metadata.gz: 300153861873b2a5b3a3a9f552c47a2b70dd083ffd5478d8eca2c4c2005e5a74d09d962b51fb9a780ea90bb1ac87bdfa902639e86b2147d5b4d4374791789bd1
7
+ data.tar.gz: 3628d85508cdd669111ae6175b6b5a191380d0d405f345612f6342e487aec0f3cc9d2e1726a2cdde7d82ee86fae82aa41f4a62ea330343598383ae3f644b5a85
@@ -1,5 +1,65 @@
1
1
  # Faraday Changelog
2
2
 
3
+ ## v1.0
4
+
5
+ Features:
6
+
7
+ * Add #trace support to Faraday::Connection #861 (@technoweenie)
8
+ * Add the log formatter that is easy to override and safe to inherit #889 (@prikha)
9
+ * Support standalone adapters #941 (@iMacTia)
10
+ * Introduce Faraday::ConflictError for 409 response code #979 (@lucasmoreno)
11
+ * Add support for setting `read_timeout` option separately #1003 (@springerigor)
12
+ * Refactor and cleanup timeout settings across adapters #1022 (@technoweenie)
13
+ * Create ParamPart class to allow multipart posts with JSON content and file upload at the same time #1017 (@jeremy-israel)
14
+ * Copy UploadIO const -> FilePart for consistency with ParamPart #1018, #1021 (@technoweenie)
15
+ * Implement streaming responses in the Excon adapter #1026 (@technoweenie)
16
+ * Add default implementation of `Middleware#close`. #1069 (@ioquatix)
17
+ * Add `Adapter#close` so that derived classes can call super. #1091 (@ioquatix)
18
+ * Add log_level option to logger default formatter #1079 (@amrrbakry)
19
+ * Fix empty array for FlatParamsEncoder `{key: []} -> "key="` #1084 (@mrexox)
20
+
21
+ Bugs:
22
+
23
+ * Explicitly require date for DateTime library in Retry middleware #844 (@nickpresta)
24
+ * Refactor Adapter as final endpoints #846 (@iMacTia)
25
+ * Separate Request and Response bodies in Faraday::Env #847 (@iMacTia)
26
+ * Implement Faraday::Connection#options to make HTTP requests with the OPTIONS verb. #857 (@technoweenie)
27
+ * Multipart: Drop Ruby 1.8 String behavior compat #892 (@olleolleolle)
28
+ * Fix Ruby warnings in Faraday::Options.memoized #962 (@technoweenie)
29
+ * Allow setting min/max SSL version for a Net::HTTP::Persistent connection #972, #973 (@bdewater, @olleolleolle)
30
+ * Fix instances of frozen empty string literals #1040 (@BobbyMcWho)
31
+ * remove temp_proxy and improve proxy tests #1063 (@technoweenie)
32
+ * improve error initializer consistency #1095 (@technoweenie)
33
+
34
+ Misc:
35
+
36
+ * Convert minitest suite to RSpec #832 (@iMacTia, with help from @gaynetdinov, @Insti, @technoweenie)
37
+ * Major effort to update code to RuboCop standards. #854 (@olleolleolle, @iMacTia, @technoweenie, @htwroclau, @jherdman, @Drenmi, @Insti)
38
+ * Rubocop #1044, #1047 (@BobbyMcWho, @olleolleolle)
39
+ * Documentation tweaks (@adsteel, @Hubro, @iMacTia, @olleolleolle, @technoweenie)
40
+ * Update license year #981 (@Kevin-Kawai)
41
+ * Configure Jekyll plugin jekyll-remote-theme to support Docker usage #999 (@Lewiscowles1986)
42
+ * Fix Ruby 2.7 warnings #1009 (@tenderlove)
43
+ * Cleanup adapter connections #1023 (@technoweenie)
44
+ * Describe clearing cached stubs #1045 (@viraptor)
45
+ * Add project metadata to the gemspec #1046 (@orien)
46
+
47
+ ## v0.17.3
48
+
49
+ Fixes:
50
+
51
+ * Reverts changes in error classes hierarchy. #1092 (@iMacTia)
52
+ * Fix Ruby 1.9 syntax errors and improve Error class testing #1094 (@BanzaiMan,
53
+ @mrexox, @technoweenie)
54
+
55
+ Misc:
56
+
57
+ * Stops using `&Proc.new` for block forwarding. #1083 (@olleolleolle)
58
+ * Update CI to test against ruby 2.0-2.7 #1087, #1099 (@iMacTia, @olleolleolle,
59
+ @technoweenie)
60
+ * require FARADAY_DEPRECATE=warn to show Faraday v1.0 deprecation warnings
61
+ #1098 (@technoweenie)
62
+
3
63
  ## v0.17.1
4
64
 
5
65
  Final release before Faraday v1.0, with important fixes for Ruby 2.7.
@@ -7,7 +67,7 @@ Final release before Faraday v1.0, with important fixes for Ruby 2.7.
7
67
  Fixes:
8
68
 
9
69
  * RaiseError response middleware raises exception if HTTP client returns a nil
10
- status. (#1042)
70
+ status. #1042 (@jonnyom, @BobbyMcWho)
11
71
 
12
72
  Misc:
13
73
 
@@ -69,17 +129,17 @@ Fixes:
69
129
 
70
130
  Features:
71
131
 
72
- * Allow overriding env proxy (#754)
73
- * Remove legacy Typhoeus adapter (#715)
74
- * External Typhoeus Adapter Compatibility (#748)
75
- * Warn about missing adapter when making a request (#743)
76
- * Faraday::Adapter::Test stubs now support entire urls (with host) (#741)
132
+ * Allow overriding env proxy #754 (@iMacTia)
133
+ * Remove legacy Typhoeus adapter #715 (@olleolleolle)
134
+ * External Typhoeus Adapter Compatibility #748 (@iMacTia)
135
+ * Warn about missing adapter when making a request #743 (@antstorm)
136
+ * Faraday::Adapter::Test stubs now support entire urls (with host) #741 (@erik-escobedo)
77
137
 
78
138
  Fixes:
79
139
 
80
- * If proxy is manually provided, this takes priority over `find_proxy` (#724)
81
- * Fixes the behaviour for Excon's open_timeout (not setting write_timeout anymore) (#731)
82
- * Handle all connection timeout messages in Patron (#687)
140
+ * If proxy is manually provided, this takes priority over `find_proxy` #724 (@iMacTia)
141
+ * Fixes the behaviour for Excon's open_timeout (not setting write_timeout anymore) #731 (@apachelogger)
142
+ * Handle all connection timeout messages in Patron #687 (@stayhero)
83
143
 
84
144
  ## v0.13.1
85
145
 
data/LICENSE.md CHANGED
@@ -1,4 +1,4 @@
1
- Copyright (c) 2009-2017 Rick Olson, Zack Hobson
1
+ Copyright (c) 2009-2020 Rick Olson, Zack Hobson
2
2
 
3
3
  Permission is hereby granted, free of charge, to any person obtaining
4
4
  a copy of this software and associated documentation files (the
data/README.md CHANGED
@@ -1,356 +1,31 @@
1
- # Faraday
1
+ # [![Faraday](./docs/assets/img/repo-card-slim.png)][website]
2
2
 
3
3
  [![Gem Version](https://badge.fury.io/rb/faraday.svg)](https://rubygems.org/gems/faraday)
4
- [![Build Status](https://travis-ci.org/lostisland/faraday.svg?branch=master)](https://travis-ci.org/lostisland/faraday)
5
- [![Coverage Status](https://coveralls.io/repos/github/lostisland/faraday/badge.svg?branch=master)](https://coveralls.io/github/lostisland/faraday?branch=master)
6
- [![Code Climate](https://codeclimate.com/github/lostisland/faraday/badges/gpa.svg)](https://codeclimate.com/github/lostisland/faraday)
4
+ [![GitHub Actions CI](https://github.com/lostisland/faraday/workflows/CI/badge.svg)](https://github.com/lostisland/faraday/actions?query=workflow%3ACI)
5
+ [![Maintainability](https://api.codeclimate.com/v1/badges/f869daab091ceef1da73/maintainability)](https://codeclimate.com/github/lostisland/faraday/maintainability)
7
6
  [![Gitter](https://badges.gitter.im/lostisland/faraday.svg)](https://gitter.im/lostisland/faraday?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge)
8
7
 
9
8
 
10
- Faraday is an HTTP client lib that provides a common interface over many
9
+ Faraday is an HTTP client library that provides a common interface over many
11
10
  adapters (such as Net::HTTP) and embraces the concept of Rack middleware when
12
11
  processing the request/response cycle.
13
12
 
14
- Faraday supports these adapters out of the box:
13
+ ## Getting Started
15
14
 
16
- * [Net::HTTP][net_http] _(default)_
17
- * [Net::HTTP::Persistent][persistent]
18
- * [Excon][]
19
- * [Patron][]
20
- * [EventMachine][]
21
- * [HTTPClient][]
22
-
23
- Adapters are slowly being moved into their own gems, or bundled with HTTP clients:
24
-
25
- * [Typhoeus][]
26
-
27
- It also includes a Rack adapter for hitting loaded Rack applications through
28
- Rack::Test, and a Test adapter for stubbing requests by hand.
29
-
30
- ## API documentation
31
-
32
- Available at [rubydoc.info](http://www.rubydoc.info/gems/faraday).
33
-
34
- ## Usage
35
-
36
- ### Basic Use
37
-
38
- ```ruby
39
- response = Faraday.get 'http://sushi.com/nigiri/sake.json'
40
- ```
41
- A simple `get` request can be performed by using the syntax described above. This works if you don't need to set up anything; you can roll with just the default middleware
42
- stack and default adapter (see [Faraday::RackBuilder#initialize](https://github.com/lostisland/faraday/blob/master/lib/faraday/rack_builder.rb)).
43
-
44
- A more flexible way to use Faraday is to start with a Connection object. If you want to keep the same defaults, you can use this syntax:
45
-
46
- ```ruby
47
- conn = Faraday.new(:url => 'http://www.example.com')
48
- response = conn.get '/users' # GET http://www.example.com/users'
49
- ```
50
-
51
- Connections can also take an options hash as a parameter or be configured by using a block. Checkout the section called [Advanced middleware usage](#advanced-middleware-usage) for more details about how to use this block for configurations.
52
- Since the default middleware stack uses url\_encoded middleware and default adapter, use them on building your own middleware stack.
53
-
54
- ```ruby
55
- conn = Faraday.new(:url => 'http://sushi.com') do |faraday|
56
- faraday.request :url_encoded # form-encode POST params
57
- faraday.response :logger # log requests to $stdout
58
- faraday.adapter Faraday.default_adapter # make requests with Net::HTTP
59
- end
60
-
61
- # Filter sensitive information from logs with a regex matcher
62
-
63
- conn = Faraday.new(:url => 'http://sushi.com/api_key=s3cr3t') do |faraday|
64
- faraday.request :url_encoded # form-encode POST params
65
- faraday.response :logger do | logger |
66
- logger.filter(/(api_key=)(\w+)/,'\1[REMOVED]')
67
- end
68
- faraday.adapter Faraday.default_adapter # make requests with Net::HTTP
69
- end
70
- ```
71
-
72
- Once you have the connection object, use it to make HTTP requests. You can pass parameters to it in a few different ways:
73
-
74
- ```ruby
75
- ## GET ##
76
-
77
- response = conn.get '/nigiri/sake.json' # GET http://sushi.com/nigiri/sake.json
78
- response.body
79
-
80
- conn.get '/nigiri', { :name => 'Maguro' } # GET http://sushi.com/nigiri?name=Maguro
81
-
82
- conn.get do |req| # GET http://sushi.com/search?page=2&limit=100
83
- req.url '/search', :page => 2
84
- req.params['limit'] = 100
85
- end
86
-
87
- ## POST ##
88
-
89
- conn.post '/nigiri', { :name => 'Maguro' } # POST "name=maguro" to http://sushi.com/nigiri
90
- ```
91
-
92
- Some configuration options can be adjusted per request:
93
-
94
- ```ruby
95
- # post payload as JSON instead of "www-form-urlencoded" encoding:
96
- conn.post do |req|
97
- req.url '/nigiri'
98
- req.headers['Content-Type'] = 'application/json'
99
- req.body = '{ "name": "Unagi" }'
100
- end
101
-
102
- ## Per-request options ##
103
-
104
- conn.get do |req|
105
- req.url '/search'
106
- req.options.timeout = 5 # open/read timeout in seconds
107
- req.options.open_timeout = 2 # connection open timeout in seconds
108
- end
109
- ```
110
-
111
- And you can inject arbitrary data into the request using the `context` option:
112
-
113
- ```ruby
114
- # Anything you inject using context option will be available in the env on all middlewares
115
-
116
- conn.get do |req|
117
- req.url '/search'
118
- req.options.context = {
119
- foo: 'foo',
120
- bar: 'bar'
121
- }
122
- end
123
- ```
124
-
125
- ### Changing how parameters are serialized
126
-
127
- Sometimes you need to send the same URL parameter multiple times with different
128
- values. This requires manually setting the parameter encoder and can be done on
129
- either per-connection or per-request basis.
130
-
131
- ```ruby
132
- # per-connection setting
133
- conn = Faraday.new :request => { :params_encoder => Faraday::FlatParamsEncoder }
134
-
135
- conn.get do |req|
136
- # per-request setting:
137
- # req.options.params_encoder = my_encoder
138
- req.params['roll'] = ['california', 'philadelphia']
139
- end
140
- # GET 'http://sushi.com?roll=california&roll=philadelphia'
141
- ```
142
-
143
- The value of Faraday `params_encoder` can be any object that responds to:
144
-
145
- * `encode(hash) #=> String`
146
- * `decode(string) #=> Hash`
147
-
148
- The encoder will affect both how query strings are processed and how POST bodies
149
- get serialized. The default encoder is Faraday::NestedParamsEncoder.
150
-
151
- ## Authentication
152
-
153
- Basic and Token authentication are handled by Faraday::Request::BasicAuthentication and Faraday::Request::TokenAuthentication respectively. These can be added as middleware manually or through the helper methods.
154
-
155
- ```ruby
156
- Faraday.new(...) do |conn|
157
- conn.basic_auth('username', 'password')
158
- end
159
-
160
- Faraday.new(...) do |conn|
161
- conn.token_auth('authentication-token')
162
- end
163
- ```
164
-
165
- ## Proxy
166
-
167
- Faraday will try to automatically infer the proxy settings from your system using `URI#find_proxy`.
168
- This will retrieve them from environment variables such as http_proxy, ftp_proxy, no_proxy, etc.
169
- If for any reason you want to disable this behaviour, you can do so by setting the global varibale `ignore_env_proxy`:
170
-
171
- ```ruby
172
- Faraday.ignore_env_proxy = true
173
- ```
174
-
175
- You can also specify a custom proxy when initializing the connection
176
-
177
- ```ruby
178
- Faraday.new('http://www.example.com', :proxy => 'http://proxy.com')
179
- ```
180
-
181
- ## Advanced middleware usage
182
-
183
- The order in which middleware is stacked is important. Like with Rack, the
184
- first middleware on the list wraps all others, while the last middleware is the
185
- innermost one, so that must be the adapter.
186
-
187
- ```ruby
188
- Faraday.new(...) do |conn|
189
- # POST/PUT params encoders:
190
- conn.request :multipart
191
- conn.request :url_encoded
192
-
193
- # add custom middleware
194
- conn.use MyMiddleware
195
-
196
- # Last middleware must be the adapter:
197
- conn.adapter :net_http
198
- end
199
- ```
200
-
201
- This request middleware setup affects POST/PUT requests in the following way:
202
-
203
- 1. `Request::Multipart` checks for files in the payload, otherwise leaves
204
- everything untouched;
205
- 2. `Request::UrlEncoded` encodes as "application/x-www-form-urlencoded" if not
206
- already encoded or of another type
207
-
208
- Swapping middleware means giving the other priority. Specifying the
209
- "Content-Type" for the request is explicitly stating which middleware should
210
- process it.
211
-
212
- Examples:
213
-
214
- ```ruby
215
- # uploading a file:
216
- payload[:profile_pic] = Faraday::UploadIO.new('/path/to/avatar.jpg', 'image/jpeg')
217
-
218
- # "Multipart" middleware detects files and encodes with "multipart/form-data":
219
- conn.put '/profile', payload
220
- ```
221
-
222
- ## Writing middleware
223
-
224
- Middleware are classes that implement a `call` instance method. They hook into
225
- the request/response cycle.
226
-
227
- ```ruby
228
- def call(request_env)
229
- # do something with the request
230
- # request_env[:request_headers].merge!(...)
231
-
232
- @app.call(request_env).on_complete do |response_env|
233
- # do something with the response
234
- # response_env[:response_headers].merge!(...)
235
- end
236
- end
237
- ```
238
-
239
- It's important to do all processing of the response only in the `on_complete`
240
- block. This enables middleware to work in parallel mode where requests are
241
- asynchronous.
242
-
243
- The `env` is a hash with symbol keys that contains info about the request and,
244
- later, response. Some keys are:
245
-
246
- ```
247
- # request phase
248
- :method - :get, :post, ...
249
- :url - URI for the current request; also contains GET parameters
250
- :body - POST parameters for :post/:put requests
251
- :request_headers
252
-
253
- # response phase
254
- :status - HTTP response status code, such as 200
255
- :body - the response body
256
- :response_headers
257
- ```
258
-
259
- ## Ad-hoc adapters customization
260
-
261
- Faraday is intended to be a generic interface between your code and the adapter. However, sometimes you need to access a feature specific to one of the adapters that is not covered in Faraday's interface.
262
-
263
- When that happens, you can pass a block when specifying the adapter to customize it. The block parameter will change based on the adapter you're using. See below for some examples.
264
-
265
- ### NetHttp
266
- ```ruby
267
- conn = Faraday.new(...) do |f|
268
- f.adapter :net_http do |http| # yields Net::HTTP
269
- http.idle_timeout = 100
270
- http.verify_callback = lambda do | preverify_ok, cert_store |
271
- # do something here...
272
- end
273
- end
274
- end
275
- ```
276
-
277
- ### NetHttpPersistent
278
- ```ruby
279
- conn = Faraday.new(...) do |f|
280
- f.adapter :net_http_persistent, pool_size: 5 do |http| # yields Net::HTTP::Persistent
281
- http.idle_timeout = 100
282
- http.retry_change_requests = true
283
- end
284
- end
285
- ```
286
-
287
- ### Patron
288
- ```ruby
289
- conn = Faraday.new(...) do |f|
290
- f.adapter :patron do |session| # yields Patron::Session
291
- session.max_redirects = 10
292
- end
293
- end
294
- ```
295
-
296
- ### HTTPClient
297
- ```ruby
298
- conn = Faraday.new(...) do |f|
299
- f.adapter :httpclient do |client| # yields HTTPClient
300
- client.keep_alive_timeout = 20
301
- client.ssl_config.timeout = 25
302
- end
303
- end
304
- ```
305
-
306
- ## Using Faraday for testing
307
-
308
- ```ruby
309
- # It's possible to define stubbed request outside a test adapter block.
310
- stubs = Faraday::Adapter::Test::Stubs.new do |stub|
311
- stub.get('/tamago') { |env| [200, {}, 'egg'] }
312
- end
313
-
314
- # You can pass stubbed request to the test adapter or define them in a block
315
- # or a combination of the two.
316
- test = Faraday.new do |builder|
317
- builder.adapter :test, stubs do |stub|
318
- stub.get('/ebi') { |env| [ 200, {}, 'shrimp' ]}
319
- end
320
- end
321
-
322
- # It's also possible to stub additional requests after the connection has
323
- # been initialized. This is useful for testing.
324
- stubs.get('/uni') { |env| [ 200, {}, 'urchin' ]}
325
-
326
- resp = test.get '/tamago'
327
- resp.body # => 'egg'
328
- resp = test.get '/ebi'
329
- resp.body # => 'shrimp'
330
- resp = test.get '/uni'
331
- resp.body # => 'urchin'
332
- resp = test.get '/else' #=> raises "no such stub" error
333
-
334
- # If you like, you can treat your stubs as mocks by verifying that all of
335
- # the stubbed calls were made. NOTE that this feature is still fairly
336
- # experimental: It will not verify the order or count of any stub, only that
337
- # it was called once during the course of the test.
338
- stubs.verify_stubbed_calls
339
- ```
15
+ The best starting point is the [Faraday Website][website], with its introduction and explanation.
16
+ Need more details? See the [Faraday API Documentation][apidoc] to see how it works internally.
340
17
 
341
18
  ## Supported Ruby versions
342
19
 
343
- This library aims to support and is [tested against][travis] the following Ruby
20
+ This library aims to support and is [tested against][actions] the following Ruby
344
21
  implementations:
345
22
 
346
- * Ruby 1.9.3+
347
- * [JRuby][] 1.7+
348
- * [Rubinius][] 2+
23
+ * Ruby 2.4+
349
24
 
350
25
  If something doesn't work on one of these Ruby versions, it's a bug.
351
26
 
352
27
  This library may inadvertently work (or seem to work) on other Ruby
353
- implementations, however support will only be provided for the versions listed
28
+ implementations and versions, however support will only be provided for the versions listed
354
29
  above.
355
30
 
356
31
  If you would like this library to support another Ruby version, you may
@@ -364,21 +39,16 @@ of a major release, support for that Ruby version may be dropped.
364
39
 
365
40
  Do you want to contribute to Faraday?
366
41
  Open the issues page and check for the `help wanted` label!
367
- But before you start coding, please read our [Contributing Guide](https://github.com/lostisland/faraday/blob/master/.github/CONTRIBUTING.md)
42
+ But before you start coding, please read our [Contributing Guide][contributing]
368
43
 
369
44
  ## Copyright
45
+ © 2009 - 2020, the [Faraday Team][faraday_team]. Website and branding design by [Elena Lo Piccolo](https://elelopic.design).
370
46
 
371
- Copyright (c) 2009-2017 [Rick Olson](mailto:technoweenie@gmail.com), Zack Hobson.
372
- See [LICENSE][] for details.
373
-
374
- [net_http]: http://ruby-doc.org/stdlib/libdoc/net/http/rdoc/Net/HTTP.html
375
- [persistent]: https://github.com/drbrain/net-http-persistent
376
- [travis]: https://travis-ci.org/lostisland/faraday
377
- [excon]: https://github.com/excon/excon#readme
378
- [patron]: http://toland.github.io/patron/
379
- [eventmachine]: https://github.com/igrigorik/em-http-request#readme
380
- [httpclient]: https://github.com/nahi/httpclient
381
- [typhoeus]: https://github.com/typhoeus/typhoeus/blob/master/lib/typhoeus/adapters/faraday.rb
47
+ [website]: https://lostisland.github.io/faraday
48
+ [faraday_team]: https://lostisland.github.io/faraday/team
49
+ [contributing]: https://github.com/lostisland/faraday/blob/master/.github/CONTRIBUTING.md
50
+ [apidoc]: http://www.rubydoc.info/gems/faraday
51
+ [actions]: https://github.com/lostisland/faraday/actions
382
52
  [jruby]: http://jruby.org/
383
53
  [rubinius]: http://rubini.us/
384
54
  [license]: LICENSE.md