alipan-sdk 0.1.0 → 0.1.2

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 391e43dcbc839df82035ba6a730fe7d274054d5fa8147fc8742847668cca0b46
4
- data.tar.gz: 96afad1647af9260c3340f6bd9ecde58c593db444ad0cce301e0e1285dc0124f
3
+ metadata.gz: e4628bf443c49974992f06af3a266a6554f631efe8affb8e96fe4a0f1211c91e
4
+ data.tar.gz: d3dc3210dc35128e261c25454dfa36f5fd16384ad2d280e226520b9e03f81ac7
5
5
  SHA512:
6
- metadata.gz: 95e7da78fb03144dc9c1b26843b0e0d8733391b1ee28a6dbb363964cc39257d1d07ffc5ecbe575cedc4eb46e695ea088791d5275f1d4be0c9fa7e00e0ebbbcb2
7
- data.tar.gz: '08f4d6470a21d3f7ec5f5e8e37ea45c52834a3baf8c7953715ee8d190114a5fb6d7244737c7f3be9d640f110c76795b78812e35adf6740a56a764234fb08f18f'
6
+ metadata.gz: 9356ddf651b4f762cbcfe0c20c70dd3050f50307975647c0d21054a1b1b101047ebbc7d27f15c03c53abaa885bb251c9ea0a204dca95ca0e00da0149fa78189c
7
+ data.tar.gz: 1ee6392d663d57d12490c4ae12f007ad2343cee00bf2bb34dd729d6131ea6468b369b26c758b69a74cc060a1bde8b4d91d6fdd1fafa0480686a570d61a8f9549
data/README.md CHANGED
@@ -1,39 +1,45 @@
1
1
  # Alipan SDK for Ruby
2
2
 
3
- TODO: Delete this and the text below, and describe your gem
3
+ [![Gem Version](https://badge.fury.io/rb/alipan-sdk.svg)](https://badge.fury.io/rb/alipan-sdk)
4
4
 
5
- Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/alipan`. To experiment with that code, run `bin/console` for an interactive prompt.
5
+ ## 关于
6
6
 
7
- ## Installation
7
+ 阿里云盘SDK 方便Ruby客户端程序调用
8
8
 
9
- TODO: Replace `UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG` with your gem name right after releasing it to RubyGems.org. Please do not do it earlier due to security reasons. Alternatively, replace this section with instructions to install your gem from git if you don't plan to release to RubyGems.org.
9
+ ## 快速开始
10
10
 
11
- Install the gem and add to the application's Gemfile by executing:
11
+ ### 鉴权配置
12
+
13
+ 登录官网[阿里云盘开发者门户](https://www.alipan.com/developer/f) 按照开发文档获取access_token
14
+
15
+ ### 安装Alipan SDK for Ruby
12
16
 
13
17
  ```bash
14
- bundle add UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG
18
+ gem install alipan-sdk
15
19
  ```
16
20
 
17
- If bundler is not being used to manage dependencies, install the gem by executing:
21
+ 并在你的程序中或者`irb`命令下包含:
18
22
 
19
23
  ```bash
20
- gem install UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG
24
+ require 'alipan'
21
25
  ```
22
26
 
23
- ## Usage
27
+ ### 创建Client
28
+ client = Alipan::Client.new({:access_token=>"xxx"})
24
29
 
25
- TODO: Write usage instructions here
30
+ `access_token`是您的鉴权信息
26
31
 
27
- ## Development
28
32
 
29
- After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
33
+ **请妥善保管您的access_token 泄露之后可能影响您的数据安全**
30
34
 
31
- To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
35
+ ### 获取drive
36
+ drive = client.get_drive
32
37
 
33
- ## Contributing
38
+ ### 获取object
39
+ objects = drive.list_objects
34
40
 
35
- Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/alipan.
41
+ ## 更多
36
42
 
37
- ## License
43
+ 更多文档请查看:
38
44
 
39
- The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
45
+ [阿里云盘开发者门户](https://www.alipan.com/developer/f)
data/lib/alipan/drive.rb CHANGED
@@ -13,5 +13,23 @@ module Alipan
13
13
  def list_objects(opts = {})
14
14
  Iterator::Objects.new(@protocol, resource_drive_id, 'root', opts).to_enum
15
15
  end
16
+
17
+ def get_object(key, opts = {}, &block)
18
+ obj = nil
19
+ file = opts[:file]
20
+ if file
21
+ File.open(File.expand_path(file), 'wb') do |f|
22
+ obj = @protocol.get_object(resource_drive_id, key, opts) do |chunk|
23
+ f.write(chunk)
24
+ end
25
+ end
26
+ elsif block
27
+ obj = @protocol.get_object(resource_drive_id, key, opts, &block)
28
+ else
29
+ obj = @protocol.get_object(resource_drive_id, key, opts)
30
+ end
31
+
32
+ obj
33
+ end
16
34
  end
17
35
  end
data/lib/alipan/http.rb CHANGED
@@ -16,6 +16,10 @@ module Alipan
16
16
  @config = config
17
17
  end
18
18
 
19
+ def handle_response(r, &block)
20
+ r.read_body { |chunk| yield chunk }
21
+ end
22
+
19
23
  def get(resources = {}, http_options = {}, &block)
20
24
  do_request('GET', resources, http_options, &block)
21
25
  end
@@ -45,7 +49,7 @@ module Alipan
45
49
  def do_request(verb, resources = {}, http_options = {}, &block)
46
50
  sub_res = resources[:sub_res]
47
51
 
48
- headers = http_options[:headers] || {}
52
+ headers = {}
49
53
  headers['Content-Type'] ||= DEFAULT_CONTENT_TYPE
50
54
  headers[AUTH_HEADER] = @config.access_token if @config.access_token
51
55
 
@@ -55,8 +59,8 @@ module Alipan
55
59
  block_response = ->(r) { handle_response(r, &block) } if block
56
60
  request = RestClient::Request.new(
57
61
  :method => verb,
58
- :url => "https://open.aliyundrive.com#{sub_res}",
59
- :headers => headers,
62
+ :url => "#{sub_res}".start_with?("/") ? "https://open.aliyundrive.com#{sub_res}" : "#{sub_res}",
63
+ :headers => http_options[:headers] || headers,
60
64
  :payload => http_options[:body],
61
65
  :block_response => block_response,
62
66
  :open_timeout => @config.open_timeout || OPEN_TIMEOUT,
@@ -64,6 +68,7 @@ module Alipan
64
68
  )
65
69
  response = request.execute do |resp, &blk|
66
70
  if resp.code >= 300
71
+ e = RuntimeError.new JSON.parse(resp.body)
67
72
  logger.error(e.to_s)
68
73
  raise e
69
74
  else
@@ -71,6 +76,11 @@ module Alipan
71
76
  end
72
77
  end
73
78
 
79
+ unless response.is_a?(RestClient::Response)
80
+ response = RestClient::Response.create(nil, response, request)
81
+ response.return!
82
+ end
83
+
74
84
  logger.debug("Received HTTP response, code: #{response.code}, headers: " \
75
85
  "#{response.headers}, body: #{response.body}")
76
86
 
data/lib/alipan/object.rb CHANGED
@@ -6,12 +6,7 @@ module Alipan
6
6
  attrs :drive_id, :file_id, :parent_file_id,
7
7
  :name, :size, :file_extension, :content_hash,
8
8
  :category, :type, :thumbnail, :url, :created_at,
9
- :updated_at, :play_cursor, :video_media_metadata,
10
- :video_preview_metadata
9
+ :updated_at, :video_media_metadata, :video_preview_metadata
11
10
 
12
- def initialize(opts = {}, protocol = nil)
13
- super(opts)
14
- @protocol = protocol
15
- end
16
11
  end
17
12
  end
@@ -27,13 +27,14 @@ module Alipan
27
27
  :backup_drive_id => body.fetch(:backup_drive_id.to_s),
28
28
  :folder_id => body.fetch(:folder_id.to_s)
29
29
  }, self)
30
+
30
31
  logger.info("Done get drive, drive: #{drive}")
31
32
 
32
33
  drive
33
34
  end
34
35
 
35
36
  def list_objects(drive_id, parent_file_id, opts = {})
36
- logger.info("Begin list objects, options: #{opts}")
37
+ logger.debug("Begin list object, drive: #{drive_id}, options: #{opts}")
37
38
 
38
39
  payload = {
39
40
  :drive_id => drive_id,
@@ -45,8 +46,23 @@ module Alipan
45
46
  body = JSON.parse(r.body)
46
47
 
47
48
  objects = body[:items.to_s].map do |item|
48
- Object.new(item, self)
49
- end
49
+ Object.new(
50
+ :drive_id => item.fetch(:drive_id.to_s),
51
+ :file_id => item.fetch(:file_id.to_s),
52
+ :parent_file_id => item.fetch(:parent_file_id.to_s),
53
+ :name => item.fetch(:name.to_s),
54
+ :size => item.fetch(:size.to_s),
55
+ :file_extension => item.fetch(:file_extension.to_s),
56
+ :content_hash => item.fetch(:content_hash.to_s),
57
+ :category => item.fetch(:category.to_s),
58
+ :type => item.fetch(:type.to_s),
59
+ :thumbnail => item.fetch(:thumbnail.to_s),
60
+ :url => item.fetch(:url.to_s),
61
+ :created_at => item.fetch(:created_at.to_s),
62
+ :updated_at => item.fetch(:updated_at.to_s),
63
+ :video_media_metadata => item.fetch(:video_media_metadata.to_s),
64
+ :video_preview_metadata => item.fetch(:video_preview_metadata.to_s))
65
+ end || []
50
66
 
51
67
  more = {
52
68
  :marker => body[:next_marker.to_s]
@@ -56,5 +72,55 @@ module Alipan
56
72
 
57
73
  [objects, more]
58
74
  end
75
+
76
+ def get_object(drive_id, object_name, opts = {}, &block)
77
+ logger.debug("Begin get object, drive_id: #{drive_id}, "\
78
+ "object: #{object_name}")
79
+
80
+ payload = {
81
+ :drive_id => drive_id,
82
+ :file_path => "#{object_name}".start_with?("/") ? "#{object_name}" : "/#{object_name}"
83
+ }
84
+
85
+ r = @http.post( {:sub_res => "/adrive/v1.0/openFile/get_by_path"}, {:body => payload.to_json})
86
+ body = JSON.parse(r.body)
87
+
88
+ obj = Object.new(
89
+ :drive_id => body.fetch(:drive_id.to_s),
90
+ :file_id => body.fetch(:file_id.to_s),
91
+ :parent_file_id => body.fetch(:parent_file_id.to_s),
92
+ :name => body.fetch(:name.to_s),
93
+ :size => body.fetch(:size.to_s),
94
+ :file_extension => body.fetch(:file_extension.to_s),
95
+ :content_hash => body.fetch(:content_hash.to_s),
96
+ :category => body.fetch(:category.to_s),
97
+ :type => body.fetch(:type.to_s),
98
+ :thumbnail => body.fetch(:thumbnail.to_s),
99
+ :url => body.fetch(:url.to_s),
100
+ :created_at => body.fetch(:created_at.to_s),
101
+ :updated_at => body.fetch(:updated_at.to_s),
102
+ :video_media_metadata => body.fetch(:video_media_metadata.to_s),
103
+ :video_preview_metadata => body.fetch(:video_preview_metadata.to_s))
104
+
105
+ if block_given?
106
+ if obj.type == 'folder' || obj.file_id.nil?
107
+ yield nil
108
+ else
109
+ payload = {
110
+ :drive_id => drive_id,
111
+ :file_id => obj.file_id
112
+ }
113
+
114
+ r = @http.post( {:sub_res => "/adrive/v1.0/openFile/getDownloadUrl"}, {:body => payload.to_json})
115
+ body = JSON.parse(r.body)
116
+
117
+ @http.get( {:sub_res => body.fetch(:url.to_s)}, {:headers => {}}, &block)
118
+ end
119
+ end
120
+
121
+ logger.debug("Done get object")
122
+
123
+ obj
124
+ end
59
125
  end
60
126
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Alipan
4
- VERSION = "0.1.0"
4
+ VERSION = "0.1.2"
5
5
  end
data/lib/alipan.rb CHANGED
@@ -1,6 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'pry'
4
3
  require_relative "alipan/common"
5
4
  require_relative "alipan/config"
6
5
  require_relative "alipan/client"
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: alipan-sdk
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - freeshenls