baidu-sdk 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (63) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +18 -0
  3. data/.rspec +2 -0
  4. data/.travis.yml +15 -0
  5. data/.yardopts +1 -0
  6. data/Gemfile +17 -0
  7. data/Guardfile +6 -0
  8. data/HISTORY.md +2 -0
  9. data/LICENSE.txt +22 -0
  10. data/README.md +161 -0
  11. data/Rakefile +4 -0
  12. data/baidu-pcs.gemspec +24 -0
  13. data/lib/baidu/configure.rb +27 -0
  14. data/lib/baidu/core.rb +19 -0
  15. data/lib/baidu/errors/error.rb +22 -0
  16. data/lib/baidu/oauth.rb +11 -0
  17. data/lib/baidu/oauth/client.rb +66 -0
  18. data/lib/baidu/oauth/flow/base.rb +44 -0
  19. data/lib/baidu/oauth/flow/code.rb +69 -0
  20. data/lib/baidu/oauth/flow/device.rb +75 -0
  21. data/lib/baidu/pcs.rb +19 -0
  22. data/lib/baidu/pcs/client.rb +1090 -0
  23. data/lib/baidu/session.rb +36 -0
  24. data/lib/baidu/support/cacert.pem +37 -0
  25. data/lib/baidu/support/request.rb +127 -0
  26. data/lib/baidu/support/util.rb +67 -0
  27. data/lib/baidu/version.rb +5 -0
  28. data/spec/baidu/core_spec.rb +20 -0
  29. data/spec/baidu/oauth/client_spec.rb +199 -0
  30. data/spec/baidu/pcs/client_spec.rb +878 -0
  31. data/spec/baidu/session_spec.rb +27 -0
  32. data/spec/baidu/support/request_spec.rb +58 -0
  33. data/spec/baidu/support/util_spec.rb +48 -0
  34. data/spec/fixtures/add_task.json +1 -0
  35. data/spec/fixtures/cancel_task.json +3 -0
  36. data/spec/fixtures/copy.json +7 -0
  37. data/spec/fixtures/delete.json +3 -0
  38. data/spec/fixtures/diff.json +17 -0
  39. data/spec/fixtures/empty.json +1 -0
  40. data/spec/fixtures/get_token_code.json +8 -0
  41. data/spec/fixtures/get_token_device.json +8 -0
  42. data/spec/fixtures/list.json +11 -0
  43. data/spec/fixtures/list_task_0.json +1 -0
  44. data/spec/fixtures/list_task_1.json +1 -0
  45. data/spec/fixtures/listrecycle.json +23 -0
  46. data/spec/fixtures/logo.png +0 -0
  47. data/spec/fixtures/meta.json +11 -0
  48. data/spec/fixtures/mkdir.json +7 -0
  49. data/spec/fixtures/move.json +8 -0
  50. data/spec/fixtures/query_task_0.json +24 -0
  51. data/spec/fixtures/query_task_1.json +22 -0
  52. data/spec/fixtures/quota.json +5 -0
  53. data/spec/fixtures/rapidupload.json +10 -0
  54. data/spec/fixtures/refresh_token.json +8 -0
  55. data/spec/fixtures/restore.json +1 -0
  56. data/spec/fixtures/search.json +11 -0
  57. data/spec/fixtures/stream_list.json +16 -0
  58. data/spec/fixtures/streaming.m3u8 +5 -0
  59. data/spec/fixtures/upload.json +9 -0
  60. data/spec/fixtures/upload_block.json +4 -0
  61. data/spec/fixtures/user_and_device_code.json +8 -0
  62. data/spec/spec_helper.rb +66 -0
  63. 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,3 @@
1
+ {
2
+ "request_id":12394838223
3
+ }
@@ -0,0 +1,7 @@
1
+ {"extra":
2
+ {"list":[{
3
+ "to":"/apps/pcstest_oauth/test2/6.jpg",
4
+ "from":"/apps/pcstest_oauth/test1/6.jpg"}]
5
+ },
6
+ "request_id":2298812844
7
+ }
@@ -0,0 +1,3 @@
1
+ {
2
+ "request_id":4043312866
3
+ }
@@ -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,11 @@
1
+ {
2
+ "list":[{"fs_id":3528850315,
3
+ "path":"/apps/yunform/music/hello",
4
+ "ctime":1331184269,
5
+ "mtime":1331184269,
6
+ "block_list":["59ca0efa9f5633cb0371bbc0355478d8"],
7
+ "size":13,
8
+ "isdir":0
9
+ }],
10
+ "request_id":4043312670
11
+ }
@@ -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,11 @@
1
+ {
2
+ "list" : [ { "fs_id" : 3528850315,
3
+ "path" : "/apps/yunform/music/hello",
4
+ "ctime" : 1331184269,
5
+ "mtime" : 1331184269,
6
+ "block_list":["59ca0efa9f5633cb0371bbc0355478d8"],
7
+ "size" : 13,
8
+ "isdir" : 1
9
+ } ],
10
+ "request_id" : 4043312678
11
+ }
@@ -0,0 +1,7 @@
1
+ {
2
+ "fs_id":1636599174,
3
+ "path":"/apps/yunfom/music",
4
+ "ctime":1331183814,
5
+ "mtime":1331183814,
6
+ "request_id":4043312656
7
+ }
@@ -0,0 +1,8 @@
1
+ {
2
+ "extra":
3
+ {"list":[{
4
+ "to":"/apps/pcstest_oauth/test2/2.jpg",
5
+ "from":"/apps/pcstest_oauth/test1/yyyytestwer.jpg"}]
6
+ },
7
+ "request_id":2298812844
8
+ }
@@ -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,5 @@
1
+ {
2
+ "quota":15000000000,
3
+ "used":5221166,
4
+ "request_id":4043312634
5
+ }
@@ -0,0 +1,10 @@
1
+ {
2
+ "path" : "/apps/album/1.jpg",
3
+ "size" : 372121,
4
+ "ctime" : 1234567890,
5
+ "mtime" : 1234567890,
6
+ "md5" : "cb123afcc12453543ef",
7
+ "fs_id" : 12345,
8
+ "isdir" : 0,
9
+ "request_id" : 12314124
10
+ }
@@ -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,11 @@
1
+ {
2
+ "list":[{"fs_id":3528850315,
3
+ "path":"/apps/yunform/music/hello",
4
+ "ctime":1331184269,
5
+ "mtime":1331184269,
6
+ "block_list":["59ca0efa9f5633cb0371bbc0355478d8"],
7
+ "size":13,
8
+ "isdir":0
9
+ }],
10
+ "request_id":4043312670
11
+ }
@@ -0,0 +1,16 @@
1
+ {
2
+ "total" : 13,
3
+ "start" : 0,
4
+ "limit" : 1,
5
+ "list" : [
6
+ {
7
+ "path" : "/apps/album/1.jpg",
8
+ "size" : 372121,
9
+ "ctime" : 1234567890,
10
+ "mtime" : 1234567890,
11
+ "md5" : "cb123afcc12453543ef",
12
+ "fs_id" : 12345,
13
+ "isdir" : 0
14
+ }
15
+ ]
16
+ }
@@ -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,9 @@
1
+ {
2
+ "path": "/apps/Backups/api测试/图片/标识.png",
3
+ "size": 4914,
4
+ "ctime": 1234567890,
5
+ "mtime": 1234567890,
6
+ "md5": "6c37219ba0d3dfdfa95ff6912e2c42b9",
7
+ "fs_id": 12345,
8
+ "request_id": 4043312669
9
+ }
@@ -0,0 +1,4 @@
1
+ {
2
+ "md5":"a7619410bca74850f985e488c9a0d51e",
3
+ "request_id":3238563823
4
+ }
@@ -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
+ }
@@ -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