brick_ftp 0.8.4 → 1.0.0.beta
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.circleci/config.yml +0 -5
- data/.rubocop.yml +2 -1
- data/.yardopts +1 -0
- data/CHANGELOG.md +16 -12
- data/Gemfile +2 -0
- data/README.md +24 -43
- data/Rakefile +2 -0
- data/bin/console +1 -0
- data/brick_ftp.gemspec +10 -9
- data/exe/brick_ftp +1 -0
- data/lib/brick_ftp.rb +5 -78
- data/lib/brick_ftp/client.rb +35 -411
- data/lib/brick_ftp/core_ext.rb +8 -0
- data/lib/brick_ftp/core_ext/hash.rb +13 -0
- data/lib/brick_ftp/core_ext/hash/compact.rb +13 -0
- data/lib/brick_ftp/core_ext/struct.rb +13 -0
- data/lib/brick_ftp/core_ext/struct/new.rb +16 -0
- data/lib/brick_ftp/restful_api.rb +82 -0
- data/lib/brick_ftp/restful_api/add_group_member.rb +44 -0
- data/lib/brick_ftp/restful_api/client.rb +179 -0
- data/lib/brick_ftp/restful_api/command.rb +21 -0
- data/lib/brick_ftp/restful_api/complete_upload.rb +46 -0
- data/lib/brick_ftp/restful_api/continue_upload.rb +50 -0
- data/lib/brick_ftp/restful_api/copy_folder.rb +46 -0
- data/lib/brick_ftp/restful_api/count_folder_contents.rb +36 -0
- data/lib/brick_ftp/restful_api/count_users.rb +22 -0
- data/lib/brick_ftp/restful_api/create_api_key.rb +46 -0
- data/lib/brick_ftp/restful_api/create_behavior.rb +42 -0
- data/lib/brick_ftp/restful_api/create_bundle.rb +40 -0
- data/lib/brick_ftp/restful_api/create_folder.rb +25 -0
- data/lib/brick_ftp/restful_api/create_group.rb +42 -0
- data/lib/brick_ftp/restful_api/create_notification.rb +44 -0
- data/lib/brick_ftp/restful_api/create_permission.rb +48 -0
- data/lib/brick_ftp/restful_api/create_public_key.rb +42 -0
- data/lib/brick_ftp/restful_api/create_user.rb +90 -0
- data/lib/brick_ftp/restful_api/create_user_in_group.rb +60 -0
- data/lib/brick_ftp/restful_api/delete_api_key.rb +23 -0
- data/lib/brick_ftp/restful_api/delete_behavior.rb +22 -0
- data/lib/brick_ftp/restful_api/delete_bundle.rb +22 -0
- data/lib/brick_ftp/restful_api/delete_folder.rb +32 -0
- data/lib/brick_ftp/restful_api/delete_group.rb +23 -0
- data/lib/brick_ftp/restful_api/delete_notification.rb +23 -0
- data/lib/brick_ftp/restful_api/delete_permission.rb +23 -0
- data/lib/brick_ftp/restful_api/delete_public_key.rb +23 -0
- data/lib/brick_ftp/restful_api/delete_user.rb +25 -0
- data/lib/brick_ftp/restful_api/download_file.rb +44 -0
- data/lib/brick_ftp/restful_api/get_api_key.rb +25 -0
- data/lib/brick_ftp/restful_api/get_behavior.rb +25 -0
- data/lib/brick_ftp/restful_api/get_bundle.rb +25 -0
- data/lib/brick_ftp/restful_api/get_bundle_zip.rb +48 -0
- data/lib/brick_ftp/restful_api/get_file_in_bundle.rb +49 -0
- data/lib/brick_ftp/restful_api/get_folder_size.rb +25 -0
- data/lib/brick_ftp/restful_api/get_group.rb +26 -0
- data/lib/brick_ftp/restful_api/get_public_key.rb +25 -0
- data/lib/brick_ftp/restful_api/get_site_usage.rb +19 -0
- data/lib/brick_ftp/restful_api/get_user.rb +26 -0
- data/lib/brick_ftp/restful_api/list_api_keys.rb +25 -0
- data/lib/brick_ftp/restful_api/list_behaviors.rb +23 -0
- data/lib/brick_ftp/restful_api/list_bundle_contents.rb +58 -0
- data/lib/brick_ftp/restful_api/list_bundles.rb +23 -0
- data/lib/brick_ftp/restful_api/list_folder_behaviors.rb +39 -0
- data/lib/brick_ftp/restful_api/list_folders.rb +108 -0
- data/lib/brick_ftp/restful_api/list_groups.rb +23 -0
- data/lib/brick_ftp/restful_api/list_notifications.rb +23 -0
- data/lib/brick_ftp/restful_api/list_permissions.rb +41 -0
- data/lib/brick_ftp/restful_api/list_public_keys.rb +25 -0
- data/lib/brick_ftp/restful_api/list_users.rb +60 -0
- data/lib/brick_ftp/restful_api/move_folder.rb +41 -0
- data/lib/brick_ftp/restful_api/remove_group_member.rb +23 -0
- data/lib/brick_ftp/restful_api/retrieve_file_history.rb +41 -0
- data/lib/brick_ftp/restful_api/retrieve_folder_history.rb +41 -0
- data/lib/brick_ftp/restful_api/retrieve_history.rb +54 -0
- data/lib/brick_ftp/restful_api/retrieve_login_history.rb +38 -0
- data/lib/brick_ftp/restful_api/retrieve_site_history.rb +38 -0
- data/lib/brick_ftp/restful_api/retrieve_user_history.rb +40 -0
- data/lib/brick_ftp/restful_api/search_user.rb +28 -0
- data/lib/brick_ftp/restful_api/start_upload.rb +27 -0
- data/lib/brick_ftp/restful_api/unlock_user.rb +25 -0
- data/lib/brick_ftp/restful_api/update_behavior.rb +39 -0
- data/lib/brick_ftp/restful_api/update_group.rb +44 -0
- data/lib/brick_ftp/restful_api/update_group_member.rb +40 -0
- data/lib/brick_ftp/restful_api/update_user.rb +101 -0
- data/lib/brick_ftp/restful_api/upload_file.rb +65 -0
- data/lib/brick_ftp/types.rb +23 -0
- data/lib/brick_ftp/types/behavior.rb +27 -0
- data/lib/brick_ftp/types/bundle.rb +34 -0
- data/lib/brick_ftp/types/bundle_content.rb +25 -0
- data/lib/brick_ftp/types/bundle_zip.rb +19 -0
- data/lib/brick_ftp/types/file.rb +45 -0
- data/lib/brick_ftp/types/file_in_bundle.rb +25 -0
- data/lib/brick_ftp/types/folder_contents_count.rb +24 -0
- data/lib/brick_ftp/types/group.rb +31 -0
- data/lib/brick_ftp/types/group_membership.rb +27 -0
- data/lib/brick_ftp/types/history.rb +43 -0
- data/lib/brick_ftp/types/notification.rb +31 -0
- data/lib/brick_ftp/types/permission.rb +35 -0
- data/lib/brick_ftp/types/site_usage.rb +23 -0
- data/lib/brick_ftp/types/upload.rb +45 -0
- data/lib/brick_ftp/types/user.rb +79 -0
- data/lib/brick_ftp/types/user_api_key.rb +33 -0
- data/lib/brick_ftp/types/user_public_key.rb +28 -0
- data/lib/brick_ftp/utils.rb +2 -0
- data/lib/brick_ftp/utils/chunk_io.rb +5 -4
- data/lib/brick_ftp/version.rb +3 -1
- metadata +123 -86
- data/lib/brick_ftp/api.rb +0 -9
- data/lib/brick_ftp/api/authentication.rb +0 -28
- data/lib/brick_ftp/api/authentication/session.rb +0 -25
- data/lib/brick_ftp/api/base.rb +0 -119
- data/lib/brick_ftp/api/behavior.rb +0 -19
- data/lib/brick_ftp/api/bundle.rb +0 -17
- data/lib/brick_ftp/api/bundle_content.rb +0 -18
- data/lib/brick_ftp/api/bundle_download.rb +0 -18
- data/lib/brick_ftp/api/file.rb +0 -20
- data/lib/brick_ftp/api/file_operation.rb +0 -6
- data/lib/brick_ftp/api/file_operation/copy.rb +0 -10
- data/lib/brick_ftp/api/file_operation/move.rb +0 -10
- data/lib/brick_ftp/api/file_operation/upload.rb +0 -66
- data/lib/brick_ftp/api/file_operation/uploading_result.rb +0 -22
- data/lib/brick_ftp/api/file_operation/uploading_session.rb +0 -95
- data/lib/brick_ftp/api/folder.rb +0 -22
- data/lib/brick_ftp/api/folder_behavior.rb +0 -12
- data/lib/brick_ftp/api/group.rb +0 -16
- data/lib/brick_ftp/api/history.rb +0 -6
- data/lib/brick_ftp/api/history/file.rb +0 -23
- data/lib/brick_ftp/api/history/folder.rb +0 -23
- data/lib/brick_ftp/api/history/login.rb +0 -23
- data/lib/brick_ftp/api/history/site.rb +0 -23
- data/lib/brick_ftp/api/history/user.rb +0 -23
- data/lib/brick_ftp/api/notification.rb +0 -14
- data/lib/brick_ftp/api/permission.rb +0 -17
- data/lib/brick_ftp/api/public_key.rb +0 -15
- data/lib/brick_ftp/api/site_usage.rb +0 -21
- data/lib/brick_ftp/api/user.rb +0 -36
- data/lib/brick_ftp/api_component.rb +0 -81
- data/lib/brick_ftp/api_definition.rb +0 -65
- data/lib/brick_ftp/cli.rb +0 -7
- data/lib/brick_ftp/cli/config.rb +0 -20
- data/lib/brick_ftp/cli/main.rb +0 -26
- data/lib/brick_ftp/cli/site.rb +0 -10
- data/lib/brick_ftp/configuration.rb +0 -121
- data/lib/brick_ftp/http_client.rb +0 -130
- data/lib/brick_ftp/log_formatter.rb +0 -15
- data/lib/brick_ftp/webhook.rb +0 -4
- data/lib/brick_ftp/webhook/request.rb +0 -28
@@ -0,0 +1,13 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module BrickFTP
|
4
|
+
module CoreExt
|
5
|
+
module Hash
|
6
|
+
autoload :Compact, 'brick_ftp/core_ext/hash/compact'
|
7
|
+
|
8
|
+
refine ::Hash do
|
9
|
+
include(BrickFTP::CoreExt::Hash::Compact) unless ::Hash.instance_methods(false).include?(:compact)
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
@@ -0,0 +1,13 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module BrickFTP
|
4
|
+
module CoreExt
|
5
|
+
module Struct
|
6
|
+
autoload :New, 'brick_ftp/core_ext/struct/new'
|
7
|
+
|
8
|
+
refine ::Struct do
|
9
|
+
::Struct.singleton_class.prepend(BrickFTP::CoreExt::Struct::New) if RUBY_VERSION < '2.5.0'
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module BrickFTP
|
4
|
+
module CoreExt
|
5
|
+
module Struct
|
6
|
+
module New
|
7
|
+
def new(*args, keyword_init: false, &block)
|
8
|
+
super(*args) do
|
9
|
+
define_method(:initialize) { |**kwargs| super(*members.map { |k| kwargs[k] }) } if keyword_init
|
10
|
+
class_eval(&block) if block
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
@@ -0,0 +1,82 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module BrickFTP
|
4
|
+
module RESTfulAPI
|
5
|
+
autoload :Client, 'brick_ftp/restful_api/client'
|
6
|
+
autoload :Command, 'brick_ftp/restful_api/command'
|
7
|
+
autoload :GetSiteUsage, 'brick_ftp/restful_api/get_site_usage'
|
8
|
+
# ref. https://developers.brickftp.com/#users
|
9
|
+
autoload :ListUsers, 'brick_ftp/restful_api/list_users'
|
10
|
+
autoload :CountUsers, 'brick_ftp/restful_api/count_users'
|
11
|
+
autoload :SearchUser, 'brick_ftp/restful_api/search_user'
|
12
|
+
autoload :GetUser, 'brick_ftp/restful_api/get_user'
|
13
|
+
autoload :CreateUser, 'brick_ftp/restful_api/create_user'
|
14
|
+
autoload :UpdateUser, 'brick_ftp/restful_api/update_user'
|
15
|
+
autoload :DeleteUser, 'brick_ftp/restful_api/delete_user'
|
16
|
+
autoload :UnlockUser, 'brick_ftp/restful_api/unlock_user'
|
17
|
+
# ref. https://developers.brickftp.com/#user-api-keys
|
18
|
+
autoload :ListAPIKeys, 'brick_ftp/restful_api/list_api_keys'
|
19
|
+
autoload :GetAPIKey, 'brick_ftp/restful_api/get_api_key'
|
20
|
+
autoload :CreateAPIKey, 'brick_ftp/restful_api/create_api_key'
|
21
|
+
autoload :DeleteAPIKey, 'brick_ftp/restful_api/delete_api_key'
|
22
|
+
# ref. https://developers.brickftp.com/#user-public-keys
|
23
|
+
autoload :ListPublicKeys, 'brick_ftp/restful_api/list_public_keys'
|
24
|
+
autoload :GetPublicKey, 'brick_ftp/restful_api/get_public_key'
|
25
|
+
autoload :CreatePublicKey, 'brick_ftp/restful_api/create_public_key'
|
26
|
+
autoload :DeletePublicKey, 'brick_ftp/restful_api/delete_public_key'
|
27
|
+
# ref. https://developers.brickftp.com/#groups
|
28
|
+
autoload :ListGroups, 'brick_ftp/restful_api/list_groups'
|
29
|
+
autoload :GetGroup, 'brick_ftp/restful_api/get_group'
|
30
|
+
autoload :CreateGroup, 'brick_ftp/restful_api/create_group'
|
31
|
+
autoload :UpdateGroup, 'brick_ftp/restful_api/update_group'
|
32
|
+
autoload :DeleteGroup, 'brick_ftp/restful_api/delete_group'
|
33
|
+
autoload :CreateUserInGroup, 'brick_ftp/restful_api/create_user_in_group'
|
34
|
+
autoload :AddGroupMember, 'brick_ftp/restful_api/add_group_member'
|
35
|
+
autoload :UpdateGroupMember, 'brick_ftp/restful_api/update_group_member'
|
36
|
+
autoload :RemoveGroupMember, 'brick_ftp/restful_api/remove_group_member'
|
37
|
+
# ref. https://developers.brickftp.com/#permissions
|
38
|
+
autoload :ListPermissions, 'brick_ftp/restful_api/list_permissions'
|
39
|
+
autoload :CreatePermission, 'brick_ftp/restful_api/create_permission'
|
40
|
+
autoload :DeletePermission, 'brick_ftp/restful_api/delete_permission'
|
41
|
+
# ref. https://developers.brickftp.com/#notifications
|
42
|
+
autoload :ListNotifications, 'brick_ftp/restful_api/list_notifications'
|
43
|
+
autoload :CreateNotification, 'brick_ftp/restful_api/create_notification'
|
44
|
+
autoload :DeleteNotification, 'brick_ftp/restful_api/delete_notification'
|
45
|
+
# ref. https://developers.brickftp.com/#history
|
46
|
+
autoload :RetrieveHistory, 'brick_ftp/restful_api/retrieve_history'
|
47
|
+
autoload :RetrieveSiteHistory, 'brick_ftp/restful_api/retrieve_site_history'
|
48
|
+
autoload :RetrieveLoginHistory, 'brick_ftp/restful_api/retrieve_login_history'
|
49
|
+
autoload :RetrieveUserHistory, 'brick_ftp/restful_api/retrieve_user_history'
|
50
|
+
autoload :RetrieveFolderHistory, 'brick_ftp/restful_api/retrieve_folder_history'
|
51
|
+
autoload :RetrieveFileHistory, 'brick_ftp/restful_api/retrieve_file_history'
|
52
|
+
# ref. https://developers.brickftp.com/#bundles
|
53
|
+
autoload :ListBundles, 'brick_ftp/restful_api/list_bundles'
|
54
|
+
autoload :GetBundle, 'brick_ftp/restful_api/get_bundle'
|
55
|
+
autoload :CreateBundle, 'brick_ftp/restful_api/create_bundle'
|
56
|
+
autoload :DeleteBundle, 'brick_ftp/restful_api/delete_bundle'
|
57
|
+
autoload :ListBundleContents, 'brick_ftp/restful_api/list_bundle_contents'
|
58
|
+
autoload :GetFileInBundle, 'brick_ftp/restful_api/get_file_in_bundle'
|
59
|
+
autoload :GetBundleZip, 'brick_ftp/restful_api/get_bundle_zip'
|
60
|
+
# ref. https://developers.brickftp.com/#behaviors
|
61
|
+
autoload :ListBehaviors, 'brick_ftp/restful_api/list_behaviors'
|
62
|
+
autoload :ListFolderBehaviors, 'brick_ftp/restful_api/list_folder_behaviors'
|
63
|
+
autoload :GetBehavior, 'brick_ftp/restful_api/get_behavior'
|
64
|
+
autoload :CreateBehavior, 'brick_ftp/restful_api/create_behavior'
|
65
|
+
autoload :UpdateBehavior, 'brick_ftp/restful_api/update_behavior'
|
66
|
+
autoload :DeleteBehavior, 'brick_ftp/restful_api/delete_behavior'
|
67
|
+
# ref. https://developers.brickftp.com/#file-and-folder-operations
|
68
|
+
autoload :ListFolders, 'brick_ftp/restful_api/list_folders'
|
69
|
+
autoload :CreateFolder, 'brick_ftp/restful_api/create_folder'
|
70
|
+
autoload :CountFolderContents, 'brick_ftp/restful_api/count_folder_contents'
|
71
|
+
autoload :GetFolderSize, 'brick_ftp/restful_api/get_folder_size'
|
72
|
+
autoload :DownloadFile, 'brick_ftp/restful_api/download_file'
|
73
|
+
autoload :MoveFolder, 'brick_ftp/restful_api/move_folder'
|
74
|
+
autoload :CopyFolder, 'brick_ftp/restful_api/copy_folder'
|
75
|
+
autoload :DeleteFolder, 'brick_ftp/restful_api/delete_folder'
|
76
|
+
# ref. https://developers.brickftp.com/#file-uploading
|
77
|
+
autoload :StartUpload, 'brick_ftp/restful_api/start_upload'
|
78
|
+
autoload :ContinueUpload, 'brick_ftp/restful_api/continue_upload'
|
79
|
+
autoload :CompleteUpload, 'brick_ftp/restful_api/complete_upload'
|
80
|
+
autoload :UploadFile, 'brick_ftp/restful_api/upload_file'
|
81
|
+
end
|
82
|
+
end
|
@@ -0,0 +1,44 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module BrickFTP
|
4
|
+
module RESTfulAPI
|
5
|
+
# Add a member
|
6
|
+
#
|
7
|
+
# @see https://developers.brickftp.com/#add-a-member Add a member
|
8
|
+
#
|
9
|
+
# ### Params
|
10
|
+
#
|
11
|
+
# PARAMETER | TYPE | DESCRIPTION
|
12
|
+
# --------- | ------- | -----------
|
13
|
+
# admin | boolean | Indicates whether the user is an administrator of the group.
|
14
|
+
#
|
15
|
+
class AddGroupMember
|
16
|
+
include Command
|
17
|
+
using BrickFTP::CoreExt::Struct
|
18
|
+
using BrickFTP::CoreExt::Hash
|
19
|
+
|
20
|
+
Params = Struct.new(
|
21
|
+
'AddGroupMemberParams',
|
22
|
+
:admin,
|
23
|
+
keyword_init: true
|
24
|
+
)
|
25
|
+
|
26
|
+
# Adds a user to a group.
|
27
|
+
#
|
28
|
+
# - By default, the member will not be an admin.
|
29
|
+
# - If the user is already a member of the group, their attributes will be updated to match the request.
|
30
|
+
#
|
31
|
+
# @param [Integer] group_id ID of the group the membership is associated with.
|
32
|
+
# @param [Integer] user_id ID of the user the membership is associated with.
|
33
|
+
# @param [BrickFTP::RESTfulAPI::AddGroupMember::Params] params parameters
|
34
|
+
# @return [BrickFTP::Types::GroupMembership] group membership
|
35
|
+
# @raise [BrickFTP::RESTfulAPI::Error] exception
|
36
|
+
#
|
37
|
+
def call(group_id, user_id, params)
|
38
|
+
res = client.put("/api/rest/v1/groups/#{group_id}/memberships/#{user_id}.json", membership: params.to_h.compact)
|
39
|
+
|
40
|
+
BrickFTP::Types::GroupMembership.new(res.symbolize_keys)
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
@@ -0,0 +1,179 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module BrickFTP
|
4
|
+
module RESTfulAPI
|
5
|
+
class Client
|
6
|
+
using BrickFTP::CoreExt::Struct
|
7
|
+
|
8
|
+
OPEN_TIMEOUT = 60
|
9
|
+
READ_TIMEOUT = 60
|
10
|
+
USER_AGENT = 'BrickFTP Client/1.0 (https://github.com/koshigoe/brick_ftp)'
|
11
|
+
|
12
|
+
# ref. https://developers.brickftp.com/#errors
|
13
|
+
ErrorResponse = Struct.new(
|
14
|
+
'RESTfulAPIErrorResponse',
|
15
|
+
:error,
|
16
|
+
:'http-code',
|
17
|
+
:errors,
|
18
|
+
keyword_init: true
|
19
|
+
)
|
20
|
+
|
21
|
+
class Error < StandardError
|
22
|
+
attr_reader :error
|
23
|
+
|
24
|
+
# @param [BrickFTP::Adapters::RESTfulAPI::ErrorResponse] error
|
25
|
+
def initialize(error)
|
26
|
+
super error.error
|
27
|
+
|
28
|
+
@error = error
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
# Initialize REST API client.
|
33
|
+
#
|
34
|
+
# @param [String] subdomain
|
35
|
+
# @param [String] api_key
|
36
|
+
#
|
37
|
+
def initialize(subdomain, api_key)
|
38
|
+
@http = Net::HTTP.new("#{subdomain}.brickftp.com", 443)
|
39
|
+
@http.use_ssl = true
|
40
|
+
@http.open_timeout = OPEN_TIMEOUT
|
41
|
+
@http.read_timeout = READ_TIMEOUT
|
42
|
+
@request_headers = {
|
43
|
+
'User-Agent' => USER_AGENT,
|
44
|
+
'Content-Type' => 'application/json',
|
45
|
+
'Accept' => 'application/json',
|
46
|
+
}
|
47
|
+
@api_key = api_key
|
48
|
+
end
|
49
|
+
|
50
|
+
# Send HTTP request via GET method.
|
51
|
+
#
|
52
|
+
# @param [String] path the request path including query string.
|
53
|
+
# @param [Hash, nil] headers additional request headers
|
54
|
+
# @return [Hash] JSON parsed object.
|
55
|
+
#
|
56
|
+
def get(path, headers = nil)
|
57
|
+
req = Net::HTTP::Get.new(path, (headers || {}).merge(@request_headers))
|
58
|
+
req.basic_auth(@api_key, 'x')
|
59
|
+
res = @http.start { |session| session.request(req) }
|
60
|
+
|
61
|
+
handle_response(res)
|
62
|
+
end
|
63
|
+
|
64
|
+
# Send HTTP request via POST method.
|
65
|
+
#
|
66
|
+
# @param [String] path the request path including query string.
|
67
|
+
# @param [Hash, nil] data the request body
|
68
|
+
# @param [Hash, nil] headers additional request headers
|
69
|
+
# @return [Hash] JSON parsed object.
|
70
|
+
#
|
71
|
+
def post(path, data = nil, headers = nil)
|
72
|
+
req = Net::HTTP::Post.new(path, (headers || {}).merge(@request_headers))
|
73
|
+
req.basic_auth(@api_key, 'x')
|
74
|
+
req.body = data.to_json unless data.nil?
|
75
|
+
res = @http.start { |session| session.request(req) }
|
76
|
+
|
77
|
+
handle_response(res)
|
78
|
+
end
|
79
|
+
|
80
|
+
# Send HTTP request via PUT method.
|
81
|
+
#
|
82
|
+
# @param [String] path the request path including query string.
|
83
|
+
# @param [Hash, nil] data the request body
|
84
|
+
# @param [Hash, nil] headers additional request headers
|
85
|
+
# @return [Hash] JSON parsed object.
|
86
|
+
#
|
87
|
+
def put(path, data = nil, headers = nil)
|
88
|
+
req = Net::HTTP::Put.new(path, (headers || {}).merge(@request_headers))
|
89
|
+
req.basic_auth(@api_key, 'x')
|
90
|
+
req.body = data.to_json unless data.nil?
|
91
|
+
res = @http.start { |session| session.request(req) }
|
92
|
+
|
93
|
+
handle_response(res)
|
94
|
+
end
|
95
|
+
|
96
|
+
# Send HTTP request via PATCH method.
|
97
|
+
#
|
98
|
+
# @param [String] path the request path including query string.
|
99
|
+
# @param [Hash, nil] data the request body
|
100
|
+
# @param [Hash, nil] headers additional request headers
|
101
|
+
# @return [Hash] JSON parsed object.
|
102
|
+
#
|
103
|
+
def patch(path, data = nil, headers = nil)
|
104
|
+
req = Net::HTTP::Patch.new(path, (headers || {}).merge(@request_headers))
|
105
|
+
req.basic_auth(@api_key, 'x')
|
106
|
+
req.body = data.to_json unless data.nil?
|
107
|
+
res = @http.start { |session| session.request(req) }
|
108
|
+
|
109
|
+
handle_response(res)
|
110
|
+
end
|
111
|
+
|
112
|
+
# Send HTTP request via DELETE method.
|
113
|
+
#
|
114
|
+
# @param [String] path the request path including query string.
|
115
|
+
# @param [Hash, nil] headers additional request headers
|
116
|
+
# @return [Hash] JSON parsed object.
|
117
|
+
#
|
118
|
+
def delete(path, headers = nil)
|
119
|
+
req = Net::HTTP::Delete.new(path, (headers || {}).merge(@request_headers))
|
120
|
+
req.basic_auth(@api_key, 'x')
|
121
|
+
res = @http.start { |session| session.request(req) }
|
122
|
+
|
123
|
+
handle_response(res)
|
124
|
+
end
|
125
|
+
|
126
|
+
# Upload file.
|
127
|
+
#
|
128
|
+
# @param [String] http_method Value is `PUT` or `POST`, and is the HTTP method used when uploading the file.
|
129
|
+
# @param [String] upload_url The URL where the file is uploaded to.
|
130
|
+
# @param [IO] io uploading data
|
131
|
+
# @return [Integer] content length
|
132
|
+
#
|
133
|
+
def upload_file(http_method, upload_url, io)
|
134
|
+
raise ArgumentError, "Unsupported HTTP method `#{http_method}`" unless %w[POST PUT].include?(http_method)
|
135
|
+
|
136
|
+
uri = URI.parse(upload_url)
|
137
|
+
http = Net::HTTP.new(uri.host, uri.port)
|
138
|
+
http.use_ssl = uri.scheme == 'https'
|
139
|
+
req = Net::HTTP.const_get(http_method.capitalize).new(uri.request_uri)
|
140
|
+
req.body_stream = io
|
141
|
+
req['Content-Length'] = io.size
|
142
|
+
res = http.start { |session| session.request(req) }
|
143
|
+
|
144
|
+
return io.size if res.is_a?(Net::HTTPSuccess)
|
145
|
+
raise Error, parse_error_response(res)
|
146
|
+
end
|
147
|
+
|
148
|
+
private
|
149
|
+
|
150
|
+
def handle_response(response)
|
151
|
+
case response
|
152
|
+
when Net::HTTPSuccess
|
153
|
+
parse_success_response(response)
|
154
|
+
else
|
155
|
+
error = parse_error_response(response)
|
156
|
+
raise Error, error
|
157
|
+
end
|
158
|
+
end
|
159
|
+
|
160
|
+
def parse_success_response(response)
|
161
|
+
JSON.parse(response.body)
|
162
|
+
end
|
163
|
+
|
164
|
+
def parse_error_response(response)
|
165
|
+
parsed = begin
|
166
|
+
JSON.parse(response.body)
|
167
|
+
rescue StandardError
|
168
|
+
{}
|
169
|
+
end
|
170
|
+
parsed = {} unless parsed.is_a?(Hash)
|
171
|
+
|
172
|
+
ErrorResponse.new(parsed.symbolize_keys).tap do |e|
|
173
|
+
e['http-code'] ||= response.code
|
174
|
+
e['error'] ||= response.body
|
175
|
+
end
|
176
|
+
end
|
177
|
+
end
|
178
|
+
end
|
179
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module BrickFTP
|
4
|
+
module RESTfulAPI
|
5
|
+
module Command
|
6
|
+
def self.included(klass)
|
7
|
+
klass.class_eval do
|
8
|
+
attr_reader :client
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
# Initialize command.
|
13
|
+
#
|
14
|
+
# @param [BrickFTP::RESTfulAPI::Client] client RESTful API client.
|
15
|
+
#
|
16
|
+
def initialize(client)
|
17
|
+
@client = client
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,46 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'erb'
|
4
|
+
|
5
|
+
module BrickFTP
|
6
|
+
module RESTfulAPI
|
7
|
+
# Completing an upload
|
8
|
+
#
|
9
|
+
# @see https://developers.brickftp.com/#completing-an-upload Completing an upload
|
10
|
+
#
|
11
|
+
# ### Params
|
12
|
+
#
|
13
|
+
# PARAMETER | TYPE | DESCRIPTION
|
14
|
+
# --------- | ------- | -----------
|
15
|
+
# ref | string | Unique identifier to reference this file upload. This identifier is needed for subsequent requests to the REST API to complete the upload or request more upload URLs.
|
16
|
+
#
|
17
|
+
class CompleteUpload
|
18
|
+
include Command
|
19
|
+
using BrickFTP::CoreExt::Struct
|
20
|
+
using BrickFTP::CoreExt::Hash
|
21
|
+
|
22
|
+
Params = Struct.new(
|
23
|
+
'CompleteUploadParams',
|
24
|
+
:ref,
|
25
|
+
keyword_init: true
|
26
|
+
)
|
27
|
+
|
28
|
+
# After uploading the file to the file storage environment,
|
29
|
+
# the REST API needs to be notified that the upload was completed.
|
30
|
+
#
|
31
|
+
# This is done by sending another POST request to `/files/PATH_AND_FILENAME.EXT` with
|
32
|
+
# parameter `action` set to end and parameter `ref` set to the reference ID returned at the start of the upload.
|
33
|
+
#
|
34
|
+
# @param [String] path Full path of the file or folder. Maximum of 550 characters.
|
35
|
+
# @param [BrickFTP::RESTfulAPI::CompleteUpload::Params] params parameters
|
36
|
+
# @return [BrickFTP::Types::File] File object
|
37
|
+
# @raise [BrickFTP::RESTfulAPI::Error] exception
|
38
|
+
#
|
39
|
+
def call(path, params)
|
40
|
+
res = client.post("/api/rest/v1/files/#{ERB::Util.url_encode(path)}", params.to_h.compact.merge(action: 'end'))
|
41
|
+
|
42
|
+
BrickFTP::Types::File.new(res.symbolize_keys)
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|