alipan-sdk 0.1.0

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 391e43dcbc839df82035ba6a730fe7d274054d5fa8147fc8742847668cca0b46
4
+ data.tar.gz: 96afad1647af9260c3340f6bd9ecde58c593db444ad0cce301e0e1285dc0124f
5
+ SHA512:
6
+ metadata.gz: 95e7da78fb03144dc9c1b26843b0e0d8733391b1ee28a6dbb363964cc39257d1d07ffc5ecbe575cedc4eb46e695ea088791d5275f1d4be0c9fa7e00e0ebbbcb2
7
+ data.tar.gz: '08f4d6470a21d3f7ec5f5e8e37ea45c52834a3baf8c7953715ee8d190114a5fb6d7244737c7f3be9d640f110c76795b78812e35adf6740a56a764234fb08f18f'
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ ## [Unreleased]
2
+
3
+ ## [0.1.0] - 2025-06-20
4
+
5
+ - Initial release
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2025 freeshenls
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,39 @@
1
+ # Alipan SDK for Ruby
2
+
3
+ TODO: Delete this and the text below, and describe your gem
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.
6
+
7
+ ## Installation
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.
10
+
11
+ Install the gem and add to the application's Gemfile by executing:
12
+
13
+ ```bash
14
+ bundle add UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG
15
+ ```
16
+
17
+ If bundler is not being used to manage dependencies, install the gem by executing:
18
+
19
+ ```bash
20
+ gem install UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG
21
+ ```
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+ ## Development
28
+
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.
30
+
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).
32
+
33
+ ## Contributing
34
+
35
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/alipan.
36
+
37
+ ## License
38
+
39
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rspec/core/rake_task"
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ task default: :spec
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Alipan
4
+ class Client
5
+
6
+ def initialize(opts)
7
+ fail ArgumentError, "Argument access_token must be provided" unless opts[:access_token]
8
+
9
+ @config = Config.new(opts)
10
+ @protocol = Protocol.new(@config)
11
+ end
12
+
13
+ def get_drive(opts = {})
14
+ @protocol.get_drive
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,55 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'logger'
4
+
5
+ module Alipan
6
+ module Common
7
+ module Logging
8
+
9
+ MAX_NUM_LOG = 100
10
+ ROTATE_SIZE = 10 * 1024 * 1024
11
+
12
+ # level = Logger::DEBUG | Logger::INFO | Logger::ERROR | Logger::FATAL
13
+ def self.set_log_level(level)
14
+ Logging.logger.level = level
15
+ end
16
+
17
+ def self.set_log_file(file)
18
+ @log_file = file
19
+ end
20
+
21
+ def logger
22
+ Logging.logger
23
+ end
24
+
25
+ private
26
+
27
+ def self.logger
28
+ unless @logger
29
+ # Environment parameter ALIPAN_SDK_LOG_PATH used to set output log to a file,do not output log if not set
30
+ @log_file ||= ENV["ALIPAN_SDK_LOG_PATH"]
31
+ @logger = Logger.new(
32
+ @log_file, MAX_NUM_LOG, ROTATE_SIZE)
33
+ @logger.level = get_env_log_level || Logger::INFO
34
+ end
35
+ @logger
36
+ end
37
+
38
+ def self.get_env_log_level
39
+ return unless ENV["ALIPAN_SDK_LOG_LEVEL"]
40
+ case ENV["ALIPAN_SDK_LOG_LEVEL"].upcase
41
+ when "DEBUG"
42
+ Logger::DEBUG
43
+ when "WARN"
44
+ Logger::WARN
45
+ when "ERROR"
46
+ Logger::ERROR
47
+ when "FATAL"
48
+ Logger::FATAL
49
+ when "UNKNOWN"
50
+ Logger::UNKNOWN
51
+ end
52
+ end
53
+ end
54
+ end
55
+ end
@@ -0,0 +1,25 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Alipan
4
+ module Common
5
+ class Struct
6
+
7
+ def self.attrs(*s)
8
+ define_method(:attrs) {s}
9
+ attr_reader(*s)
10
+ end
11
+
12
+ def initialize(opts = {})
13
+ extra_keys = opts.keys - attrs
14
+ unless extra_keys.empty?
15
+ fail NameError,
16
+ "Unexpected extra keys: #{extra_keys.join(', ')}"
17
+ end
18
+
19
+ attrs.each do |attr|
20
+ instance_variable_set("@#{attr}", opts[attr])
21
+ end
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,4 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "common/struct"
4
+ require_relative "common/logging"
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Alipan
4
+ class Config < Alipan::Common::Struct
5
+
6
+ attrs :access_token, :open_timeout, :read_timeout
7
+
8
+ def initialize(opts = {})
9
+ super(opts)
10
+
11
+ @access_token = @access_token.strip if @access_token
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Alipan
4
+ class Drive < Common::Struct
5
+
6
+ attrs :user_id, :name, :avatar, :default_drive_id, :resource_drive_id, :backup_drive_id, :folder_id
7
+
8
+ def initialize(opts = {}, protocol = nil)
9
+ super(opts)
10
+ @protocol = protocol
11
+ end
12
+
13
+ def list_objects(opts = {})
14
+ Iterator::Objects.new(@protocol, resource_drive_id, 'root', opts).to_enum
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,80 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'rest-client'
4
+
5
+ module Alipan
6
+ class HTTP
7
+
8
+ DEFAULT_CONTENT_TYPE = 'application/json'
9
+ AUTH_HEADER = 'Authorization'
10
+ OPEN_TIMEOUT = 10
11
+ READ_TIMEOUT = 120
12
+
13
+ include Common::Logging
14
+
15
+ def initialize(config)
16
+ @config = config
17
+ end
18
+
19
+ def get(resources = {}, http_options = {}, &block)
20
+ do_request('GET', resources, http_options, &block)
21
+ end
22
+
23
+ def put(resources = {}, http_options = {}, &block)
24
+ do_request('PUT', resources, http_options, &block)
25
+ end
26
+
27
+ def post(resources = {}, http_options = {}, &block)
28
+ do_request('POST', resources, http_options, &block)
29
+ end
30
+
31
+ def delete(resources = {}, http_options = {}, &block)
32
+ do_request('DELETE', resources, http_options, &block)
33
+ end
34
+
35
+ def head(resources = {}, http_options = {}, &block)
36
+ do_request('HEAD', resources, http_options, &block)
37
+ end
38
+
39
+ def options(resources = {}, http_options = {}, &block)
40
+ do_request('OPTIONS', resources, http_options, &block)
41
+ end
42
+
43
+ private
44
+
45
+ def do_request(verb, resources = {}, http_options = {}, &block)
46
+ sub_res = resources[:sub_res]
47
+
48
+ headers = http_options[:headers] || {}
49
+ headers['Content-Type'] ||= DEFAULT_CONTENT_TYPE
50
+ headers[AUTH_HEADER] = @config.access_token if @config.access_token
51
+
52
+ logger.debug("Send HTTP request, verb: #{verb}, resources: " \
53
+ "#{resources}, http options: #{http_options}")
54
+
55
+ block_response = ->(r) { handle_response(r, &block) } if block
56
+ request = RestClient::Request.new(
57
+ :method => verb,
58
+ :url => "https://open.aliyundrive.com#{sub_res}",
59
+ :headers => headers,
60
+ :payload => http_options[:body],
61
+ :block_response => block_response,
62
+ :open_timeout => @config.open_timeout || OPEN_TIMEOUT,
63
+ :read_timeout => @config.read_timeout || READ_TIMEOUT
64
+ )
65
+ response = request.execute do |resp, &blk|
66
+ if resp.code >= 300
67
+ logger.error(e.to_s)
68
+ raise e
69
+ else
70
+ resp.return!(&blk)
71
+ end
72
+ end
73
+
74
+ logger.debug("Received HTTP response, code: #{response.code}, headers: " \
75
+ "#{response.headers}, body: #{response.body}")
76
+
77
+ response
78
+ end
79
+ end
80
+ end
@@ -0,0 +1,48 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Alipan
4
+ module Iterator
5
+ class Base
6
+
7
+ def initialize(protocol, opts = {})
8
+ @protocol = protocol
9
+ @results, @more = [], opts
10
+ end
11
+
12
+ def next
13
+ loop do
14
+ fetch_more if @results.empty?
15
+
16
+ r = @results.shift
17
+ break unless r
18
+
19
+ yield r
20
+ end
21
+ end
22
+
23
+ def to_enum
24
+ self.enum_for(:next)
25
+ end
26
+
27
+ private
28
+ def fetch_more
29
+ return if @more[:truncated] == false
30
+ fetch(@more)
31
+ end
32
+ end
33
+
34
+ class Objects < Base
35
+ def initialize(protocol, drive_id, parent_file_id, opts = {})
36
+ super(protocol, opts)
37
+ @drive_id = drive_id
38
+ @parent_file_id = parent_file_id
39
+ end
40
+
41
+ def fetch(more)
42
+ @results, cont = @protocol.list_objects(@drive_id, @parent_file_id, more)
43
+ @more[:marker] = cont[:marker]
44
+ @more[:truncated] = !cont[:marker].empty?
45
+ end
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Alipan
4
+ class Object < Common::Struct
5
+
6
+ attrs :drive_id, :file_id, :parent_file_id,
7
+ :name, :size, :file_extension, :content_hash,
8
+ :category, :type, :thumbnail, :url, :created_at,
9
+ :updated_at, :play_cursor, :video_media_metadata,
10
+ :video_preview_metadata
11
+
12
+ def initialize(opts = {}, protocol = nil)
13
+ super(opts)
14
+ @protocol = protocol
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,60 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'json'
4
+
5
+ module Alipan
6
+ class Protocol
7
+ include Common::Logging
8
+
9
+ def initialize(config)
10
+ @config = config
11
+ @http = HTTP.new(config)
12
+ end
13
+
14
+ def get_drive(opts = {})
15
+ logger.info("Begin get drive, options: #{opts}")
16
+
17
+ r = @http.post( {:sub_res => "/adrive/v1.0/user/getDriveInfo"}, {})
18
+ body = JSON.parse(r.body)
19
+
20
+ drive = Drive.new(
21
+ {
22
+ :user_id => body.fetch(:user_id.to_s),
23
+ :name => body.fetch(:name.to_s),
24
+ :avatar => body.fetch(:avatar.to_s),
25
+ :default_drive_id => body.fetch(:default_drive_id.to_s),
26
+ :resource_drive_id => body.fetch(:resource_drive_id.to_s),
27
+ :backup_drive_id => body.fetch(:backup_drive_id.to_s),
28
+ :folder_id => body.fetch(:folder_id.to_s)
29
+ }, self)
30
+ logger.info("Done get drive, drive: #{drive}")
31
+
32
+ drive
33
+ end
34
+
35
+ def list_objects(drive_id, parent_file_id, opts = {})
36
+ logger.info("Begin list objects, options: #{opts}")
37
+
38
+ payload = {
39
+ :drive_id => drive_id,
40
+ :parent_file_id => parent_file_id,
41
+ :marker => opts[:marker]
42
+ }
43
+
44
+ r = @http.post( {:sub_res => "/adrive/v1.0/openFile/list"}, {:body => payload.to_json})
45
+ body = JSON.parse(r.body)
46
+
47
+ objects = body[:items.to_s].map do |item|
48
+ Object.new(item, self)
49
+ end
50
+
51
+ more = {
52
+ :marker => body[:next_marker.to_s]
53
+ }
54
+
55
+ logger.debug("Done list object. objects: #{objects}, more: #{more}")
56
+
57
+ [objects, more]
58
+ end
59
+ end
60
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Alipan
4
+ VERSION = "0.1.0"
5
+ end
data/lib/alipan.rb ADDED
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'pry'
4
+ require_relative "alipan/common"
5
+ require_relative "alipan/config"
6
+ require_relative "alipan/client"
7
+ require_relative "alipan/http"
8
+ require_relative "alipan/protocol"
9
+ require_relative "alipan/iterator"
10
+ require_relative "alipan/drive"
11
+ require_relative "alipan/object"
data/sig/alipan.rbs ADDED
@@ -0,0 +1,4 @@
1
+ module Alipan
2
+ VERSION: String
3
+ # See the writing guide of rbs: https://github.com/ruby/rbs#guides
4
+ end
metadata ADDED
@@ -0,0 +1,76 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: alipan-sdk
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - freeshenls
8
+ bindir: exe
9
+ cert_chain: []
10
+ date: 1980-01-02 00:00:00.000000000 Z
11
+ dependencies:
12
+ - !ruby/object:Gem::Dependency
13
+ name: rest-client
14
+ requirement: !ruby/object:Gem::Requirement
15
+ requirements:
16
+ - - "~>"
17
+ - !ruby/object:Gem::Version
18
+ version: '2.1'
19
+ type: :runtime
20
+ prerelease: false
21
+ version_requirements: !ruby/object:Gem::Requirement
22
+ requirements:
23
+ - - "~>"
24
+ - !ruby/object:Gem::Version
25
+ version: '2.1'
26
+ description: A Ruby program to facilitate accessing Alipan Service
27
+ email:
28
+ - freeshenls@163.com
29
+ executables: []
30
+ extensions: []
31
+ extra_rdoc_files: []
32
+ files:
33
+ - ".rspec"
34
+ - CHANGELOG.md
35
+ - LICENSE.txt
36
+ - README.md
37
+ - Rakefile
38
+ - lib/alipan.rb
39
+ - lib/alipan/client.rb
40
+ - lib/alipan/common.rb
41
+ - lib/alipan/common/logging.rb
42
+ - lib/alipan/common/struct.rb
43
+ - lib/alipan/config.rb
44
+ - lib/alipan/drive.rb
45
+ - lib/alipan/http.rb
46
+ - lib/alipan/iterator.rb
47
+ - lib/alipan/object.rb
48
+ - lib/alipan/protocol.rb
49
+ - lib/alipan/version.rb
50
+ - sig/alipan.rbs
51
+ homepage: https://github.com/freeshenls/alipan-ruby-sdk
52
+ licenses:
53
+ - MIT
54
+ metadata:
55
+ allowed_push_host: https://rubygems.org
56
+ homepage_uri: https://github.com/freeshenls/alipan-ruby-sdk
57
+ source_code_uri: https://github.com/freeshenls/alipan-ruby-sdk
58
+ changelog_uri: https://github.com/freeshenls/alipan-ruby-sdk/blob/main/CHANGELOG.md
59
+ rdoc_options: []
60
+ require_paths:
61
+ - lib
62
+ required_ruby_version: !ruby/object:Gem::Requirement
63
+ requirements:
64
+ - - ">="
65
+ - !ruby/object:Gem::Version
66
+ version: 3.1.0
67
+ required_rubygems_version: !ruby/object:Gem::Requirement
68
+ requirements:
69
+ - - ">="
70
+ - !ruby/object:Gem::Version
71
+ version: '0'
72
+ requirements: []
73
+ rubygems_version: 3.6.7
74
+ specification_version: 4
75
+ summary: Alipan SDK for Ruby
76
+ test_files: []