diff --git a/README.md b/README.md index 1d24e4b..3ae55c4 100644 --- a/README.md +++ b/README.md @@ -176,13 +176,14 @@ taxonomy_name = term.taxonomy.name posts = term.posts ``` -#### OAuth +#### OAuth & OAuth2 Provide a symbol-keyed hash of `token`, `token_secret`, `consumer_key` and `consumer_secret` on configuration. ```ruby WpApiClient.configure do |api_client| - api_client.oauth_credentials = oauth_credentials_hash + api_client.oauth_credentials = oauth_credentials_hash #OAuth + api_client.oauth2_token = oauth_token #OAuth2 end client = WpApiClient.get_client diff --git a/lib/wp_api_client/client.rb b/lib/wp_api_client/client.rb index 2fd16d6..e71668c 100644 --- a/lib/wp_api_client/client.rb +++ b/lib/wp_api_client/client.rb @@ -15,6 +15,16 @@ def get(url, params = {}) end end + def post(url, params = {}) + if @concurrent_client + @concurrent_client.post(api_path_from(url), params) + else + response = @connection.post(api_path_from(url), params) + @headers = response.headers + native_representation_of response.body + end + end + def concurrently @concurrent_client ||= ConcurrentClient.new(@connection) yield @concurrent_client @@ -23,7 +33,7 @@ def concurrently result end - private + private def api_path_from(url) url.split('wp/v2/').last diff --git a/lib/wp_api_client/concurrent_client.rb b/lib/wp_api_client/concurrent_client.rb index d079a07..9b56b39 100644 --- a/lib/wp_api_client/concurrent_client.rb +++ b/lib/wp_api_client/concurrent_client.rb @@ -6,6 +6,11 @@ def get(url, params = {}) @queue << [api_path_from(url), params] end + def post(url, params = {}) + @queue ||= [] + @queue << [api_path_from(url), params] + end + def run responses = @connection.get_concurrently(@queue) responses.map { |r| native_representation_of(r.body) } diff --git a/lib/wp_api_client/configuration.rb b/lib/wp_api_client/configuration.rb index e1c650a..5f3bc6a 100644 --- a/lib/wp_api_client/configuration.rb +++ b/lib/wp_api_client/configuration.rb @@ -20,6 +20,7 @@ class Configuration attr_accessor :endpoint attr_accessor :embed attr_accessor :oauth_credentials + attr_accessor :oauth2_token attr_accessor :debug attr_accessor :cache attr_accessor :basic_auth diff --git a/lib/wp_api_client/connection.rb b/lib/wp_api_client/connection.rb index bdbb039..0976d83 100644 --- a/lib/wp_api_client/connection.rb +++ b/lib/wp_api_client/connection.rb @@ -15,7 +15,9 @@ def initialize(configuration) @queue = [] @conn = Faraday.new(url: configuration.endpoint) do |faraday| - if configuration.oauth_credentials + if configuration.oauth2_token + faraday.request :oauth2, configuration.oauth2_token + elsif configuration.oauth_credentials faraday.use FaradayMiddleware::OAuth, configuration.oauth_credentials end @@ -47,6 +49,10 @@ def get(url, params = {}) @conn.get url, parse_params(params) end + def post(url, params = {}) + @conn.post url, params + end + # requests come in as url/params pairs def get_concurrently(requests) responses = [] diff --git a/spec/post_spec.rb b/spec/post_spec.rb index 5569f99..d3d2582 100644 --- a/spec/post_spec.rb +++ b/spec/post_spec.rb @@ -37,45 +37,51 @@ describe "meta function" do - before :all do - WpApiClient.reset! - oauth_credentials = get_test_oauth_credentials - - WpApiClient.configure do |api_client| - api_client.oauth_credentials = oauth_credentials + [:oauth, :oauth2].each do |auth_type| + before :all do + WpApiClient.reset! + oauth_credentials = get_test_oauth_credentials + + WpApiClient.configure do |api_client| + if auth_type == :oauth + api_client.oauth_credentials = oauth_credentials + elsif auth_type == :oauth2 + api_client.oauth2_token = oauth_credentials + end + end + @api = WpApiClient.get_client end - @api = WpApiClient.get_client - end - - after :all do - WpApiClient.reset! - end - it "returns an individual meta value" do - VCR.use_cassette('single_post_auth', record: :new_episodes) do - @post = WpApiClient.get_client.get("posts/1") - expect(@post.meta(:example_metadata_field)).to eq "example_meta_value" + after :all do + WpApiClient.reset! end - end - it "caches" do - VCR.use_cassette('single_post_auth', record: :new_episodes) do - @post = WpApiClient.get_client.get("posts/1") - meta_value = @post.meta(:example_metadata_field) + it "returns an individual meta value" do + VCR.use_cassette('single_post_auth', record: :new_episodes) do + @post = WpApiClient.get_client.get("posts/1") + expect(@post.meta(:example_metadata_field)).to eq "example_meta_value" + end end - VCR.turned_off do - ::WebMock.disable_net_connect! - expect { - @post.meta(:example_associated_post_id) - }.to_not raise_error + + it "caches" do + VCR.use_cassette('single_post_auth', record: :new_episodes) do + @post = WpApiClient.get_client.get("posts/1") + meta_value = @post.meta(:example_metadata_field) + end + VCR.turned_off do + ::WebMock.disable_net_connect! + expect { + @post.meta(:example_associated_post_id) + }.to_not raise_error + end end - end - it "returns the right items from cache" do - VCR.use_cassette('single_post_auth', record: :new_episodes) do - @post = WpApiClient.get_client.get("posts/1") - expect(@post.meta(:example_metadata_field)).to eq "example_meta_value" - expect(@post.meta(:example_associated_post_id)).to eq "100" + it "returns the right items from cache" do + VCR.use_cassette('single_post_auth', record: :new_episodes) do + @post = WpApiClient.get_client.get("posts/1") + expect(@post.meta(:example_metadata_field)).to eq "example_meta_value" + expect(@post.meta(:example_associated_post_id)).to eq "100" + end end end end diff --git a/spec/relationships_spec.rb b/spec/relationships_spec.rb index 4be8454..e885fa9 100644 --- a/spec/relationships_spec.rb +++ b/spec/relationships_spec.rb @@ -38,24 +38,27 @@ end end - describe "relationships with metadata", vcr: {cassette_name: 'single_post_auth', record: :new_episodes} do - before :each do - # we need oAuth for this - WpApiClient.reset! - oauth_credentials = get_test_oauth_credentials - WpApiClient.configure do |api_client| - api_client.oauth_credentials = oauth_credentials - end - @api = WpApiClient.get_client - post = @api.get("posts/1") - @relationship = WpApiClient::Relationship.new(post.resource, "https://api.w.org/meta") - end + describe "relationships with metadata" do + [:oauth, :oauth2].each do |auth_type| + subject(:api) { + WpApiClient.reset! + oauth_credentials = get_test_oauth_credentials + WpApiClient.configure do |api_client| + if auth_type == :oauth + api_client.oauth_credentials = oauth_credentials + elsif auth_type == :oauth2 + api_client.oauth2_token = oauth_credentials + end + end + WpApiClient.get_client + } - it "returns an hash of meta" do - relations = @relationship.get_relations - expect(relations).to be_a Hash - expect(relations["example_metadata_field"]).to be_a String + it "returns an hash of meta", vcr: {cassette_name: 'single_post_auth', record: :new_episodes} do + post = api.get("posts/1") + relations = WpApiClient::Relationship.new(post.resource, "https://api.w.org/meta").get_relations + expect(relations).to be_a Hash + expect(relations["example_metadata_field"]).to be_a String + end end end - end diff --git a/spec/wp_api_configuration_spec.rb b/spec/wp_api_configuration_spec.rb index cfe127f..7f71384 100644 --- a/spec/wp_api_configuration_spec.rb +++ b/spec/wp_api_configuration_spec.rb @@ -58,6 +58,16 @@ WpApiClient.get_client.get('posts/1') end + it "can set up OAuth2 token", vcr: {cassette_name: :oauth_test} do + oauth_credentials = get_test_oauth_credentials + + WpApiClient.configure do |api_client| + api_client.oauth2_token = oauth_credentials + end + + WpApiClient.get_client.get('posts/1') + end + it "can set up link relationships", vcr: {cassette_name: :single_post} do WpApiClient.configure do |api_client| api_client.define_mapping("http://my.own/mapping", :post)