Skip to content
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

feat: use user_info API to check tokens DD-4674 #12

Merged
merged 2 commits into from
Jan 27, 2025
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: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,8 @@

## 0.5.1

* Use POST method for `token_info` request
* Use POST method for `token_info` request

## 0.5.2

* Use GET `user_info` to check tokens
8 changes: 7 additions & 1 deletion lib/panda.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,14 @@ def make_request(request)
conn.response :json
end

response = connection.run_request(request.method, request.url, request.params, request.headers)
dmitrytrager marked this conversation as resolved.
Show resolved Hide resolved
response = do_request(connection, request)
Panda::HTTPResponse.new(response.status, response.headers, response.body)
end

def do_request(connection, request)
return connection.post(request.url, request.params, request.headers) if request.method == :post

connection.get(request.url, request.params, request.headers)
end
end
end
14 changes: 12 additions & 2 deletions lib/panda/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
require 'panda/errors'
require 'panda/http_request'
require 'panda/http_response'
require 'panda/token_info'
require 'panda/item'

module Panda
class Client
Expand Down Expand Up @@ -53,6 +53,11 @@ def report(advertiser_id, report_type, dimensions, params = {})
)
end

def user_info
get_user('user/info/')
end

# requires token from Accounts API
def token_info
get_token(
'tt_user/token_info/get/',
Expand All @@ -63,9 +68,14 @@ def token_info

private

def get_user(path, params = {})
request = Panda::HTTPRequest.new('GET', path, params, 'Access-Token' => access_token)
Panda::Item.new(Panda.make_request(request))
end

def get_token(path, params = {})
request = Panda::HTTPRequest.new('POST', path, params)
Panda::TokenInfo.new(Panda.make_request(request))
Panda::Item.new(Panda.make_request(request))
end

def get_collection(path, params = {})
Expand Down
2 changes: 1 addition & 1 deletion lib/panda/token_info.rb → lib/panda/item.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# frozen_string_literal: true

module Panda
class TokenInfo
class Item
attr_reader :data

def initialize(response)
Expand Down
2 changes: 1 addition & 1 deletion lib/panda/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# frozen_string_literal: true

module Panda
VERSION = '0.5.1'
VERSION = '0.5.2'
end
8 changes: 8 additions & 0 deletions spec/cases/client_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,14 @@
end
end

describe '#user_info' do
it 'calls #get_user' do
expect(subject).to receive(:get_user).with('user/info/')

subject.user_info
end
end

describe '#token_info' do
it 'calls #get_token' do
expect(subject)
Expand Down
52 changes: 52 additions & 0 deletions spec/cases/item_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# frozen_string_literal: true

require 'spec_helper'

describe Panda::Item do
let(:response) { Panda::HTTPResponse.new(200, {}, response_body) }

context 'when item is token info' do
let(:response_body) do
{
'message' => 'OK',
'code' => 0,
'data' => {
'app_id' => Panda.config.app_id,
'creator_id' => 'test_creator_id',
'scope' => 'user.info.basic'
},
'request_id': '2020031009181201018904922342087A16'
}
end

it 'gets token info' do
token_info = described_class.new(response)

expect(token_info['creator_id']).to eq('test_creator_id')
expect(token_info['scope']).to eq('user.info.basic')
end
end

context 'item is user info' do
let(:response_body) do
{
'message' => 'OK',
'code' => 0,
'data' => {
'display_name' => 'user_name',
'email' => '[email protected]',
'core_user_id' => 'core_user_id',
'avatar_url' => 'https://example.com/avatar.jpg'
},
'request_id': '2020031009181201018904922342087A16'
}
end

it 'gets user info' do
user_info = described_class.new(response)

expect(user_info['display_name']).to eq('user_name')
expect(user_info['email']).to eq('[email protected]')
end
end
end
29 changes: 0 additions & 29 deletions spec/cases/token_info_spec.rb

This file was deleted.

Loading