alipan-sdk 0.1.1 → 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: d3f53b591622f3f464672c3e9bb90f51cf0de5d3cd60442513700cb2b13df5ac
4
- data.tar.gz: 635f457fddba0eddd3909dd3f53b872801dfaa041555b802082a9a2850857abd
3
+ metadata.gz: e4628bf443c49974992f06af3a266a6554f631efe8affb8e96fe4a0f1211c91e
4
+ data.tar.gz: d3dc3210dc35128e261c25454dfa36f5fd16384ad2d280e226520b9e03f81ac7
5
5
  SHA512:
6
- metadata.gz: bcf71c608206c88d31caf4782fe724538128f56acfdfcbe5a88aa13c2413a2d243c2962cef5f6f2af3eae6bbb30ee39e0b7868f80860cda4cd587df7633387bb
7
- data.tar.gz: 87807a884d03c3bc416625f100f28c86813f701dbdd6cb495118e17d1450bb7dbc829662d0a1c58ab3185cafb535f787f277ed4cd13801ebd073df8d26994468
6
+ metadata.gz: 9356ddf651b4f762cbcfe0c20c70dd3050f50307975647c0d21054a1b1b101047ebbc7d27f15c03c53abaa885bb251c9ea0a204dca95ca0e00da0149fa78189c
7
+ data.tar.gz: 1ee6392d663d57d12490c4ae12f007ad2343cee00bf2bb34dd729d6131ea6468b369b26c758b69a74cc060a1bde8b4d91d6fdd1fafa0480686a570d61a8f9549
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,
@@ -72,6 +76,11 @@ module Alipan
72
76
  end
73
77
  end
74
78
 
79
+ unless response.is_a?(RestClient::Response)
80
+ response = RestClient::Response.create(nil, response, request)
81
+ response.return!
82
+ end
83
+
75
84
  logger.debug("Received HTTP response, code: #{response.code}, headers: " \
76
85
  "#{response.headers}, body: #{response.body}")
77
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,
@@ -46,7 +47,6 @@ module Alipan
46
47
 
47
48
  objects = body[:items.to_s].map do |item|
48
49
  Object.new(
49
- {
50
50
  :drive_id => item.fetch(:drive_id.to_s),
51
51
  :file_id => item.fetch(:file_id.to_s),
52
52
  :parent_file_id => item.fetch(:parent_file_id.to_s),
@@ -60,11 +60,9 @@ module Alipan
60
60
  :url => item.fetch(:url.to_s),
61
61
  :created_at => item.fetch(:created_at.to_s),
62
62
  :updated_at => item.fetch(:updated_at.to_s),
63
- :play_cursor => item.fetch(:play_cursor.to_s),
64
63
  :video_media_metadata => item.fetch(:video_media_metadata.to_s),
65
- :video_preview_metadata => item.fetch(:video_preview_metadata.to_s)
66
- }, self)
67
- end
64
+ :video_preview_metadata => item.fetch(:video_preview_metadata.to_s))
65
+ end || []
68
66
 
69
67
  more = {
70
68
  :marker => body[:next_marker.to_s]
@@ -74,5 +72,55 @@ module Alipan
74
72
 
75
73
  [objects, more]
76
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
77
125
  end
78
126
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Alipan
4
- VERSION = "0.1.1"
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.1
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - freeshenls