From 519f819cd5dd5e17312f69c1bfe157aa160cb46f Mon Sep 17 00:00:00 2001 From: koyanagi Date: Mon, 6 May 2019 21:10:19 +0900 Subject: [PATCH 1/6] =?UTF-8?q?post=20=E8=BF=BD=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/wp_api_client/client.rb | 12 +++++++++++- lib/wp_api_client/concurrent_client.rb | 5 +++++ lib/wp_api_client/connection.rb | 4 ++++ 3 files changed, 20 insertions(+), 1 deletion(-) 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/connection.rb b/lib/wp_api_client/connection.rb index bdbb039..244fcb4 100644 --- a/lib/wp_api_client/connection.rb +++ b/lib/wp_api_client/connection.rb @@ -47,6 +47,10 @@ def get(url, params = {}) @conn.get url, parse_params(params) end + def post(url, params = {}) + @conn.get url, parse_params(params) + end + # requests come in as url/params pairs def get_concurrently(requests) responses = [] From 9c01c774aea4b20c0665416ac4649fa8e571105e Mon Sep 17 00:00:00 2001 From: koyanagi Date: Mon, 6 May 2019 21:47:21 +0900 Subject: [PATCH 2/6] post body --- lib/wp_api_client/connection.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/wp_api_client/connection.rb b/lib/wp_api_client/connection.rb index 244fcb4..d22e9c7 100644 --- a/lib/wp_api_client/connection.rb +++ b/lib/wp_api_client/connection.rb @@ -48,7 +48,7 @@ def get(url, params = {}) end def post(url, params = {}) - @conn.get url, parse_params(params) + @conn.post url, params end # requests come in as url/params pairs From 7d875de88e6f1e4c8bc45ec9a4bab2c3e87fbf21 Mon Sep 17 00:00:00 2001 From: koyanagi Date: Tue, 7 May 2019 10:23:05 +0900 Subject: [PATCH 3/6] add oauth2 token --- lib/wp_api_client/configuration.rb | 1 + lib/wp_api_client/connection.rb | 4 ++++ 2 files changed, 5 insertions(+) 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 d22e9c7..ba0514f 100644 --- a/lib/wp_api_client/connection.rb +++ b/lib/wp_api_client/connection.rb @@ -15,6 +15,10 @@ def initialize(configuration) @queue = [] @conn = Faraday.new(url: configuration.endpoint) do |faraday| + if configuration.oauth2_token + faraday.use FaradayMiddleware::OAuth2, configuration.oauth2_token + end + if configuration.oauth_credentials faraday.use FaradayMiddleware::OAuth, configuration.oauth_credentials end From fe794d83ae3c0fdf49dda2681c2e492b707b2d15 Mon Sep 17 00:00:00 2001 From: koyanagi Date: Tue, 7 May 2019 10:36:39 +0900 Subject: [PATCH 4/6] add bearer token with oauth2 --- lib/wp_api_client/connection.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/wp_api_client/connection.rb b/lib/wp_api_client/connection.rb index ba0514f..075fc3f 100644 --- a/lib/wp_api_client/connection.rb +++ b/lib/wp_api_client/connection.rb @@ -16,7 +16,7 @@ def initialize(configuration) @conn = Faraday.new(url: configuration.endpoint) do |faraday| if configuration.oauth2_token - faraday.use FaradayMiddleware::OAuth2, configuration.oauth2_token + faraday.use FaradayMiddleware::OAuth2, configuration.oauth2_token, token_type: :bearer end if configuration.oauth_credentials From d711716906bc6d64b298ef6c5717e7ae5741fe4c Mon Sep 17 00:00:00 2001 From: koyanagi Date: Sat, 2 Nov 2019 15:17:16 +0900 Subject: [PATCH 5/6] choice oauth or oauth2 add specs for oauth2 --- lib/wp_api_client/connection.rb | 6 +-- spec/post_spec.rb | 70 +++++++++++++++++-------------- spec/relationships_spec.rb | 37 ++++++++-------- spec/wp_api_configuration_spec.rb | 10 +++++ 4 files changed, 70 insertions(+), 53 deletions(-) diff --git a/lib/wp_api_client/connection.rb b/lib/wp_api_client/connection.rb index 075fc3f..0976d83 100644 --- a/lib/wp_api_client/connection.rb +++ b/lib/wp_api_client/connection.rb @@ -16,10 +16,8 @@ def initialize(configuration) @conn = Faraday.new(url: configuration.endpoint) do |faraday| if configuration.oauth2_token - faraday.use FaradayMiddleware::OAuth2, configuration.oauth2_token, token_type: :bearer - end - - if configuration.oauth_credentials + faraday.request :oauth2, configuration.oauth2_token + elsif configuration.oauth_credentials faraday.use FaradayMiddleware::OAuth, configuration.oauth_credentials end 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) From 2e278394689dbf7303cceb20ef1a2c9d0d7eed04 Mon Sep 17 00:00:00 2001 From: koyanagi Date: Sat, 2 Nov 2019 21:14:22 +0900 Subject: [PATCH 6/6] add oauth2 doc --- README.md | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) 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