Skip to content

Use application/hal+json as default Content-Type and add to Accept header #81

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

Merged
merged 1 commit into from
Dec 1, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@

* Your contribution here.

### 0.7.0-pre (December 1, 2014)

This version introduces several backwards incompatible changes. See [UPGRADING](UPGRADING.md) for details.

* [#81](https://github.com/codegram/hyperclient/pull/81): The default Content-Type is now `application/hal+json` - [@koenpunt](https://github.com/koenpunt).

### 0.6.1 (October 17, 2014)

This version introduces several backwards incompatible changes. See [UPGRADING](UPGRADING.md) for details.
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ require 'hyperclient'
api = Hyperclient.new('https://grape-with-roar.herokuapp.com/api')
```

By default, Hyperclient adds `application/json` as `Content-Type` and `Accept` headers. It will also send requests as JSON and parse JSON responses. Specify additional headers or authentication if necessary.
By default, Hyperclient adds `application/hal+json` as `Content-Type` and `Accept` headers. It will also send requests as JSON and parse JSON responses. Specify additional headers or authentication if necessary.

```ruby
api = Hyperclient.new('https://grape-with-roar.herokuapp.com/api') do |client|
Expand Down
13 changes: 13 additions & 0 deletions UPGRADING.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,19 @@
Upgrading Hyperclient
=====================

### Upgrading to >= 0.7.0

#### Changes in default headers

The default `Content-Type` is now `application/hal+json` and the `Accept` header includes `application/hal+json` as well.
If your API only accepts `application/json` as Content-Type, you can override the default headers in the client initialization block as such:

```rb
Hyperclient.new('https://api.example.org/') do |client|
client.headers = { 'Content-Type' => 'application/json', 'Accept' => 'application/json,application/hal+json' }
end
```

### Upgrading to >= 0.6.0

#### Changes in HTTP Error Handling
Expand Down
4 changes: 3 additions & 1 deletion features/steps/default_config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ class Spinach::Features::DefaultConfig < Spinach::FeatureSteps
end

step 'the request should have been sent with the correct JSON headers' do
assert_requested :get, 'api.example.org', headers: { 'Content-Type' => 'application/json', 'Accept' => 'application/json' }
assert_requested :get, 'api.example.org', headers: {
'Content-Type' => 'application/hal+json', 'Accept' => 'application/hal+json,application/json'
}
end

step 'I send some data to the API' do
Expand Down
6 changes: 3 additions & 3 deletions features/support/api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ module API
include Spinach::Fixtures

before do
stub_request(:any, %r{api.example.org*}).to_return(body: root_response, headers: { 'Content-Type' => 'application/json' })
stub_request(:get, 'api.example.org/posts').to_return(body: posts_response, headers: { 'Content-Type' => 'application/json' })
stub_request(:get, 'api.example.org/posts/1').to_return(body: post_response, headers: { 'Content-Type' => 'application/json' })
stub_request(:any, %r{api.example.org*}).to_return(body: root_response, headers: { 'Content-Type' => 'application/hal+json' })
stub_request(:get, 'api.example.org/posts').to_return(body: posts_response, headers: { 'Content-Type' => 'application/hal+json' })
stub_request(:get, 'api.example.org/posts/1').to_return(body: post_response, headers: { 'Content-Type' => 'application/hal+json' })
end

def api
Expand Down
1 change: 1 addition & 0 deletions hyperclient.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ Gem::Specification.new do |gem|
gem.add_dependency 'faraday', '~> 0.8'
gem.add_dependency 'futuroscope', '>= 0.0.10'
gem.add_dependency 'faraday_middleware', '~> 0.9'
gem.add_dependency 'faraday_hal_middleware', '~> 0.0.1'
gem.add_dependency 'uri_template', '~> 0.5'
gem.add_dependency 'net-http-digest_auth', '~> 1.2'
gem.add_dependency 'faraday-digestauth', '~> 0.2'
Expand Down
7 changes: 4 additions & 3 deletions lib/hyperclient/entry_point.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
require 'hyperclient/link'
require 'faraday_middleware'
require 'faraday_hal_middleware'
require_relative '../faraday/connection'

module Hyperclient
Expand Down Expand Up @@ -109,8 +110,8 @@ def default_faraday_block
lambda do |conn|
conn.use Faraday::Response::RaiseError
conn.use FaradayMiddleware::FollowRedirects
conn.request :json
conn.response :json, content_type: /\bjson$/
conn.request :hal_json
conn.response :hal_json, content_type: /\bjson$/
conn.adapter :net_http
end
end
Expand All @@ -120,7 +121,7 @@ def default_faraday_block
#
# Returns a Hash.
def default_headers
{ 'Content-Type' => 'application/json', 'Accept' => 'application/json' }
{ 'Content-Type' => 'application/hal+json', 'Accept' => 'application/hal+json,application/json' }
end
end
end
2 changes: 1 addition & 1 deletion lib/hyperclient/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module Hyperclient
VERSION = '0.6.2'
VERSION = '0.7.0-pre'
end
16 changes: 8 additions & 8 deletions test/hyperclient/entry_point_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ module Hyperclient
end

it 'creates a Faraday connection with the default headers' do
entry_point.headers['Content-Type'].must_equal 'application/json'
entry_point.headers['Accept'].must_equal 'application/json'
entry_point.headers['Content-Type'].must_equal 'application/hal+json'
entry_point.headers['Accept'].must_equal 'application/hal+json,application/json'
end

it 'can update headers after a connection has been constructed' do
Expand All @@ -35,8 +35,8 @@ module Hyperclient
handlers = entry_point.connection.builder.handlers
handlers.must_include Faraday::Response::RaiseError
handlers.must_include FaradayMiddleware::FollowRedirects
handlers.must_include FaradayMiddleware::EncodeJson
handlers.must_include FaradayMiddleware::ParseJson
handlers.must_include FaradayMiddleware::EncodeHalJson
handlers.must_include FaradayMiddleware::ParseHalJson
handlers.must_include Faraday::Adapter::NetHttp
end

Expand Down Expand Up @@ -108,8 +108,8 @@ module Hyperclient

describe 'connection' do
it 'creates a Faraday connection with the default and additional headers' do
entry_point.headers['Content-Type'].must_equal 'application/json'
entry_point.headers['Accept'].must_equal 'application/json'
entry_point.headers['Content-Type'].must_equal 'application/hal+json'
entry_point.headers['Accept'].must_equal 'application/hal+json,application/json'
entry_point.headers['Access-Token'].must_equal 'token'
end

Expand All @@ -122,8 +122,8 @@ module Hyperclient
handlers.must_include Faraday::Request::OAuth
handlers.must_include Faraday::Response::RaiseError
handlers.must_include FaradayMiddleware::FollowRedirects
handlers.must_include FaradayMiddleware::EncodeJson
handlers.must_include FaradayMiddleware::ParseJson
handlers.must_include FaradayMiddleware::EncodeHalJson
handlers.must_include FaradayMiddleware::ParseHalJson
handlers.must_include Faraday::Adapter::NetHttp
end
end
Expand Down
8 changes: 4 additions & 4 deletions test/hyperclient_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@
end

it 'creates a Faraday connection with the default and additional headers' do
client.headers['Content-Type'].must_equal 'application/json'
client.headers['Accept'].must_equal 'application/json'
client.headers['Content-Type'].must_equal 'application/hal+json'
client.headers['Accept'].must_equal 'application/hal+json,application/json'
client.headers['Access-Token'].must_equal 'token'
end

Expand All @@ -34,8 +34,8 @@
handlers.must_include Faraday::Request::OAuth
handlers.must_include Faraday::Response::RaiseError
handlers.must_include FaradayMiddleware::FollowRedirects
handlers.must_include FaradayMiddleware::EncodeJson
handlers.must_include FaradayMiddleware::ParseJson
handlers.must_include FaradayMiddleware::EncodeHalJson
handlers.must_include FaradayMiddleware::ParseHalJson
handlers.must_include Faraday::Adapter::NetHttp
end
end
Expand Down