baidu-sdk 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +18 -0
- data/.rspec +2 -0
- data/.travis.yml +15 -0
- data/.yardopts +1 -0
- data/Gemfile +17 -0
- data/Guardfile +6 -0
- data/HISTORY.md +2 -0
- data/LICENSE.txt +22 -0
- data/README.md +161 -0
- data/Rakefile +4 -0
- data/baidu-pcs.gemspec +24 -0
- data/lib/baidu/configure.rb +27 -0
- data/lib/baidu/core.rb +19 -0
- data/lib/baidu/errors/error.rb +22 -0
- data/lib/baidu/oauth.rb +11 -0
- data/lib/baidu/oauth/client.rb +66 -0
- data/lib/baidu/oauth/flow/base.rb +44 -0
- data/lib/baidu/oauth/flow/code.rb +69 -0
- data/lib/baidu/oauth/flow/device.rb +75 -0
- data/lib/baidu/pcs.rb +19 -0
- data/lib/baidu/pcs/client.rb +1090 -0
- data/lib/baidu/session.rb +36 -0
- data/lib/baidu/support/cacert.pem +37 -0
- data/lib/baidu/support/request.rb +127 -0
- data/lib/baidu/support/util.rb +67 -0
- data/lib/baidu/version.rb +5 -0
- data/spec/baidu/core_spec.rb +20 -0
- data/spec/baidu/oauth/client_spec.rb +199 -0
- data/spec/baidu/pcs/client_spec.rb +878 -0
- data/spec/baidu/session_spec.rb +27 -0
- data/spec/baidu/support/request_spec.rb +58 -0
- data/spec/baidu/support/util_spec.rb +48 -0
- data/spec/fixtures/add_task.json +1 -0
- data/spec/fixtures/cancel_task.json +3 -0
- data/spec/fixtures/copy.json +7 -0
- data/spec/fixtures/delete.json +3 -0
- data/spec/fixtures/diff.json +17 -0
- data/spec/fixtures/empty.json +1 -0
- data/spec/fixtures/get_token_code.json +8 -0
- data/spec/fixtures/get_token_device.json +8 -0
- data/spec/fixtures/list.json +11 -0
- data/spec/fixtures/list_task_0.json +1 -0
- data/spec/fixtures/list_task_1.json +1 -0
- data/spec/fixtures/listrecycle.json +23 -0
- data/spec/fixtures/logo.png +0 -0
- data/spec/fixtures/meta.json +11 -0
- data/spec/fixtures/mkdir.json +7 -0
- data/spec/fixtures/move.json +8 -0
- data/spec/fixtures/query_task_0.json +24 -0
- data/spec/fixtures/query_task_1.json +22 -0
- data/spec/fixtures/quota.json +5 -0
- data/spec/fixtures/rapidupload.json +10 -0
- data/spec/fixtures/refresh_token.json +8 -0
- data/spec/fixtures/restore.json +1 -0
- data/spec/fixtures/search.json +11 -0
- data/spec/fixtures/stream_list.json +16 -0
- data/spec/fixtures/streaming.m3u8 +5 -0
- data/spec/fixtures/upload.json +9 -0
- data/spec/fixtures/upload_block.json +4 -0
- data/spec/fixtures/user_and_device_code.json +8 -0
- data/spec/spec_helper.rb +66 -0
- metadata +169 -0
@@ -0,0 +1,27 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
|
3
|
+
require 'spec_helper'
|
4
|
+
|
5
|
+
describe Baidu do
|
6
|
+
describe '.from' do
|
7
|
+
let(:hash) {
|
8
|
+
{
|
9
|
+
access_token: "1.a6b7dbd428f731035f771b8d15063f61.86400",
|
10
|
+
expires_in: 86400,
|
11
|
+
refresh_token: "2.385d55f8615fdfd9edb7c4b5ebdc3e39.604800",
|
12
|
+
scope: "basic",
|
13
|
+
session_key: "ANXxSNjwQDugf8615OnqeikRMu2bKaXCdlLxn",
|
14
|
+
session_secret: "248APxvxjCZ0VEC43EYrvxqaK4oZExMB"
|
15
|
+
}
|
16
|
+
}
|
17
|
+
|
18
|
+
it 'creates new session from hash' do
|
19
|
+
session = Baidu::Session.from hash
|
20
|
+
expect(session.access_token).to eq('1.a6b7dbd428f731035f771b8d15063f61.86400')
|
21
|
+
expect(session.refresh_token).to eq('2.385d55f8615fdfd9edb7c4b5ebdc3e39.604800')
|
22
|
+
expect(session.scope).to eq('basic')
|
23
|
+
expect(session.session_key).to eq('ANXxSNjwQDugf8615OnqeikRMu2bKaXCdlLxn')
|
24
|
+
expect(session.session_secret).to eq('248APxvxjCZ0VEC43EYrvxqaK4oZExMB')
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
@@ -0,0 +1,58 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
|
3
|
+
require 'spec_helper'
|
4
|
+
require 'baidu/pcs'
|
5
|
+
|
6
|
+
describe Baidu::Support::Request do
|
7
|
+
before :all do
|
8
|
+
Baidu.config do |c|
|
9
|
+
c.pcs_dir_name = 'Backups'
|
10
|
+
end
|
11
|
+
@client = Baidu::PCS::Client.new('ATOKEN')
|
12
|
+
end
|
13
|
+
|
14
|
+
describe '#request' do
|
15
|
+
it 'needs rewrite with no pcs be required'
|
16
|
+
|
17
|
+
describe 'got redirection status code' do
|
18
|
+
it 'redirects 302 response' do
|
19
|
+
stub_request(:get, 'baidu.com/notfound').to_return(status: 302, headers: { Location: 'http://baidu.com' })
|
20
|
+
stub_request(:get, 'baidu.com/').to_return(status: 200, body: 'stop redirect')
|
21
|
+
|
22
|
+
@client.get('/notfound', nil, site: 'http://baidu.com', raw: true)
|
23
|
+
a_request(:get, 'baidu.com/notfound').should have_been_made
|
24
|
+
a_request(:get, 'baidu.com').should have_been_made
|
25
|
+
end
|
26
|
+
|
27
|
+
it 'throw exception with infinite redirect loop' do
|
28
|
+
stub_request(:get, 'baidu.com/loop').to_return(status: 302, headers: { Location: 'http://baidu.com/loop' })
|
29
|
+
expect {
|
30
|
+
@client.get('/loop', nil, site: 'http://baidu.com')
|
31
|
+
}.to raise_error(Baidu::Errors::Error)
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
describe 'got other status code' do
|
36
|
+
it 'requests with invalid access token' do
|
37
|
+
stub_request(:get, 'baidu.com/noaccess').to_return(status: 401)
|
38
|
+
expect {
|
39
|
+
@client.get('/noaccess', nil, site: 'http://baidu.com')
|
40
|
+
}.to raise_error(Baidu::Errors::AuthError)
|
41
|
+
end
|
42
|
+
|
43
|
+
it 'requests with invalid api' do
|
44
|
+
stub_request(:get, 'baidu.com/invalid').to_return(status: 400)
|
45
|
+
expect {
|
46
|
+
@client.get('/invalid', nil, site: 'http://baidu.com')
|
47
|
+
}.to raise_error(Baidu::Errors::ClientError)
|
48
|
+
end
|
49
|
+
|
50
|
+
it 'requests with server error' do
|
51
|
+
stub_request(:get, 'baidu.com/serverfault').to_return(status: 500)
|
52
|
+
expect {
|
53
|
+
@client.get('/serverfault', nil, site: 'http://baidu.com')
|
54
|
+
}.to raise_error(Baidu::Errors::ServerError)
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
@@ -0,0 +1,48 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
|
3
|
+
require 'spec_helper'
|
4
|
+
|
5
|
+
describe Baidu::Support::Util do
|
6
|
+
describe '.blank?' do
|
7
|
+
it 'checks non blank value' do
|
8
|
+
expect(Baidu::Support::Util.blank? 'abc').to be false
|
9
|
+
expect(Baidu::Support::Util.blank? ' abc ').to be false
|
10
|
+
expect(Baidu::Support::Util.blank? ' 你好 ').to be false
|
11
|
+
expect(Baidu::Support::Util.blank? ' 你好 ').to be false
|
12
|
+
expect(Baidu::Support::Util.blank? %w[abc]).to be false
|
13
|
+
expect(Baidu::Support::Util.blank?({abc: 'def'})).to be false
|
14
|
+
expect(Baidu::Support::Util.blank? true).to be false
|
15
|
+
end
|
16
|
+
|
17
|
+
it 'checks blank value' do
|
18
|
+
expect(Baidu::Support::Util.blank? '').to be true
|
19
|
+
expect(Baidu::Support::Util.blank?(' ')).to be true
|
20
|
+
expect(Baidu::Support::Util.blank? "\n").to be true
|
21
|
+
expect(Baidu::Support::Util.blank? "\t").to be true
|
22
|
+
expect(Baidu::Support::Util.blank? nil).to be true
|
23
|
+
expect(Baidu::Support::Util.blank? []).to be true
|
24
|
+
expect(Baidu::Support::Util.blank?({})).to be true
|
25
|
+
expect(Baidu::Support::Util.blank? false).to be true
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
describe '.edit_path' do
|
30
|
+
it 'removes redundant slashes' do
|
31
|
+
expect(Baidu::Support::Util.edit_path('/2013//hel///lo.png')).to eq('/2013/hel/lo.png')
|
32
|
+
end
|
33
|
+
|
34
|
+
it 'replace \\\\ ? | " > < : * with _' do
|
35
|
+
expect(Baidu::Support::Util.edit_path('2013\\hello?.png')).to eq('2013_hello_.png')
|
36
|
+
expect(Baidu::Support::Util.edit_path('2013|hello*.png')).to eq('2013_hello_.png')
|
37
|
+
expect(Baidu::Support::Util.edit_path('2013"hello:.png')).to eq('2013_hello_.png')
|
38
|
+
expect(Baidu::Support::Util.edit_path('2013>hello<.png')).to eq('2013_hello_.png')
|
39
|
+
expect(Baidu::Support::Util.edit_path('20\\\\\\\\13?|"><:*hello.png')).to eq('20____13_______hello.png')
|
40
|
+
end
|
41
|
+
|
42
|
+
it 'removes dot and whitespaces' do
|
43
|
+
expect(Baidu::Support::Util.edit_path(' 2013|hello.png..')).to eq('2013_hello.png')
|
44
|
+
expect(Baidu::Support::Util.edit_path('.2013|hello.png ')).to eq('2013_hello.png')
|
45
|
+
expect(Baidu::Support::Util.edit_path("\n .2013|hello.png . \t")).to eq('2013_hello.png')
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
@@ -0,0 +1 @@
|
|
1
|
+
{"task_id":432432432432432,"request_id":3372220525}
|
@@ -0,0 +1,17 @@
|
|
1
|
+
{
|
2
|
+
"entries":{
|
3
|
+
"\/baiduapp\/browser":{
|
4
|
+
"fs_id":2427025269,
|
5
|
+
"path":"\/baiduapp\/browser",
|
6
|
+
"size":0,
|
7
|
+
"isdir":1,
|
8
|
+
"md5":"",
|
9
|
+
"mtime":1336631762,
|
10
|
+
"ctime":1336631762
|
11
|
+
}
|
12
|
+
},
|
13
|
+
"has_more":true,
|
14
|
+
"reset":true,
|
15
|
+
"cursor":"MxKx6UPie%2F9WzBkwALPrVWQlyxlmK0LgHG8zutwXp8oyC%2FngIdGgS3w2Jt%2B%2BktMKKQpBbnC%2B11aH7Ec9pt%2BfteS%2F%2BknWrp3JIz%2F6fXHccEkZo2kkkSH748hScdRgcA4VCZJuCMQMvNkXAlSmzT5TwqBVc3xwhSxaFkClqbcogAOc8I0k7xtTb9nG6rBJsxNgRFgBV4F695TkrLDHYHRy%2BQ%3D%3D",
|
16
|
+
"request_id":3355443548
|
17
|
+
}
|
@@ -0,0 +1 @@
|
|
1
|
+
{"request_id":2307473052}
|
@@ -0,0 +1,8 @@
|
|
1
|
+
{
|
2
|
+
"access_token": "1.a6b7dbd428f731035f771b8d15063f61.86400.1292922000-2346678-124328",
|
3
|
+
"expires_in": 86400,
|
4
|
+
"refresh_token": "2.385d55f8615fdfd9edb7c4b5ebdc3e39.604800.1293440400-2346678-124328",
|
5
|
+
"scope": "basic email",
|
6
|
+
"session_key": "ANXxSNjwQDugf8615OnqeikRMu2bKaXCdlLxn",
|
7
|
+
"session_secret": "248APxvxjCZ0VEC43EYrvxqaK4oZExMB"
|
8
|
+
}
|
@@ -0,0 +1,8 @@
|
|
1
|
+
{
|
2
|
+
"access_token": "1.a6b7dbd428f731035f771b8d15063f61.86400.1292922000-2346678-124328",
|
3
|
+
"expires_in": 86400,
|
4
|
+
"refresh_token": "2.385d55f8615fdfd9edb7c4b5ebdc3e39.604800.1293440400-2346678-124328",
|
5
|
+
"scope": "basic email",
|
6
|
+
"session_key": "ANXxSNjwQDugf8615OnqeikRMu2bKaXCdlLxn",
|
7
|
+
"session_secret": "248APxvxjCZ0VEC43EYrvxqaK4oZExMB"
|
8
|
+
}
|
@@ -0,0 +1 @@
|
|
1
|
+
{"task_info":[{"task_id":"26"}],"total":"1","request_id":1283164486}
|
@@ -0,0 +1 @@
|
|
1
|
+
{"task_info":[{"task_id":"26","source_url":"http:\/\/dl_dir.qq.com:80\/qqfile\/qq\/QQ2012\/QQ2012.exe","save_path":"\/apps\/Slideshow\/wu_jing_test0","rate_limit":"100","timeout":"10000","callback":"http:\/\/www.baidu.com","status":"1","create_time":"1347449048"}],"total":"1","request_id":1285732167}
|
@@ -0,0 +1,23 @@
|
|
1
|
+
{
|
2
|
+
"list":[
|
3
|
+
{
|
4
|
+
"fs_id":1579174,
|
5
|
+
"path":"\/apps\/CloudDriveDemo\/testfile-10.rar",
|
6
|
+
"ctime":1361934614,
|
7
|
+
"mtime":1361934625,
|
8
|
+
"md5":"1131170ac11cfbec411a5e8d4e111769",
|
9
|
+
"size":10730431,
|
10
|
+
"isdir":0
|
11
|
+
},
|
12
|
+
{
|
13
|
+
"fs_id":304521061,
|
14
|
+
"path":"\/apps\/CloudDriveDemo\/testfile-4.rar",
|
15
|
+
"ctime":1361934605,
|
16
|
+
"mtime":1361934625,
|
17
|
+
"md5":"9552bf5e5abdf962e2de94be243bec7c",
|
18
|
+
"size":4287611,
|
19
|
+
"isdir":0
|
20
|
+
}
|
21
|
+
],
|
22
|
+
"request_id":3779302504
|
23
|
+
}
|
Binary file
|
@@ -0,0 +1,24 @@
|
|
1
|
+
{
|
2
|
+
"task_info": {
|
3
|
+
"22223333": {
|
4
|
+
"source_url": "http://test.com/1.dmg",
|
5
|
+
"finished_size": "15321458",
|
6
|
+
"save_path": "/apps/Backups/1.dmg",
|
7
|
+
"rate_limit": "0",
|
8
|
+
"timeout": "3600",
|
9
|
+
"callback": "",
|
10
|
+
"status": "0",
|
11
|
+
"create_time": "1384272276",
|
12
|
+
"task_name": "1.dmg",
|
13
|
+
"od_type": "0",
|
14
|
+
"file_list": [
|
15
|
+
{
|
16
|
+
"file_name": "1.dmg",
|
17
|
+
"file_size": "15321458"
|
18
|
+
}
|
19
|
+
],
|
20
|
+
"result": 0
|
21
|
+
}
|
22
|
+
},
|
23
|
+
"request_id": 541910000
|
24
|
+
}
|
@@ -0,0 +1,22 @@
|
|
1
|
+
{
|
2
|
+
"task_info": {
|
3
|
+
"22223333": {
|
4
|
+
"status": "0",
|
5
|
+
"file_size": "15321458",
|
6
|
+
"finished_size": "15321458",
|
7
|
+
"create_time": "1384272276",
|
8
|
+
"start_time": "1384272276",
|
9
|
+
"finish_time": "1384272276",
|
10
|
+
"save_path": "/apps/Backups/1.dmg",
|
11
|
+
"source_url": "http://test.com/1.dmg",
|
12
|
+
"task_name": "1.dmg",
|
13
|
+
"od_type": "0",
|
14
|
+
"file_list": [{
|
15
|
+
"file_name": "1.dmg",
|
16
|
+
"file_size": "15321458"
|
17
|
+
}],
|
18
|
+
"result": 0
|
19
|
+
}
|
20
|
+
},
|
21
|
+
"request_id": 5120983337
|
22
|
+
}
|
@@ -0,0 +1,8 @@
|
|
1
|
+
{
|
2
|
+
"access_token": "1.a6b7dbd428f731035f771b8d15063f61.86400.1292922000-2346678-124328",
|
3
|
+
"expires_in": 86400,
|
4
|
+
"refresh_token": "2.af3d55f8615fdfd9edb7c4b5ebdc3e32.604800.1293440400-2346678-124328",
|
5
|
+
"scope": "basic email",
|
6
|
+
"session_key": "ANXxSNjwQDugf8615OnqeikRMu2bKaXCdlLxn",
|
7
|
+
"session_secret": "248APxvxjCZ0VEC43EYrvxqaK4oZExMB"
|
8
|
+
}
|
@@ -0,0 +1 @@
|
|
1
|
+
{"extra":{"list":[{"fs_id":"1356099017"}]},"request_id":3775323016}
|
@@ -0,0 +1,5 @@
|
|
1
|
+
#EXTM3U
|
2
|
+
#EXT-X-TARGETDURATION:15
|
3
|
+
#EXT-X-CYBERTRANDURATION:1446
|
4
|
+
#EXTINF:3,
|
5
|
+
http://nj.cybertran.baidu.com/video/videotran.ts?src_user_id=00000&src_fs_id=1717&msgtype=nanjing&bucket=videotran&output_format=2048&local=1&addr=00000&start=0&len=132916&src=1025.m3u8-0.ts
|
@@ -0,0 +1,8 @@
|
|
1
|
+
{
|
2
|
+
"device_code":"a82hjs723h72h3a82hjs723h72h3vb",
|
3
|
+
"user_code":"8sjiae3p",
|
4
|
+
"verification_url":"https:\/\/openapi.baidu.com\/oauth\/2\.0\/device",
|
5
|
+
"qrcode_url":"http:\/\/openapi.baidu.com\/device\/qrcode\/6c6a8afee394f99e55eb25858\/2c885vjk",
|
6
|
+
"expires_in":1800,
|
7
|
+
"interval":5
|
8
|
+
}
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,66 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
|
3
|
+
require 'baidu/core'
|
4
|
+
require 'webmock/rspec'
|
5
|
+
|
6
|
+
# This file was generated by the `rspec --init` command. Conventionally, all
|
7
|
+
# specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
|
8
|
+
# Require this file using `require "spec_helper"` to ensure that it is only
|
9
|
+
# loaded once.
|
10
|
+
#
|
11
|
+
# See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
|
12
|
+
RSpec.configure do |config|
|
13
|
+
config.treat_symbols_as_metadata_keys_with_true_values = true
|
14
|
+
config.run_all_when_everything_filtered = true
|
15
|
+
config.filter_run focus: true
|
16
|
+
|
17
|
+
# Run specs in random order to surface order dependencies. If you find an
|
18
|
+
# order dependency and want to debug it, you can fix the order by providing
|
19
|
+
# the seed, which is printed after each run.
|
20
|
+
# --seed 1234
|
21
|
+
config.order = 'random'
|
22
|
+
|
23
|
+
# disable real requests to network
|
24
|
+
WebMock.disable_net_connect!
|
25
|
+
end
|
26
|
+
|
27
|
+
def fixtures_path
|
28
|
+
File.expand_path('../fixtures', __FILE__)
|
29
|
+
end
|
30
|
+
|
31
|
+
def ft(path)
|
32
|
+
File.new "#{fixtures_path}/#{path}"
|
33
|
+
end
|
34
|
+
|
35
|
+
def webmock_url(mod, path)
|
36
|
+
case mod
|
37
|
+
when :oauth then "https://openapi.baidu.com#{path}"
|
38
|
+
when :pcs then "https://pcs.baidu.com/rest/2.0/pcs#{path}"
|
39
|
+
when :pcs_upload then "https://c.pcs.baidu.com/rest/2.0/pcs#{path}"
|
40
|
+
when :pcs_download then "https://d.pcs.baidu.com/rest/2.0/pcs#{path}"
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
def stub_get(mod, path, params={})
|
45
|
+
req = stub_request :get, webmock_url(mod, path)
|
46
|
+
req.with(query: params) unless params.empty?
|
47
|
+
req
|
48
|
+
end
|
49
|
+
|
50
|
+
def stub_post(mod, path, params={})
|
51
|
+
req = stub_request :post, webmock_url(mod, path)
|
52
|
+
req.with(body: params) unless params.empty?
|
53
|
+
req
|
54
|
+
end
|
55
|
+
|
56
|
+
def a_get(mod, path, params={})
|
57
|
+
req = a_request :get, webmock_url(mod, path)
|
58
|
+
req.with(query: params) unless params.empty?
|
59
|
+
req
|
60
|
+
end
|
61
|
+
|
62
|
+
def a_post(mod, path, params={})
|
63
|
+
req = a_request :post, webmock_url(mod, path)
|
64
|
+
req.with(body: params) unless params.empty?
|
65
|
+
req
|
66
|
+
end
|