squarecloud-unofficial 1.0.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: 0d8f21e29130213fd94e5300d77e9740f833b2f2f6e0dc07cc14fe9c8c4efa79
4
+ data.tar.gz: a6225e8763e621119e145b247babb0b0fac2bef624a0e7421c9d2d90d60944e4
5
+ SHA512:
6
+ metadata.gz: 4f237a9df7cad30cc37e33740646037036a97b7dfbb3b5020c2e6e43dd2aa0f2553ffe1d40ba2bf2197fb61ec6fb1e1df2d2c7759e90e6b4fa9c573fd5209af3
7
+ data.tar.gz: d4915b72e9df155421fe161237bb777ebb1e5c7714927c915fc05ec5e308fad5d297546ec297616215aa31300e3d3cea49ddcfe89889f997d511b947e9677188
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2022 Júlia Klee
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy of
6
+ this software and associated documentation files (the "Software"), to deal in
7
+ the Software without restriction, including without limitation the rights to
8
+ use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9
+ the Software, and to permit persons to whom the Software is furnished to do so,
10
+ subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ 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, FITNESS
17
+ FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18
+ COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19
+ IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20
+ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,169 @@
1
+ # Square Cloud Ruby SDK (Unofficial)
2
+
3
+ An unofficial Ruby SDK for the [Square Cloud](https://squarecloud.app/) API.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'squarecloud-unofficial'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ ```
16
+ $ bundle install
17
+ ```
18
+
19
+ Or install it yourself as:
20
+
21
+ ```
22
+ $ gem install squarecloud-unofficial
23
+ ```
24
+
25
+ ## Getting API Key
26
+
27
+ To get your API key/token, go to the [Square Cloud](https://squarecloud.app) website and register/login.
28
+ After that, navigate to `dashboard` > `my account` > `Regenerate API/CLI KEY` and copy the key.
29
+
30
+ ## Usage
31
+
32
+ ### Authentication
33
+
34
+ To use the SDK, you need to authenticate with your API key from the Square Cloud dashboard.
35
+
36
+ ```ruby
37
+ require 'squarecloud'
38
+
39
+ # Initialize the client with your API key
40
+ client = Squarecloud::Client.new(api_key: 'YOUR_API_KEY')
41
+ ```
42
+
43
+ ### User Information
44
+
45
+ ```ruby
46
+ # Get user information
47
+ user = client.user
48
+ puts "User: #{user.name}"
49
+ puts "Plan: #{user.plan.name}"
50
+ ```
51
+
52
+ ### Application Management
53
+
54
+ ```ruby
55
+ # List all applications
56
+ apps = client.all_apps
57
+ apps.each do |app|
58
+ puts "#{app.name} (#{app.id})"
59
+ end
60
+
61
+ # Get a specific application by ID
62
+ app = client.app('APP_ID')
63
+
64
+ # Get application status
65
+ status = app.status
66
+ puts "Running: #{status.running}"
67
+ puts "CPU: #{status.cpu}"
68
+ puts "RAM: #{status.ram}"
69
+
70
+ # Get application logs
71
+ logs = app.logs
72
+ puts logs.logs
73
+
74
+ # Start, stop, and restart an application
75
+ app.start
76
+ app.stop
77
+ app.restart
78
+
79
+ # Delete an application
80
+ app.delete
81
+ ```
82
+
83
+ ### File Operations
84
+
85
+ ```ruby
86
+ # List files in a directory
87
+ files = app.files_list('/')
88
+ files.each do |file|
89
+ puts "#{file.name} (#{file.type}, #{file.size} bytes)"
90
+ end
91
+
92
+ # Read a file's content
93
+ content = app.read_file('/path/to/file')
94
+
95
+ # Create a new file
96
+ new_file = Squarecloud::File.new('local_file.txt', 'text/plain')
97
+ app.create_file(new_file, '/path/in/app.txt')
98
+
99
+ # Move a file
100
+ app.move_file('/old/path.txt', '/new/path.txt')
101
+
102
+ # Delete a file
103
+ app.delete_file('/path/to/file.txt')
104
+ ```
105
+
106
+ ### Backups
107
+
108
+ ```ruby
109
+ # Create a backup
110
+ backup = app.backup
111
+ puts "Backup URL: #{backup.url}"
112
+
113
+ # List all backups
114
+ backups = app.all_backups
115
+ backups.each do |backup|
116
+ puts "#{backup.name} (#{backup.size} bytes)"
117
+ end
118
+
119
+ # Download a backup
120
+ backup.download('./backups/')
121
+ ```
122
+
123
+ ### Domain Management
124
+
125
+ ```ruby
126
+ # Set a custom domain
127
+ app.set_custom_domain('example.com')
128
+
129
+ # Get domain analytics
130
+ analytics = app.domain_analytics
131
+ visits = analytics.domain.analytics.total.first.visits
132
+ puts "Total visits: #{visits}"
133
+
134
+ # Get DNS records
135
+ records = app.dns_records
136
+ records.each do |record|
137
+ puts "#{record.type} #{record.name} #{record.value}"
138
+ end
139
+ ```
140
+
141
+ ### GitHub Integration
142
+
143
+ ```ruby
144
+ # Set up GitHub integration
145
+ app.create_github_integration('GITHUB_TOKEN')
146
+
147
+ # Get current integration
148
+ integration = app.current_integration
149
+ puts "Current integration: #{integration}"
150
+ ```
151
+
152
+ ## Examples
153
+
154
+ For more examples, check the `examples/` directory in this repository:
155
+
156
+ - `examples/client_example.rb` - Basic usage of the client
157
+ - `examples/file_operations.rb` - Examples of file operations
158
+
159
+ ## Disclaimer
160
+
161
+ This is an **unofficial** SDK not affiliated with or endorsed by Square Cloud. This project was created to provide Ruby developers with an easy way to interact with the Square Cloud API.
162
+
163
+ ## Contributing
164
+
165
+ Feel free to contribute with suggestions or bug reports at our GitHub repository.
166
+
167
+ ## License
168
+
169
+ This SDK is available as open source under the terms of the MIT License.
data/bin/console ADDED
@@ -0,0 +1,7 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "squarecloud"
5
+
6
+ require "irb"
7
+ IRB.start(__FILE__)
@@ -0,0 +1,146 @@
1
+ module Squarecloud
2
+ class File
3
+ attr_reader :path, :mime_type, :filename
4
+
5
+ def initialize(path, mime_type = 'application/zip')
6
+ @path = path
7
+ @mime_type = mime_type
8
+ @filename = ::File.basename(path)
9
+ end
10
+ end
11
+
12
+ class Application
13
+ attr_reader :id, :name, :cluster, :ram, :language, :domain, :custom, :desc
14
+
15
+ def initialize(http:, data:)
16
+ @http = http
17
+
18
+ if data.is_a?(Hash)
19
+ @id = data[:id]
20
+ @name = data[:name]
21
+ @cluster = data[:cluster]
22
+ @ram = data[:ram]
23
+ @language = data[:language]
24
+ @domain = data[:domain]
25
+ @custom = data[:custom]
26
+ @desc = data[:desc]
27
+ else
28
+
29
+
30
+ @id = nil
31
+ end
32
+ end
33
+
34
+ def status
35
+ response = @http.fetch_app_status(@id)
36
+ StatusData.new(response)
37
+ end
38
+
39
+ def logs
40
+ response = @http.fetch_logs(@id)
41
+ LogsData.new(response)
42
+ end
43
+
44
+ def start
45
+ @http.start_application(@id)
46
+ true
47
+ end
48
+
49
+ def stop
50
+ @http.stop_application(@id)
51
+ true
52
+ end
53
+
54
+ def restart
55
+ @http.restart_application(@id)
56
+ true
57
+ end
58
+
59
+ def backup
60
+ response = @http.backup(@id)
61
+ Backup.new(response)
62
+ end
63
+
64
+ def delete
65
+ @http.delete_application(@id)
66
+ true
67
+ end
68
+
69
+ def commit(file)
70
+ @http.commit(@id, file)
71
+ true
72
+ end
73
+
74
+ def files_list(path)
75
+ response = @http.fetch_app_files_list(@id, path)
76
+ response.map { |file_data| FileInfo.new(@id, file_data) }
77
+ end
78
+
79
+ def read_file(path)
80
+ @http.read_app_file(@id, path)
81
+ end
82
+
83
+ def create_file(file, path)
84
+ @http.create_app_file(@id, file, path)
85
+ true
86
+ end
87
+
88
+ def delete_file(path)
89
+ @http.file_delete(@id, path)
90
+ true
91
+ end
92
+
93
+ def move_file(origin, dest)
94
+ @http.move_app_file(@id, origin, dest)
95
+ true
96
+ end
97
+
98
+ def last_deploys
99
+ response = @http.get_last_deploys(@id)
100
+ response.map do |deploy_group|
101
+ deploy_group.map { |deploy| DeployData.new(deploy) }
102
+ end
103
+ end
104
+
105
+ def create_github_integration(access_token)
106
+ @http.create_github_integration(@id, access_token)
107
+ end
108
+
109
+ def current_integration
110
+ @http.get_app_current_integration(@id)
111
+ end
112
+
113
+ def domain_analytics
114
+ response = @http.domain_analytics(@id)
115
+ DomainAnalytics.new(response)
116
+ end
117
+
118
+ def set_custom_domain(custom_domain)
119
+ @http.update_custom_domain(@id, custom_domain)
120
+ true
121
+ end
122
+
123
+ def all_backups
124
+ response = @http.get_all_app_backups(@id)
125
+ response.map { |backup| BackupInfo.new(backup) }
126
+ end
127
+
128
+ def dns_records
129
+ response = @http.dns_records(@id)
130
+ response.map { |record| DNSRecord.new(record) }
131
+ end
132
+
133
+ def to_h
134
+ {
135
+ id: @id,
136
+ name: @name,
137
+ cluster: @cluster,
138
+ ram: @ram,
139
+ language: @language,
140
+ domain: @domain,
141
+ custom: @custom,
142
+ desc: @desc
143
+ }
144
+ end
145
+ end
146
+ end
@@ -0,0 +1,274 @@
1
+ module Squarecloud
2
+ module HTTP
3
+ class Response
4
+ attr_reader :data, :route, :status, :code, :message, :response
5
+
6
+ def initialize(data, route)
7
+ @data = data
8
+ @route = route
9
+ @status = data.is_a?(Hash) ? data[:status] : nil
10
+ @code = data.is_a?(Hash) ? data[:code] : nil
11
+ @message = data.is_a?(Hash) ? data[:message] : nil
12
+ @response = data.is_a?(Hash) ? data[:response] : data
13
+ end
14
+
15
+ def to_s
16
+ "
17
+ end
18
+ end
19
+
20
+ class Client
21
+ BASE_URL = 'https://api.squarecloud.app/v2'.freeze
22
+
23
+ attr_reader :api_key, :last_response
24
+
25
+ def initialize(api_key:)
26
+ @api_key = api_key
27
+ @last_response = nil
28
+ end
29
+
30
+ def request(endpoint, method: 'GET', params: {}, multipart: false, body: nil)
31
+ conn = Faraday.new(url: BASE_URL) do |f|
32
+ f.request :multipart if multipart
33
+ f.request :json
34
+ f.response :json, content_type: /\bjson$/
35
+ f.adapter Faraday.default_adapter
36
+ end
37
+
38
+ url = endpoint.build_path(params)
39
+
40
+ begin
41
+ response = conn.send(method.downcase) do |req|
42
+ req.url url
43
+ req.headers['Authorization'] = @api_key
44
+ req.headers['User-Agent'] = "squarecloud-ruby/
45
+ req.body = body if body
46
+ end
47
+
48
+ data = response.body
49
+ extra_error_kwargs = {}
50
+
51
+
52
+ if params[:custom_domain]
53
+ extra_error_kwargs[:domain] = params[:custom_domain]
54
+ end
55
+
56
+
57
+ case response.status
58
+ when 401
59
+ raise Squarecloud::AuthenticationFailure.new(
60
+ route: endpoint.name,
61
+ status_code: response.status,
62
+ code: data.is_a?(Hash) ? data[:code] : nil
63
+ )
64
+ when 404
65
+ raise Squarecloud::NotFoundError.new(
66
+ route: endpoint.name,
67
+ status_code: response.status,
68
+ code: data.is_a?(Hash) ? data[:code] : nil
69
+ )
70
+ when 400
71
+ error_code = data.is_a?(Hash) ? data[:code] : nil
72
+ error_class = get_error(error_code)
73
+ if error_class
74
+ raise error_class.new(
75
+ route: endpoint.name,
76
+ status_code: response.status,
77
+ code: error_code,
78
+ **extra_error_kwargs
79
+ )
80
+ else
81
+ raise Squarecloud::BadRequestError.new(
82
+ route: endpoint.name,
83
+ status_code: response.status,
84
+ code: error_code,
85
+ message: "Bad request:
86
+ )
87
+ end
88
+ when 429
89
+ raise Squarecloud::TooManyRequests.new(
90
+ route: endpoint.name,
91
+ status_code: response.status,
92
+ code: data.is_a?(Hash) ? data[:code] : nil
93
+ )
94
+ end
95
+
96
+
97
+ @last_response = Response.new(data, endpoint)
98
+
99
+ if @last_response.status == 'error'
100
+ error_class = get_error(@last_response.code)
101
+ if error_class
102
+ raise error_class.new(
103
+ route: endpoint.name,
104
+ status_code: response.status,
105
+ code: @last_response.code,
106
+ **extra_error_kwargs
107
+ )
108
+ else
109
+ raise Squarecloud::RequestError.new(
110
+ route: endpoint.name,
111
+ status_code: response.status,
112
+ code: @last_response.code,
113
+ message: "Request error:
114
+ )
115
+ end
116
+ end
117
+
118
+ @last_response.response
119
+ rescue Faraday::Error => e
120
+
121
+ raise Squarecloud::RequestError.new(
122
+ route: endpoint.name,
123
+ message: "Network error:
124
+ )
125
+ end
126
+ end
127
+
128
+ def get_error(code)
129
+ errors = {
130
+ 'FEW_MEMORY' => Squarecloud::FewMemory,
131
+ 'BAD_MEMORY' => Squarecloud::BadMemory,
132
+ 'MISSING_CONFIG' => Squarecloud::MissingConfigFile,
133
+ 'MISSING_DEPENDENCIES_FILE' => Squarecloud::MissingDependenciesFile,
134
+ 'MISSING_MAIN' => Squarecloud::MissingMainFile,
135
+ 'INVALID_MAIN' => Squarecloud::InvalidMain,
136
+ 'INVALID_DISPLAY_NAME' => Squarecloud::InvalidDisplayName,
137
+ 'MISSING_DISPLAY_NAME' => Squarecloud::MissingDisplayName,
138
+ 'INVALID_MEMORY' => Squarecloud::InvalidMemory,
139
+ 'MISSING_MEMORY' => Squarecloud::MissingMemory,
140
+ 'INVALID_VERSION' => Squarecloud::InvalidVersion,
141
+ 'MISSING_VERSION' => Squarecloud::MissingVersion,
142
+ 'INVALID_ACCESS_TOKEN' => Squarecloud::InvalidAccessToken,
143
+ 'REGEX_VALIDATION' => Squarecloud::InvalidDomain,
144
+ 'INVALID_START' => Squarecloud::InvalidStart
145
+ }
146
+
147
+ errors[code]
148
+ end
149
+
150
+ def fetch_user_info
151
+ endpoint = Squarecloud::HTTP::Endpoint.user
152
+ request(endpoint)
153
+ end
154
+
155
+ def fetch_app_status(app_id)
156
+ endpoint = Squarecloud::HTTP::Endpoint.app_status
157
+ request(endpoint, params: { app_id: app_id })
158
+ end
159
+
160
+ def fetch_logs(app_id)
161
+ endpoint = Squarecloud::HTTP::Endpoint.logs
162
+ request(endpoint, params: { app_id: app_id })
163
+ end
164
+
165
+ def start_application(app_id)
166
+ endpoint = Squarecloud::HTTP::Endpoint.start
167
+ request(endpoint, method: 'POST', params: { app_id: app_id })
168
+ end
169
+
170
+ def stop_application(app_id)
171
+ endpoint = Squarecloud::HTTP::Endpoint.stop
172
+ request(endpoint, method: 'POST', params: { app_id: app_id })
173
+ end
174
+
175
+ def restart_application(app_id)
176
+ endpoint = Squarecloud::HTTP::Endpoint.restart
177
+ request(endpoint, method: 'POST', params: { app_id: app_id })
178
+ end
179
+
180
+ def backup(app_id)
181
+ endpoint = Squarecloud::HTTP::Endpoint.backup
182
+ request(endpoint, method: 'POST', params: { app_id: app_id })
183
+ end
184
+
185
+ def delete_application(app_id)
186
+ endpoint = Squarecloud::HTTP::Endpoint.delete_app
187
+ request(endpoint, method: 'DELETE', params: { app_id: app_id })
188
+ end
189
+
190
+ def commit(app_id, file)
191
+ endpoint = Squarecloud::HTTP::Endpoint.commit
192
+ form_data = { file: Faraday::Multipart::FilePart.new(file.path, file.mime_type) }
193
+ request(endpoint, method: 'POST', params: { app_id: app_id }, multipart: true, body: form_data)
194
+ end
195
+
196
+ def upload(file)
197
+ endpoint = Squarecloud::HTTP::Endpoint.upload
198
+ form_data = { file: Faraday::Multipart::FilePart.new(file.path, file.mime_type) }
199
+ request(endpoint, method: 'POST', multipart: true, body: form_data)
200
+ end
201
+
202
+ def fetch_app_files_list(app_id, path)
203
+ endpoint = Squarecloud::HTTP::Endpoint.files_list
204
+ request(endpoint, params: { app_id: app_id, path: path })
205
+ end
206
+
207
+ def read_app_file(app_id, path)
208
+ endpoint = Squarecloud::HTTP::Endpoint.files_read
209
+ request(endpoint, params: { app_id: app_id, path: path })
210
+ end
211
+
212
+ def create_app_file(app_id, file, path)
213
+ endpoint = Squarecloud::HTTP::Endpoint.files_create
214
+ form_data = { file: Faraday::Multipart::FilePart.new(file.path, file.mime_type), path: path }
215
+ request(endpoint, method: 'PUT', params: { app_id: app_id }, multipart: true, body: form_data)
216
+ end
217
+
218
+ def file_delete(app_id, path)
219
+ endpoint = Squarecloud::HTTP::Endpoint.files_delete
220
+ request(endpoint, method: 'DELETE', params: { app_id: app_id, path: path })
221
+ end
222
+
223
+ def get_app_data(app_id)
224
+ endpoint = Squarecloud::HTTP::Endpoint.app_data
225
+ request(endpoint, params: { app_id: app_id })
226
+ end
227
+
228
+ def get_last_deploys(app_id)
229
+ endpoint = Squarecloud::HTTP::Endpoint.last_deploys
230
+ request(endpoint, params: { app_id: app_id })
231
+ end
232
+
233
+ def create_github_integration(app_id, github_access_token)
234
+ endpoint = Squarecloud::HTTP::Endpoint.github_integration
235
+ request(endpoint, method: 'POST', params: { app_id: app_id }, body: { access_token: github_access_token })
236
+ end
237
+
238
+ def update_custom_domain(app_id, custom_domain)
239
+ endpoint = Squarecloud::HTTP::Endpoint.custom_domain
240
+ request(endpoint, method: 'POST', params: { app_id: app_id, custom_domain: custom_domain }, body: { custom_domain: custom_domain })
241
+ end
242
+
243
+ def domain_analytics(app_id)
244
+ endpoint = Squarecloud::HTTP::Endpoint.domain_analytics
245
+ request(endpoint, params: { app_id: app_id })
246
+ end
247
+
248
+ def get_all_app_backups(app_id)
249
+ endpoint = Squarecloud::HTTP::Endpoint.all_backups
250
+ request(endpoint, params: { app_id: app_id })
251
+ end
252
+
253
+ def all_apps_status
254
+ endpoint = Squarecloud::HTTP::Endpoint.all_apps_status
255
+ request(endpoint)
256
+ end
257
+
258
+ def move_app_file(app_id, origin, dest)
259
+ endpoint = Squarecloud::HTTP::Endpoint.move_file
260
+ request(endpoint, method: 'PATCH', params: { app_id: app_id }, body: { origin: origin, dest: dest })
261
+ end
262
+
263
+ def dns_records(app_id)
264
+ endpoint = Squarecloud::HTTP::Endpoint.dns_records
265
+ request(endpoint, params: { app_id: app_id })
266
+ end
267
+
268
+ def get_app_current_integration(app_id)
269
+ endpoint = Squarecloud::HTTP::Endpoint.current_integration
270
+ request(endpoint, params: { app_id: app_id })
271
+ end
272
+ end
273
+ end
274
+ end