minisky 0.5.0 → 0.5.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 +4 -4
- data/CHANGELOG.md +41 -15
- data/LICENSE.txt +1 -1
- data/README.md +22 -15
- data/SECURITY.md +16 -0
- data/lib/minisky/compat.rb +22 -0
- data/lib/minisky/errors.rb +76 -4
- data/lib/minisky/minisky.rb +52 -11
- data/lib/minisky/requests.rb +360 -41
- data/lib/minisky/version.rb +3 -1
- metadata +12 -17
- data/example/ask_password.rb +0 -76
- data/example/fetch_my_posts.rb +0 -46
- data/example/fetch_profile.rb +0 -53
- data/example/find_missing_follows.rb +0 -67
- data/example/post_skeet.rb +0 -36
- data/example/science_feed.rb +0 -53
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 412a75311a1f85346a0085428cfafb40ed865b9fa167c05afec75988758938da
|
|
4
|
+
data.tar.gz: 8a8e0bb0b328281605f8c17a2a0cfe9b6c3d08a66a4d952e4114bd2219574af2
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: c85aef6fcc92c12ce3293ccf59bdf1bdcb3dee31aa74d9e0ea4186dd0bcc878840f24bf7258027daf833bb6e5a51d8934763487182d1212ad8b128d182dee79f
|
|
7
|
+
data.tar.gz: 3b161229e28fe3ea1594fe2101d134addb78f5050930f869fa06fa790f2a25c6b53808777cb59a201499fdea9ee418627e51f6a0632987ac4f50ff93425d0299
|
data/CHANGELOG.md
CHANGED
|
@@ -1,8 +1,34 @@
|
|
|
1
|
-
## [0.5.
|
|
1
|
+
## [0.5.2] – 2026-11-09
|
|
2
|
+
|
|
3
|
+
* added `:timeout` option which is passed to `:open_timeout` & `:read_timeout` on requests
|
|
4
|
+
* improved error handling for JSON responses, JWT payloads, or wrong argument values
|
|
5
|
+
* fixed access token JWT parsing which could fail sometimes due to wrong version of Base64 being expected
|
|
6
|
+
* detect repeated cursors during `fetch_all` pagination, which could lead to an infinite loop when fetching from a misbehaving server
|
|
7
|
+
* changed `fetch_all` to not modify the received `params` object in place
|
|
8
|
+
* added `frozen_string_literal` comments
|
|
9
|
+
|
|
10
|
+
## [0.5.1] – 2026-02-15
|
|
11
|
+
|
|
12
|
+
The "really niche bugfix" edition:
|
|
13
|
+
|
|
14
|
+
* don't stop fetching in `fetch_all` if an empty page is returned but the cursor is not nil; it's technically allowed for the server to return an empty page but still have more data to send, e.g. if the service does some filtering at the last step and all records happen to be filtered out
|
|
15
|
+
- unfortunately, this on the other hand causes problems with some specific endpoints which incorrectly return a cursor when reaching the end of data, so you can also restore the previous behavior if needed, by setting the `stop_fetch_on_empty_page` option
|
|
16
|
+
* in `post_request`, don't set Content-Type to "application/json" if the data sent is a string or nil (it might cause an error in some cases, like when uploading some binary content)
|
|
17
|
+
* handle the (somewhat theoretical but possible) case where an access token is not a JWT but just some opaque blob – in that case, Minisky will now not throw an error trying to parse it, but just treat it as "unknown" and will not try to refresh it
|
|
18
|
+
- note: at the moment Minisky will not catch the "token expired" error and refresh the token automatically in such scenario
|
|
19
|
+
* allow connecting to non-HTTPS servers (e.g. `http://localhost:3000`)
|
|
20
|
+
* allow making unauthenticated clients with custom classes by returning `nil` from `#config`; custom clients with a config that's missing an `id` or `pass` are treated as an error
|
|
21
|
+
* deprecate logging in using an email address in the `id` field – `createSession` accepts such identifier, but unlike with handle or DID, there's no way to use it to look up the DID document and PDS location if we wanted to
|
|
22
|
+
* fixed URL query params in POST requests on Ruby 2.x
|
|
23
|
+
* marked `Minisky#active_repl?` method as private
|
|
24
|
+
|
|
25
|
+
Also added [YARD API documentation](https://rubydoc.info/gems/minisky/) for most of the code.
|
|
26
|
+
|
|
27
|
+
## [0.5.0] – 2024-12-27 🎄
|
|
2
28
|
|
|
3
29
|
* `host` param in the initializer can be passed with a `https://` prefix (useful if you're passing it directly from a DID document, e.g. using DIDKit)
|
|
4
30
|
* added validation of the `method` parameter in request calls: it needs to be either a proper NSID, or a full URL as a string or a URI object
|
|
5
|
-
* added new optional `params` keyword argument in `post_request`, which lets you append query parameters to the URL if a POST endpoint requires passing them this way
|
|
31
|
+
* added new optional `params` keyword argument in `post_request`, which lets you append query parameters to the URL if a POST endpoint requires passing them this way (e.g. `uploadVideo`)
|
|
6
32
|
* `default_progress` is set by default to show progress using dots (`.`) if Minisky is loaded inside an IRB or Pry context
|
|
7
33
|
* when experimenting with Minisky in the console, you can now skip the `field:` parameter to `fetch_all` if you don't remember the expected key name in the response, and the method will make a request and return an error which tells you the list of available keys
|
|
8
34
|
* added `access_token_expired?` helper method
|
|
@@ -10,33 +36,33 @@
|
|
|
10
36
|
* `check_access` now returns a result symbol: `:logged_in`, `:refreshed` or `:ok`
|
|
11
37
|
* fixed `method_missing` setter on `User`
|
|
12
38
|
|
|
13
|
-
## [0.4.0]
|
|
39
|
+
## [0.4.0] – 2024-03-31 🐣
|
|
14
40
|
|
|
15
41
|
* allow passing non-JSON body to requests (e.g. when uploading blobs)
|
|
16
42
|
* allow passing custom headers to requests, including overriding `Content-Type`
|
|
17
43
|
* fixed error when the response is success but not JSON (e.g. an empty body like in deleteRecord)
|
|
18
44
|
* allow passing options to the client in the initializer
|
|
19
45
|
* aliased `default_progress` setting as `progress`
|
|
20
|
-
* added `base64` dependency explicitly to the gemspec
|
|
46
|
+
* added `base64` dependency explicitly to the gemspec – fixes a warning in Ruby 3.3, since it will be extracted as an optional gem in 3.4
|
|
21
47
|
|
|
22
|
-
## [0.3.1]
|
|
48
|
+
## [0.3.1] – 2023-10-10
|
|
23
49
|
|
|
24
50
|
* fixed Minisky not working on Ruby 2.x
|
|
25
51
|
|
|
26
|
-
## [0.3.0]
|
|
52
|
+
## [0.3.0] – 2023-10-05
|
|
27
53
|
|
|
28
54
|
* authentication improvements & changes:
|
|
29
55
|
- Minisky now automatically manages access tokens, calling `check_access` manually is not necessary (set `auto_manage_tokens` to `false` to disable this)
|
|
30
56
|
- `check_access` now just checks token's expiry time instead of making a request to `getSession`
|
|
31
|
-
- added `send_auth_headers` option
|
|
32
|
-
- removed default config file name
|
|
33
|
-
- Minisky can now be used in unauthenticated mode
|
|
57
|
+
- added `send_auth_headers` option – set to `false` to not set auth header automatically, which is the default
|
|
58
|
+
- removed default config file name – explicit file name is now required
|
|
59
|
+
- Minisky can now be used in unauthenticated mode – pass `nil` as the config file name
|
|
34
60
|
- added `reset_tokens` helper method
|
|
35
|
-
* refactored response handling
|
|
61
|
+
* refactored response handling – typed errors are now raised on non-success response status
|
|
36
62
|
* `user` wrapper can also be used for writing fields to the config
|
|
37
63
|
* improved error handling
|
|
38
64
|
|
|
39
|
-
## [0.2.0]
|
|
65
|
+
## [0.2.0] – 2023-09-02
|
|
40
66
|
|
|
41
67
|
* more consistent handling of parameters in the main methods:
|
|
42
68
|
- `auth` is now a named parameter
|
|
@@ -51,8 +77,8 @@
|
|
|
51
77
|
* renamed `ident` field in the config hash to `id`
|
|
52
78
|
* config is now accessed in `Requests` from the client object as a `config` property instead of `@config` ivar
|
|
53
79
|
* config fields are exposed as a `user` wrapper object, e.g. `user.did` delegates to `@config['did']`
|
|
54
|
-
|
|
55
|
-
## [0.1.0]
|
|
80
|
+
|
|
81
|
+
## [0.1.0] – 2023-09-01
|
|
56
82
|
|
|
57
83
|
- extracted most code to a `Requests` module that can be included into a different client class with custom config handling
|
|
58
84
|
- added `#check_access` method
|
|
@@ -60,9 +86,9 @@
|
|
|
60
86
|
- config file name can be passed as a parameter
|
|
61
87
|
- added tests
|
|
62
88
|
|
|
63
|
-
## [0.0.1]
|
|
89
|
+
## [0.0.1] – 2023-08-30
|
|
64
90
|
|
|
65
|
-
Initial release
|
|
91
|
+
Initial release – extracted from original gist:
|
|
66
92
|
|
|
67
93
|
- logging in and refreshing the token
|
|
68
94
|
- making GET & POST requests
|
data/LICENSE.txt
CHANGED
data/README.md
CHANGED
|
@@ -1,22 +1,24 @@
|
|
|
1
1
|
# Minisky 🌤
|
|
2
2
|
|
|
3
|
+
[](https://rubygems.org/gems/minisky) [](https://rubydoc.info/gems/minisky)
|
|
4
|
+
|
|
3
5
|
Minisky is a minimal client of the Bluesky (ATProto) API. It provides a simple API client class that you can use to log in to the Bluesky API and make any GET and POST requests there. It's meant to be an easy way to start playing and experimenting with the AT Protocol API.
|
|
4
6
|
|
|
5
|
-
This is designed as a low-level XRPC client library
|
|
7
|
+
This is designed as a low-level XRPC client library – it purposefully does not include any convenience methods like "get posts" or "get profile" etc., it only provides base components that you could use to build a higher level API.
|
|
6
8
|
|
|
7
9
|
> [!NOTE]
|
|
8
|
-
> ATProto Ruby
|
|
10
|
+
> Part of ATProto Ruby SDK: [ruby.sdk.blue](https://ruby.sdk.blue)
|
|
9
11
|
|
|
10
12
|
|
|
11
13
|
## Installation
|
|
12
14
|
|
|
13
|
-
To use Minisky, you need a reasonably new version of Ruby
|
|
15
|
+
To use Minisky, you need a reasonably new version of Ruby – it should run on Ruby 2.6 and above, although it's recommended to use a version that's still getting maintainance updates, i.e. currently 3.3+. A compatible version should be preinstalled on macOS Big Sur and above and on many Linux systems. Otherwise, you can install one using tools such as [RVM](https://rvm.io), [asdf](https://asdf-vm.com), [ruby-install](https://github.com/postmodern/ruby-install) or [ruby-build](https://github.com/rbenv/ruby-build), or `rpm` or `apt-get` on Linux (see more installation options on [ruby-lang.org](https://www.ruby-lang.org/en/downloads/)).
|
|
14
16
|
|
|
15
17
|
To install the Minisky gem, run the command:
|
|
16
18
|
|
|
17
19
|
[sudo] gem install minisky
|
|
18
20
|
|
|
19
|
-
Or
|
|
21
|
+
Or add it to your app's `Gemfile`:
|
|
20
22
|
|
|
21
23
|
gem 'minisky', '~> 0.5'
|
|
22
24
|
|
|
@@ -47,7 +49,7 @@ bsky = Minisky.new('api.bsky.app', nil)
|
|
|
47
49
|
> [!NOTE]
|
|
48
50
|
> To call PDS endpoints like `getRecord` or `listRecords`, you need to connect to the PDS of the user whose data you're loading, not to yours (unless it's the same one). Alternatively, you can use the `bsky.social` "entryway" PDS hostname for any Bluesky-hosted accounts, but this will not work for self-hosted accounts.
|
|
49
51
|
>
|
|
50
|
-
> To look up the PDS hostname of a user given their handle or DID, you can use the [didkit](https://
|
|
52
|
+
> To look up the PDS hostname of a user given their handle or DID, you can use the [didkit](https://tangled.org/mackuba.eu/didkit) library.
|
|
51
53
|
>
|
|
52
54
|
> For the AppView, `api.bsky.app` connects directly to Bluesky's AppView, and `public.api.bsky.app` to a version with extra caching that will usually be faster.
|
|
53
55
|
|
|
@@ -66,7 +68,7 @@ pass: very-secret-password
|
|
|
66
68
|
The `id` can be either your handle, or your DID, or the email you've used to sign up. It's recommended that you use the "app password" that you can create in the settings instead of your main account password.
|
|
67
69
|
|
|
68
70
|
> [!NOTE]
|
|
69
|
-
> Bluesky has recently implemented OAuth, but Minisky doesn't support it yet
|
|
71
|
+
> Bluesky has recently implemented OAuth, but Minisky doesn't support it yet – it will be added in a future version. App passwords should still be supported for a fairly long time.
|
|
70
72
|
|
|
71
73
|
After you log in, this file will also be used to store your access & request tokens and DID. The data in the config file can be accessed through a `user` wrapper property that exposes them as methods, e.g. the password is available as `user.pass` and the DID as `user.did`.
|
|
72
74
|
|
|
@@ -78,7 +80,7 @@ require 'minisky'
|
|
|
78
80
|
bsky = Minisky.new('bsky.social', 'bluesky.yml')
|
|
79
81
|
```
|
|
80
82
|
|
|
81
|
-
Minisky automatically manages your access and refresh tokens
|
|
83
|
+
Minisky automatically manages your access and refresh tokens – it will first log you in using the login & password, and then use the refresh token to update the access token before the request when it expires.
|
|
82
84
|
|
|
83
85
|
|
|
84
86
|
### Making requests
|
|
@@ -135,20 +137,18 @@ This will output a line like this:
|
|
|
135
137
|
.................
|
|
136
138
|
```
|
|
137
139
|
|
|
138
|
-
You can find more examples in the [example](https://github.com/mackuba/minisky/tree/master/example) directory.
|
|
139
|
-
|
|
140
140
|
|
|
141
141
|
## Customization
|
|
142
142
|
|
|
143
143
|
The `Minisky` client currently supports such configuration options:
|
|
144
144
|
|
|
145
|
-
- `default_progress`
|
|
146
|
-
- `send_auth_headers`
|
|
147
|
-
- `auto_manage_tokens`
|
|
145
|
+
- `default_progress` – a progress character to automatically use for `#fetch_all` calls (default: `.` when in an interactive console, `nil` otherwise)
|
|
146
|
+
- `send_auth_headers` – whether auth headers should be added by default (default: `true` in authenticated mode)
|
|
147
|
+
- `auto_manage_tokens` – whether access tokens should be generated and refreshed automatically when needed (default: `true` in authenticated mode)
|
|
148
148
|
|
|
149
149
|
In authenticated mode, you can disable the `send_auth_headers` option and then explicitly add `auth: true` to specific requests to include a header there.
|
|
150
150
|
|
|
151
|
-
You can also disable the `auto_manage_tokens` option
|
|
151
|
+
You can also disable the `auto_manage_tokens` option – in this case you will need to call the `#check_access` method before a request to refresh a token if needed, or alternatively, call either `#login` or `#perform_token_refresh`.
|
|
152
152
|
|
|
153
153
|
|
|
154
154
|
### Using your own class
|
|
@@ -186,13 +186,20 @@ bsky.get_request(...)
|
|
|
186
186
|
The class needs to provide:
|
|
187
187
|
|
|
188
188
|
- a `host` method or property that returns the hostname of the server
|
|
189
|
-
- a `config` property which returns a hash or a hash-like object with the configuration and user data
|
|
189
|
+
- a `config` property which returns a hash or a hash-like object with the configuration and user data – it needs to support reading and writing arbitrary key-value pairs with string keys
|
|
190
190
|
- a `save_config` method which persists the config object to the chosen storage
|
|
191
191
|
|
|
192
192
|
|
|
193
|
+
## Other resources
|
|
194
|
+
|
|
195
|
+
- [YARD API documentation](https://rubydoc.info/gems/minisky) at rubydoc.info
|
|
196
|
+
- [ruby.sdk.blue](https://ruby.sdk.blue)
|
|
197
|
+
- [Example scripts](https://ruby.sdk.blue/examples/)
|
|
198
|
+
|
|
199
|
+
|
|
193
200
|
## Credits
|
|
194
201
|
|
|
195
|
-
Copyright ©
|
|
202
|
+
Copyright © 2026 Kuba Suder ([@mackuba.eu](https://bsky.app/profile/did:plc:oio4hkxaop4ao4wz2pp3f4cr)).
|
|
196
203
|
|
|
197
204
|
The code is available under the terms of the [zlib license](https://choosealicense.com/licenses/zlib/) (permissive, similar to MIT).
|
|
198
205
|
|
data/SECURITY.md
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
# Security Policy
|
|
2
|
+
|
|
3
|
+
## How To Report a Vulnerability
|
|
4
|
+
|
|
5
|
+
If you find a potential vulnerability in one of my projects, it's recommended not to report it through a standard publicly-readable issue report, in order to avoid giving possible clues to any potential attackers who could abuse it before it's fixed (see: ["Just a rumour of a bug is enough to find a security exploit these days"](https://anil.recoil.org/notes/rumour-is-the-exploit)).
|
|
6
|
+
|
|
7
|
+
You can use one of these to contact me instead:
|
|
8
|
+
|
|
9
|
+
- [@mackuba.eu](https://bsky.app/profile/did:plc:oio4hkxaop4ao4wz2pp3f4cr) on Bluesky (my DMs should be open, or mention me discretely with a request for contact)
|
|
10
|
+
- [@mackuba@martianbase.net](https://martianbase.net/@mackuba) on Mastodon (my personal instance)
|
|
11
|
+
- email: mackuba `@` protonmail.ch (optionally with [this PGP key](https://mail-api.proton.me/pks/lookup?op=get&search=mackuba@protonmail.ch))
|
|
12
|
+
- ["Report a vulnerability"](https://github.com/mackuba/minisky/security/advisories/new) form on GitHub
|
|
13
|
+
|
|
14
|
+
I do not check my Twitter/X or LinkedIn accounts regularly.
|
|
15
|
+
|
|
16
|
+
Note: please don't submit AI-reported issues that have not been verified manually :)
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative 'minisky'
|
|
4
|
+
|
|
5
|
+
class Minisky
|
|
6
|
+
|
|
7
|
+
#
|
|
8
|
+
# Versions of {Requests#get_request} & {Requests#post_request} that work on Ruby 2.x.
|
|
9
|
+
#
|
|
10
|
+
|
|
11
|
+
module Ruby2Compat
|
|
12
|
+
def get_request(method, params = nil, auth: default_auth_mode, headers: nil, **kwargs)
|
|
13
|
+
params ||= kwargs unless kwargs.empty?
|
|
14
|
+
super(method, params, auth: auth, headers: headers)
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
def post_request(method, data = nil, auth: default_auth_mode, headers: nil, params: nil, **kwargs)
|
|
18
|
+
data ||= kwargs unless kwargs.empty?
|
|
19
|
+
super(method, data, auth: auth, headers: headers, params: params)
|
|
20
|
+
end
|
|
21
|
+
end
|
|
22
|
+
end
|
data/lib/minisky/errors.rb
CHANGED
|
@@ -1,18 +1,36 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
1
3
|
require_relative 'minisky'
|
|
2
4
|
|
|
3
5
|
class Minisky
|
|
6
|
+
|
|
7
|
+
#
|
|
8
|
+
# Common base error class for Minisky errors.
|
|
9
|
+
#
|
|
4
10
|
class Error < StandardError
|
|
5
11
|
end
|
|
6
12
|
|
|
13
|
+
#
|
|
14
|
+
# Raised when a required token or credentials are missing or invalid.
|
|
15
|
+
#
|
|
7
16
|
class AuthError < Error
|
|
8
|
-
def initialize(message)
|
|
9
|
-
super(message)
|
|
10
|
-
end
|
|
11
17
|
end
|
|
12
18
|
|
|
19
|
+
#
|
|
20
|
+
# Raised when the API returns an error status code.
|
|
21
|
+
#
|
|
13
22
|
class BadResponse < Error
|
|
14
|
-
attr_reader :status, :data
|
|
15
23
|
|
|
24
|
+
# @return [Integer] HTTP status code
|
|
25
|
+
attr_reader :status
|
|
26
|
+
|
|
27
|
+
# @return [String, Hash] response data (JSON hash or string)
|
|
28
|
+
attr_reader :data
|
|
29
|
+
|
|
30
|
+
# @param status [Integer] HTTP status code
|
|
31
|
+
# @param status_message [String] HTTP status message
|
|
32
|
+
# @param data [Hash, String] response data (JSON hash or string)
|
|
33
|
+
#
|
|
16
34
|
def initialize(status, status_message, data)
|
|
17
35
|
@status = status
|
|
18
36
|
@data = data
|
|
@@ -26,39 +44,93 @@ class Minisky
|
|
|
26
44
|
super(message)
|
|
27
45
|
end
|
|
28
46
|
|
|
47
|
+
# @return [String, nil] machine-readable error code from the response data
|
|
29
48
|
def error_type
|
|
30
49
|
@data['error'] if @data.is_a?(Hash)
|
|
31
50
|
end
|
|
32
51
|
|
|
52
|
+
# @return [String, nil] human-readable error message from the response data
|
|
33
53
|
def error_message
|
|
34
54
|
@data['message'] if @data.is_a?(Hash)
|
|
35
55
|
end
|
|
36
56
|
end
|
|
37
57
|
|
|
58
|
+
#
|
|
59
|
+
# Raised when the API returns a client error status code (4xx).
|
|
60
|
+
#
|
|
38
61
|
class ClientErrorResponse < BadResponse
|
|
39
62
|
end
|
|
40
63
|
|
|
64
|
+
#
|
|
65
|
+
# Raised when the API returns a server error status code (5xx).
|
|
66
|
+
#
|
|
41
67
|
class ServerErrorResponse < BadResponse
|
|
42
68
|
end
|
|
43
69
|
|
|
70
|
+
#
|
|
71
|
+
# Raised when the API returns an error indicating that the access or request
|
|
72
|
+
# token that was passed in the header is expired.
|
|
73
|
+
#
|
|
44
74
|
class ExpiredTokenError < ClientErrorResponse
|
|
45
75
|
end
|
|
46
76
|
|
|
77
|
+
#
|
|
78
|
+
# Raised when the API returns a redirect status code (3xx). Minisky doesn't
|
|
79
|
+
# currently follow any redirects.
|
|
80
|
+
#
|
|
47
81
|
class UnexpectedRedirect < BadResponse
|
|
82
|
+
|
|
83
|
+
# @return [String] value of the "Location" header
|
|
48
84
|
attr_reader :location
|
|
49
85
|
|
|
86
|
+
# @param status [Integer] HTTP status code
|
|
87
|
+
# @param status_message [String] HTTP status message
|
|
88
|
+
# @param location [String] value of the "Location" header
|
|
89
|
+
#
|
|
50
90
|
def initialize(status, status_message, location)
|
|
51
91
|
super(status, status_message, { 'message' => "Unexpected redirect: #{location}" })
|
|
52
92
|
@location = location
|
|
53
93
|
end
|
|
54
94
|
end
|
|
55
95
|
|
|
96
|
+
#
|
|
97
|
+
# Raised by {Requests#fetch_all} when the `field` parameter isn't set.
|
|
98
|
+
#
|
|
99
|
+
# The message of the exception lists the fields available in the first fetched page.
|
|
100
|
+
#
|
|
101
|
+
# @example Making a request in the console with empty `field`
|
|
102
|
+
# sky = Minisky.new('public.api.bsky.app', nil)
|
|
103
|
+
# # => #<Minisky:0x0000000120f5f6b0 @host="public.api.bsky.app", ...>
|
|
104
|
+
#
|
|
105
|
+
# sky.fetch_all('app.bsky.graph.getFollowers', { actor: 'sdk.blue' })
|
|
106
|
+
# # ./lib/minisky/requests.rb:270:in 'block in Minisky::Requests#fetch_all':
|
|
107
|
+
# # Field parameter not provided; available fields: ["followers"] (Minisky::FieldNotSetError)
|
|
108
|
+
#
|
|
109
|
+
# sky.fetch_all('app.bsky.graph.getFollowers', { actor: 'sdk.blue' }, field: 'followers')
|
|
110
|
+
# # => .....
|
|
111
|
+
#
|
|
56
112
|
class FieldNotSetError < Error
|
|
113
|
+
|
|
114
|
+
# @return [Array<String>] list of fields in the response data
|
|
57
115
|
attr_reader :fields
|
|
58
116
|
|
|
117
|
+
# @param fields [Array<String>] list of fields in the response data
|
|
118
|
+
#
|
|
59
119
|
def initialize(fields)
|
|
60
120
|
@fields = fields
|
|
61
121
|
super("Field parameter not provided; available fields: #{@fields.inspect}")
|
|
62
122
|
end
|
|
63
123
|
end
|
|
124
|
+
|
|
125
|
+
#
|
|
126
|
+
# Raised when the received response includes an invalid or unexpected cursor.
|
|
127
|
+
#
|
|
128
|
+
class InvalidCursorError < Error
|
|
129
|
+
end
|
|
130
|
+
|
|
131
|
+
#
|
|
132
|
+
# Raised when the received response JSON has some missing field or invalid format.
|
|
133
|
+
#
|
|
134
|
+
class InvalidJSONError < Error
|
|
135
|
+
end
|
|
64
136
|
end
|
data/lib/minisky/minisky.rb
CHANGED
|
@@ -1,8 +1,49 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
1
3
|
require 'yaml'
|
|
2
4
|
|
|
5
|
+
#
|
|
6
|
+
# The default API client class for making requests to AT Protocol servers. Can be used
|
|
7
|
+
# with authentication – with the credentials stored in a YAML file – or without it, for
|
|
8
|
+
# unauthenticated requests only (by passing `nil` as the config file name).
|
|
9
|
+
#
|
|
10
|
+
# @example Authenticated client
|
|
11
|
+
# # Expects a config.yml file like:
|
|
12
|
+
# #
|
|
13
|
+
# # id: test.example.com
|
|
14
|
+
# # pass: secret7
|
|
15
|
+
# #
|
|
16
|
+
# # "id" can be a handle or a DID.
|
|
17
|
+
#
|
|
18
|
+
# sky = Minisky.new('eurosky.social', 'config.yml')
|
|
19
|
+
#
|
|
20
|
+
# feed = sky.get_request('app.bsky.feed.getTimeline', { limit: 100 })
|
|
21
|
+
#
|
|
22
|
+
# @example Unauthenticated client
|
|
23
|
+
# sky = Minisky.new('public.api.bsky.app', nil, progress: '*')
|
|
24
|
+
#
|
|
25
|
+
# follows = sky.get_request('app.bsky.graph.getFollows',
|
|
26
|
+
# { actor: 'atproto.com', limit: 100 },
|
|
27
|
+
# field: 'follows'
|
|
28
|
+
# )
|
|
29
|
+
#
|
|
30
|
+
|
|
3
31
|
class Minisky
|
|
4
|
-
attr_reader :host, :config
|
|
5
32
|
|
|
33
|
+
# @return [String] the hostname (or base URL) of the server
|
|
34
|
+
attr_reader :host
|
|
35
|
+
|
|
36
|
+
# @return [Hash] loaded contents of the config file
|
|
37
|
+
attr_reader :config
|
|
38
|
+
|
|
39
|
+
# Creates a new client instance.
|
|
40
|
+
#
|
|
41
|
+
# @param host [String] the hostname (or base URL) of the server
|
|
42
|
+
# @param config_file [String, nil] path to the YAML config file, or `nil` for unauthenticated client
|
|
43
|
+
# @param options [Hash] option properties to set on the new instance (see {Minisky::Requests} properties)
|
|
44
|
+
#
|
|
45
|
+
# @raise [AuthError] if the config file is missing an ID or password
|
|
46
|
+
#
|
|
6
47
|
def initialize(host, config_file, options = {})
|
|
7
48
|
@host = host
|
|
8
49
|
@config_file = config_file
|
|
@@ -10,13 +51,10 @@ class Minisky
|
|
|
10
51
|
if @config_file
|
|
11
52
|
@config = YAML.load(File.read(@config_file))
|
|
12
53
|
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
end
|
|
54
|
+
raise AuthError, "Missing user id in the config file #{@config_file}" if user.id.nil?
|
|
55
|
+
raise AuthError, "Missing password in the config file #{@config_file}" if user.pass.nil?
|
|
16
56
|
else
|
|
17
|
-
@config =
|
|
18
|
-
@send_auth_headers = false
|
|
19
|
-
@auto_manage_tokens = false
|
|
57
|
+
@config = nil
|
|
20
58
|
end
|
|
21
59
|
|
|
22
60
|
if active_repl?
|
|
@@ -30,15 +68,18 @@ class Minisky
|
|
|
30
68
|
end
|
|
31
69
|
end
|
|
32
70
|
|
|
71
|
+
def save_config
|
|
72
|
+
File.write(@config_file, YAML.dump(@config)) if @config_file
|
|
73
|
+
end
|
|
74
|
+
|
|
75
|
+
|
|
76
|
+
private
|
|
77
|
+
|
|
33
78
|
def active_repl?
|
|
34
79
|
return true if defined?(IRB) && IRB.respond_to?(:CurrentContext) && IRB.CurrentContext
|
|
35
80
|
return true if defined?(Pry) && Pry.respond_to?(:cli) && Pry.cli
|
|
36
81
|
false
|
|
37
82
|
end
|
|
38
|
-
|
|
39
|
-
def save_config
|
|
40
|
-
File.write(@config_file, YAML.dump(@config)) if @config_file
|
|
41
|
-
end
|
|
42
83
|
end
|
|
43
84
|
|
|
44
85
|
require_relative 'requests'
|