longwei 1.0.0.dev0__tar.gz → 1.1.0__tar.gz
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.
- {longwei-1.0.0.dev0 → longwei-1.1.0}/PKG-INFO +35 -22
- {longwei-1.0.0.dev0 → longwei-1.1.0}/README.md +33 -20
- {longwei-1.0.0.dev0 → longwei-1.1.0}/pyproject.toml +7 -6
- longwei-1.0.0.dev0/src/longwei/client_2_server.py → longwei-1.1.0/src/longwei/__init__.py +59 -36
- longwei-1.1.0/src/longwei/_base.py +16 -0
- {longwei-1.0.0.dev0 → longwei-1.1.0}/src/longwei/_mixin_auth.py +33 -13
- {longwei-1.0.0.dev0 → longwei-1.1.0}/src/longwei/_mixin_credentials.py +54 -19
- {longwei-1.0.0.dev0 → longwei-1.1.0}/src/longwei/_mixin_request_helpers.py +14 -15
- {longwei-1.0.0.dev0 → longwei-1.1.0}/src/longwei/_mixin_statuses.py +188 -45
- {longwei-1.0.0.dev0 → longwei-1.1.0}/src/longwei/_mixin_timelines.py +71 -31
- {longwei-1.0.0.dev0 → longwei-1.1.0}/src/longwei/constants.py +4 -2
- longwei-1.1.0/src/longwei/enums.py +20 -0
- {longwei-1.0.0.dev0 → longwei-1.1.0}/src/longwei/models.py +16 -1
- longwei-1.1.0/src/longwei/types.py +9 -0
- longwei-1.0.0.dev0/src/longwei/__init__.py +0 -74
- longwei-1.0.0.dev0/src/longwei/types.py +0 -9
- {longwei-1.0.0.dev0 → longwei-1.1.0}/src/longwei/exceptions.py +0 -0
- {longwei-1.0.0.dev0 → longwei-1.1.0}/src/longwei/utils.py +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.3
|
|
2
2
|
Name: longwei
|
|
3
|
-
Version: 1.
|
|
3
|
+
Version: 1.1.0
|
|
4
4
|
Summary: Python client library for ActivityPub-compatible servers
|
|
5
5
|
Author: marvin8
|
|
6
6
|
Author-email: marvin8 <marvin8@tuta.io>
|
|
@@ -20,13 +20,14 @@ Requires-Dist: pytz~=2026.1.post1
|
|
|
20
20
|
Requires-Dist: whenever~=0.9.5
|
|
21
21
|
Requires-Python: >=3.11, <3.15
|
|
22
22
|
Project-URL: Changelog, https://codeberg.org/MarvinsMastodonTools/longwei/src/branch/main/CHANGELOG.md
|
|
23
|
-
Project-URL: Documentation, https://marvinsmastodontools.codeberg.page/longwei/
|
|
23
|
+
Project-URL: Documentation, https://marvinsmastodontools.codeberg.page/longwei/latest/
|
|
24
24
|
Project-URL: Issues, https://codeberg.org/MarvinsMastodonTools/longwei/issues
|
|
25
25
|
Project-URL: Source, https://codeberg.org/MarvinsMastodonTools/longwei
|
|
26
26
|
Description-Content-Type: text/markdown
|
|
27
27
|
|
|
28
28
|
# longwei
|
|
29
29
|
|
|
30
|
+
[](https://marvinsmastodontools.codeberg.page/longwei/latest/ "Latest documentation")
|
|
30
31
|
[](https://codeberg.org/MarvinsMastodonTools/longwei "Repo at Codeberg.org")
|
|
31
32
|
[](https://ci.codeberg.org/MarvinsMastodonTools/longwei "CI / Woodpecker")
|
|
32
33
|
[](https://pepy.tech/project/longwei "Download count")
|
|
@@ -39,25 +40,48 @@ Description-Content-Type: text/markdown
|
|
|
39
40
|
[](https://pypi.org/project/longwei "PyPI – Wheel")
|
|
40
41
|
[](https://spdx.org/licenses/AGPL-3.0-or-later.html "AGPL 3 or later")
|
|
41
42
|
|
|
42
|
-
Longwei is a minimal Python implementation of the ActivityPub REST API used by [Mastodon](https://joinmastodon.org/), [Pleroma](https://pleroma.social/), [GotoSocial](https://gotosocial.org/)
|
|
43
|
+
Longwei is a minimal Python implementation of the ActivityPub client REST API used by [Mastodon](https://joinmastodon.org/), [Pleroma](https://pleroma.social/), and [GotoSocial](https://gotosocial.org/). This implementation makes use of asyncio where appropriate. It is intended to be used as a library by other applications. No standalone functionality is provided.
|
|
43
44
|
|
|
44
|
-
So far Longwei only implements the
|
|
45
|
+
So far Longwei only implements the ActivityPub API calls I need for my other projects [Fedinesia](https://codeberg.org/MarvinsMastodonTools/fedinesia), [Feed2Fedi](https://codeberg.org/marvinsmastodontools/feed2fedi), [FenLiu and Zhongli](https://codeberg.org/marvinsmastodontools/dujiangyan).
|
|
45
46
|
|
|
46
47
|
**DO NOT** expect a full or complete implementation of all [ActivityPub API](https://activitypub.rocks/) functionality.
|
|
47
48
|
|
|
49
|
+
## Quick start
|
|
50
|
+
|
|
51
|
+
```bash
|
|
52
|
+
pip install longwei
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
```python
|
|
56
|
+
import asyncio
|
|
57
|
+
import httpx
|
|
58
|
+
from longwei import APClient
|
|
59
|
+
|
|
60
|
+
async def main():
|
|
61
|
+
async with httpx.AsyncClient() as client:
|
|
62
|
+
ap = await APClient.create(
|
|
63
|
+
instance="mastodon.social",
|
|
64
|
+
client=client,
|
|
65
|
+
access_token="your_token",
|
|
66
|
+
)
|
|
67
|
+
status = await ap.post_status("Hello, fediverse!")
|
|
68
|
+
print(status.url)
|
|
69
|
+
|
|
70
|
+
asyncio.run(main())
|
|
71
|
+
```
|
|
72
|
+
|
|
48
73
|
## API References
|
|
49
74
|
|
|
50
75
|
- [Mastodon API](https://docs.joinmastodon.org/api/)
|
|
51
76
|
- [Pleroma API](https://api.pleroma.social)
|
|
52
77
|
- [GotoSocial API](https://docs.gotosocial.org/en/stable/api/swagger/)
|
|
53
78
|
|
|
54
|
-
##
|
|
79
|
+
## Status
|
|
55
80
|
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
I advise not to use this at until at least version 1.0.0 (non dev)
|
|
81
|
+
longwei is a fork of `minimal_activitypub`. The main breaking change from that package is the
|
|
82
|
+
class rename: `ActivityPub` → `APClient`. See the [Upgrading guide](https://marvinsmastodontools.codeberg.page/longwei/latest/upgrading/) for migration instructions.
|
|
60
83
|
|
|
84
|
+
It is **not** a drop-in replacement. See the upgrading guide for migration instructions.
|
|
61
85
|
|
|
62
86
|
## Contributing
|
|
63
87
|
|
|
@@ -67,26 +91,15 @@ longwei is using [pre-commit](https://pre-commit.com/) for code quality checks a
|
|
|
67
91
|
|
|
68
92
|
## Documentation
|
|
69
93
|
|
|
70
|
-
The documentation is
|
|
94
|
+
The live documentation is at **https://marvinsmastodontools.codeberg.page/longwei/latest/**
|
|
71
95
|
|
|
72
|
-
|
|
96
|
+
Built with [MkDocs](https://www.mkdocs.org/) and the [Material theme](https://squidfunk.github.io/mkdocs-material/). To build and serve locally:
|
|
73
97
|
|
|
74
98
|
```bash
|
|
75
|
-
# Install documentation dependencies (using the docs group from pyproject.toml)
|
|
76
99
|
uv sync --group docs
|
|
77
|
-
|
|
78
|
-
# Build documentation
|
|
79
|
-
mkdocs build
|
|
80
|
-
|
|
81
|
-
# Serve documentation locally
|
|
82
100
|
mkdocs serve
|
|
83
101
|
```
|
|
84
102
|
|
|
85
|
-
### Documentation Structure
|
|
86
|
-
|
|
87
|
-
- `docs/` - Markdown source files
|
|
88
|
-
- `mkdocs.yml` - MkDocs configuration
|
|
89
|
-
|
|
90
103
|
## Development
|
|
91
104
|
|
|
92
105
|
This project uses [uv](https://docs.astral.sh/uv/) for dependency management and virtual environments. To set up the development environment:
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
# longwei
|
|
2
2
|
|
|
3
|
+
[](https://marvinsmastodontools.codeberg.page/longwei/latest/ "Latest documentation")
|
|
3
4
|
[](https://codeberg.org/MarvinsMastodonTools/longwei "Repo at Codeberg.org")
|
|
4
5
|
[](https://ci.codeberg.org/MarvinsMastodonTools/longwei "CI / Woodpecker")
|
|
5
6
|
[](https://pepy.tech/project/longwei "Download count")
|
|
@@ -12,25 +13,48 @@
|
|
|
12
13
|
[](https://pypi.org/project/longwei "PyPI – Wheel")
|
|
13
14
|
[](https://spdx.org/licenses/AGPL-3.0-or-later.html "AGPL 3 or later")
|
|
14
15
|
|
|
15
|
-
Longwei is a minimal Python implementation of the ActivityPub REST API used by [Mastodon](https://joinmastodon.org/), [Pleroma](https://pleroma.social/), [GotoSocial](https://gotosocial.org/)
|
|
16
|
+
Longwei is a minimal Python implementation of the ActivityPub client REST API used by [Mastodon](https://joinmastodon.org/), [Pleroma](https://pleroma.social/), and [GotoSocial](https://gotosocial.org/). This implementation makes use of asyncio where appropriate. It is intended to be used as a library by other applications. No standalone functionality is provided.
|
|
16
17
|
|
|
17
|
-
So far Longwei only implements the
|
|
18
|
+
So far Longwei only implements the ActivityPub API calls I need for my other projects [Fedinesia](https://codeberg.org/MarvinsMastodonTools/fedinesia), [Feed2Fedi](https://codeberg.org/marvinsmastodontools/feed2fedi), [FenLiu and Zhongli](https://codeberg.org/marvinsmastodontools/dujiangyan).
|
|
18
19
|
|
|
19
20
|
**DO NOT** expect a full or complete implementation of all [ActivityPub API](https://activitypub.rocks/) functionality.
|
|
20
21
|
|
|
22
|
+
## Quick start
|
|
23
|
+
|
|
24
|
+
```bash
|
|
25
|
+
pip install longwei
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
```python
|
|
29
|
+
import asyncio
|
|
30
|
+
import httpx
|
|
31
|
+
from longwei import APClient
|
|
32
|
+
|
|
33
|
+
async def main():
|
|
34
|
+
async with httpx.AsyncClient() as client:
|
|
35
|
+
ap = await APClient.create(
|
|
36
|
+
instance="mastodon.social",
|
|
37
|
+
client=client,
|
|
38
|
+
access_token="your_token",
|
|
39
|
+
)
|
|
40
|
+
status = await ap.post_status("Hello, fediverse!")
|
|
41
|
+
print(status.url)
|
|
42
|
+
|
|
43
|
+
asyncio.run(main())
|
|
44
|
+
```
|
|
45
|
+
|
|
21
46
|
## API References
|
|
22
47
|
|
|
23
48
|
- [Mastodon API](https://docs.joinmastodon.org/api/)
|
|
24
49
|
- [Pleroma API](https://api.pleroma.social)
|
|
25
50
|
- [GotoSocial API](https://docs.gotosocial.org/en/stable/api/swagger/)
|
|
26
51
|
|
|
27
|
-
##
|
|
52
|
+
## Status
|
|
28
53
|
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
I advise not to use this at until at least version 1.0.0 (non dev)
|
|
54
|
+
longwei is a fork of `minimal_activitypub`. The main breaking change from that package is the
|
|
55
|
+
class rename: `ActivityPub` → `APClient`. See the [Upgrading guide](https://marvinsmastodontools.codeberg.page/longwei/latest/upgrading/) for migration instructions.
|
|
33
56
|
|
|
57
|
+
It is **not** a drop-in replacement. See the upgrading guide for migration instructions.
|
|
34
58
|
|
|
35
59
|
## Contributing
|
|
36
60
|
|
|
@@ -40,26 +64,15 @@ longwei is using [pre-commit](https://pre-commit.com/) for code quality checks a
|
|
|
40
64
|
|
|
41
65
|
## Documentation
|
|
42
66
|
|
|
43
|
-
The documentation is
|
|
67
|
+
The live documentation is at **https://marvinsmastodontools.codeberg.page/longwei/latest/**
|
|
44
68
|
|
|
45
|
-
|
|
69
|
+
Built with [MkDocs](https://www.mkdocs.org/) and the [Material theme](https://squidfunk.github.io/mkdocs-material/). To build and serve locally:
|
|
46
70
|
|
|
47
71
|
```bash
|
|
48
|
-
# Install documentation dependencies (using the docs group from pyproject.toml)
|
|
49
72
|
uv sync --group docs
|
|
50
|
-
|
|
51
|
-
# Build documentation
|
|
52
|
-
mkdocs build
|
|
53
|
-
|
|
54
|
-
# Serve documentation locally
|
|
55
73
|
mkdocs serve
|
|
56
74
|
```
|
|
57
75
|
|
|
58
|
-
### Documentation Structure
|
|
59
|
-
|
|
60
|
-
- `docs/` - Markdown source files
|
|
61
|
-
- `mkdocs.yml` - MkDocs configuration
|
|
62
|
-
|
|
63
76
|
## Development
|
|
64
77
|
|
|
65
78
|
This project uses [uv](https://docs.astral.sh/uv/) for dependency management and virtual environments. To set up the development environment:
|
|
@@ -4,7 +4,7 @@ build-backend = "uv_build"
|
|
|
4
4
|
|
|
5
5
|
[project]
|
|
6
6
|
name = "longwei"
|
|
7
|
-
version = "1.0
|
|
7
|
+
version = "1.1.0"
|
|
8
8
|
authors = [
|
|
9
9
|
{ name = "marvin8", email = "marvin8@tuta.io" },
|
|
10
10
|
]
|
|
@@ -35,15 +35,16 @@ dev = [
|
|
|
35
35
|
"complexipy~=5.2.0",
|
|
36
36
|
"git-cliff~=2.12.0",
|
|
37
37
|
"prek~=0.3.8",
|
|
38
|
-
"ruff~=0.15.
|
|
39
|
-
"ty~=0.0.
|
|
40
|
-
"uv~=0.11.
|
|
38
|
+
"ruff~=0.15.9",
|
|
39
|
+
"ty~=0.0.28",
|
|
40
|
+
"uv~=0.11.3",
|
|
41
41
|
]
|
|
42
42
|
docs = [
|
|
43
|
-
"mike~=2.1.4",
|
|
44
43
|
"mkdocs~=1.6.1",
|
|
45
44
|
"mkdocs-material~=9.7.6",
|
|
45
|
+
"mkdocstrings~=1.0.3",
|
|
46
46
|
"mkdocstrings-python~=2.0.3",
|
|
47
|
+
"mike~=2.1.4",
|
|
47
48
|
]
|
|
48
49
|
nox = [
|
|
49
50
|
"nox-uv~=0.7.1",
|
|
@@ -61,7 +62,7 @@ constraint-dependencies = [
|
|
|
61
62
|
]
|
|
62
63
|
|
|
63
64
|
[project.urls]
|
|
64
|
-
Documentation = "https://marvinsmastodontools.codeberg.page/longwei/"
|
|
65
|
+
Documentation = "https://marvinsmastodontools.codeberg.page/longwei/latest/"
|
|
65
66
|
Issues = "https://codeberg.org/MarvinsMastodonTools/longwei/issues"
|
|
66
67
|
Source = "https://codeberg.org/MarvinsMastodonTools/longwei"
|
|
67
68
|
Changelog = "https://codeberg.org/MarvinsMastodonTools/longwei/src/branch/main/CHANGELOG.md"
|
|
@@ -1,49 +1,44 @@
|
|
|
1
|
-
"""
|
|
2
|
-
|
|
3
|
-
This is a minimal implementation only implementing some API calls. API
|
|
4
|
-
calls supported will likely be expanded over time. However, do not
|
|
5
|
-
expect a full or complete implementation of the ActivityPub API.
|
|
6
|
-
"""
|
|
1
|
+
"""Package 'longwei' level definitions."""
|
|
7
2
|
|
|
8
3
|
import logging
|
|
9
4
|
|
|
10
5
|
from httpx import AsyncClient
|
|
11
6
|
from whenever import Instant
|
|
12
7
|
|
|
13
|
-
from longwei import
|
|
14
|
-
from longwei import
|
|
15
|
-
from longwei import
|
|
16
|
-
from longwei import __version__
|
|
8
|
+
from longwei._base import USER_AGENT
|
|
9
|
+
from longwei._base import __display_name__
|
|
10
|
+
from longwei._base import __version__
|
|
17
11
|
from longwei._mixin_auth import _AuthMixin
|
|
18
12
|
from longwei._mixin_credentials import _CredentialsMixin
|
|
19
13
|
from longwei._mixin_request_helpers import _RequestHelpersMixin
|
|
20
14
|
from longwei._mixin_statuses import _StatusesMixin
|
|
21
15
|
from longwei._mixin_timelines import _TimelinesMixin
|
|
22
16
|
from longwei.constants import INSTANCE_TYPE_MASTODON
|
|
23
|
-
from longwei.constants import INSTANCE_TYPE_PLEROMA
|
|
24
|
-
from longwei.constants import INSTANCE_TYPE_TAKAHE # noqa: F401
|
|
17
|
+
from longwei.constants import INSTANCE_TYPE_PLEROMA
|
|
25
18
|
from longwei.constants import MAX_ATTACHMENTS_MASTODON
|
|
26
|
-
from longwei.
|
|
27
|
-
from longwei.
|
|
28
|
-
from longwei.exceptions import ActivityPubError
|
|
29
|
-
from longwei.exceptions import ApiError
|
|
30
|
-
from longwei.exceptions import ClientError
|
|
31
|
-
from longwei.exceptions import ConflictError
|
|
32
|
-
from longwei.exceptions import ForbiddenError
|
|
33
|
-
from longwei.exceptions import GoneError
|
|
34
|
-
from longwei.exceptions import NetworkError
|
|
35
|
-
from longwei.exceptions import NotFoundError
|
|
36
|
-
from longwei.exceptions import RatelimitError
|
|
37
|
-
from longwei.exceptions import ServerError
|
|
38
|
-
from longwei.exceptions import UnauthorizedError
|
|
39
|
-
from longwei.exceptions import UnprocessedError
|
|
40
|
-
from longwei.
|
|
41
|
-
from longwei.
|
|
19
|
+
from longwei.enums import SearchType
|
|
20
|
+
from longwei.enums import Visibility
|
|
21
|
+
from longwei.exceptions import ActivityPubError
|
|
22
|
+
from longwei.exceptions import ApiError
|
|
23
|
+
from longwei.exceptions import ClientError
|
|
24
|
+
from longwei.exceptions import ConflictError
|
|
25
|
+
from longwei.exceptions import ForbiddenError
|
|
26
|
+
from longwei.exceptions import GoneError
|
|
27
|
+
from longwei.exceptions import NetworkError
|
|
28
|
+
from longwei.exceptions import NotFoundError
|
|
29
|
+
from longwei.exceptions import RatelimitError
|
|
30
|
+
from longwei.exceptions import ServerError
|
|
31
|
+
from longwei.exceptions import UnauthorizedError
|
|
32
|
+
from longwei.exceptions import UnprocessedError
|
|
33
|
+
from longwei.models import Account
|
|
34
|
+
from longwei.models import SearchResults
|
|
35
|
+
from longwei.types import APClientClass
|
|
36
|
+
from longwei.types import Status
|
|
42
37
|
|
|
43
38
|
logger = logging.getLogger(__display_name__)
|
|
44
39
|
|
|
45
40
|
|
|
46
|
-
class
|
|
41
|
+
class APClient(_RequestHelpersMixin, _CredentialsMixin, _TimelinesMixin, _StatusesMixin, _AuthMixin):
|
|
47
42
|
"""Simplifies interacting with an ActivityPub server / instance.
|
|
48
43
|
|
|
49
44
|
This is a minimal implementation only implementing methods needed
|
|
@@ -51,13 +46,13 @@ class ActivityPub(_RequestHelpersMixin, _CredentialsMixin, _TimelinesMixin, _Sta
|
|
|
51
46
|
"""
|
|
52
47
|
|
|
53
48
|
def __init__(
|
|
54
|
-
self:
|
|
49
|
+
self: APClientClass,
|
|
55
50
|
instance: str,
|
|
56
51
|
client: AsyncClient,
|
|
57
52
|
access_token: str | None = None,
|
|
58
53
|
timeout: int | None = None,
|
|
59
54
|
) -> None:
|
|
60
|
-
"""Initialise
|
|
55
|
+
"""Initialise APClient instance with reasonable default values.
|
|
61
56
|
|
|
62
57
|
:param instance: domain name or url to instance to connect to
|
|
63
58
|
:param client: httpx AsyncClient to use for communicating with instance
|
|
@@ -85,11 +80,11 @@ class ActivityPub(_RequestHelpersMixin, _CredentialsMixin, _TimelinesMixin, _Sta
|
|
|
85
80
|
self.ratelimit_remaining = 300
|
|
86
81
|
self.ratelimit_reset = Instant.now().py_datetime()
|
|
87
82
|
logger.debug(
|
|
88
|
-
"
|
|
83
|
+
"APClient(instance=%s, client=%s, access_token=<redacted>)",
|
|
89
84
|
instance,
|
|
90
85
|
client,
|
|
91
86
|
)
|
|
92
|
-
logger.debug("
|
|
87
|
+
logger.debug("APClient() ... version=%s", __version__)
|
|
93
88
|
|
|
94
89
|
@classmethod
|
|
95
90
|
async def create(
|
|
@@ -98,8 +93,8 @@ class ActivityPub(_RequestHelpersMixin, _CredentialsMixin, _TimelinesMixin, _Sta
|
|
|
98
93
|
client: AsyncClient,
|
|
99
94
|
access_token: str | None = None,
|
|
100
95
|
timeout: int | None = None,
|
|
101
|
-
) -> "
|
|
102
|
-
"""Create and initialise an
|
|
96
|
+
) -> "APClient":
|
|
97
|
+
"""Create and initialise an APClient instance, auto-detecting the instance type.
|
|
103
98
|
|
|
104
99
|
This is the recommended entry point. Unlike direct instantiation, this factory
|
|
105
100
|
automatically calls :meth:`determine_instance_type` to configure server-specific
|
|
@@ -109,9 +104,37 @@ class ActivityPub(_RequestHelpersMixin, _CredentialsMixin, _TimelinesMixin, _Sta
|
|
|
109
104
|
:param client: httpx AsyncClient to use for communicating with instance
|
|
110
105
|
:param access_token: authentication token
|
|
111
106
|
:param timeout: optional seconds to wait for a response (default: 120)
|
|
112
|
-
:returns: Fully initialised :class:`
|
|
107
|
+
:returns: Fully initialised :class:`APClient` instance
|
|
113
108
|
:raises NetworkError: if the instance cannot be reached during type detection
|
|
114
109
|
"""
|
|
115
110
|
ap = cls(instance=instance, client=client, access_token=access_token, timeout=timeout)
|
|
116
111
|
await ap.determine_instance_type()
|
|
117
112
|
return ap
|
|
113
|
+
|
|
114
|
+
|
|
115
|
+
__all__ = [ # noqa: RUF022
|
|
116
|
+
"Account",
|
|
117
|
+
"APClient",
|
|
118
|
+
"APClientClass",
|
|
119
|
+
"ActivityPubError",
|
|
120
|
+
"ApiError",
|
|
121
|
+
"ClientError",
|
|
122
|
+
"ConflictError",
|
|
123
|
+
"__display_name__",
|
|
124
|
+
"ForbiddenError",
|
|
125
|
+
"GoneError",
|
|
126
|
+
"INSTANCE_TYPE_MASTODON",
|
|
127
|
+
"INSTANCE_TYPE_PLEROMA",
|
|
128
|
+
"NetworkError",
|
|
129
|
+
"NotFoundError",
|
|
130
|
+
"RatelimitError",
|
|
131
|
+
"SearchResults",
|
|
132
|
+
"SearchType",
|
|
133
|
+
"ServerError",
|
|
134
|
+
"Status",
|
|
135
|
+
"UnauthorizedError",
|
|
136
|
+
"UnprocessedError",
|
|
137
|
+
"USER_AGENT",
|
|
138
|
+
"__version__",
|
|
139
|
+
"Visibility",
|
|
140
|
+
]
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
"""Internal base definitions shared across mixin modules.
|
|
2
|
+
|
|
3
|
+
This module exists to break the circular import that would arise if mixins
|
|
4
|
+
imported directly from the ``longwei`` package: package-level constants live
|
|
5
|
+
here so that ``__init__.py`` can import both this module and the mixins
|
|
6
|
+
without a cycle.
|
|
7
|
+
"""
|
|
8
|
+
|
|
9
|
+
import sys
|
|
10
|
+
from importlib.metadata import version
|
|
11
|
+
from typing import Final
|
|
12
|
+
|
|
13
|
+
__display_name__: Final[str] = "Longwei"
|
|
14
|
+
__version__: Final[str] = version("longwei")
|
|
15
|
+
|
|
16
|
+
USER_AGENT: Final[str] = f"{__display_name__}_v{__version__}_Python_{sys.version.split()[0]}"
|
|
@@ -7,8 +7,8 @@ from urllib.parse import urlencode
|
|
|
7
7
|
from httpx import AsyncClient
|
|
8
8
|
from httpx import HTTPError
|
|
9
9
|
|
|
10
|
-
from longwei import USER_AGENT
|
|
11
|
-
from longwei import __display_name__
|
|
10
|
+
from longwei._base import USER_AGENT
|
|
11
|
+
from longwei._base import __display_name__
|
|
12
12
|
from longwei.constants import REDIRECT_URI
|
|
13
13
|
from longwei.exceptions import NetworkError
|
|
14
14
|
from longwei.utils import _check_exception
|
|
@@ -32,9 +32,14 @@ class _AuthMixin:
|
|
|
32
32
|
:param user_agent: User agent identifier to use. Defaults to longwei related one.
|
|
33
33
|
|
|
34
34
|
:returns: String containing URL to visit to get access token interactively from instance.
|
|
35
|
+
|
|
36
|
+
Note:
|
|
37
|
+
Constructs a URL for ``GET /oauth/authorize``
|
|
38
|
+
(Mastodon, Pleroma, GotoSocial). No HTTP request is made by this method.
|
|
39
|
+
|
|
35
40
|
"""
|
|
36
41
|
logger.debug(
|
|
37
|
-
"
|
|
42
|
+
"APClient.get_auth_token_interactive(instance_url=%s, client=...,client_id=%s, user_agent=%s)",
|
|
38
43
|
instance_url,
|
|
39
44
|
client_id,
|
|
40
45
|
user_agent,
|
|
@@ -52,7 +57,7 @@ class _AuthMixin:
|
|
|
52
57
|
)
|
|
53
58
|
auth_url = f"{instance_url}/oauth/authorize?{url_params}"
|
|
54
59
|
logger.debug(
|
|
55
|
-
"
|
|
60
|
+
"APClient.get_auth_token_interactive(...) -> %s",
|
|
56
61
|
auth_url,
|
|
57
62
|
)
|
|
58
63
|
|
|
@@ -75,9 +80,14 @@ class _AuthMixin:
|
|
|
75
80
|
:param client_secret: client secret as returned by create_app method
|
|
76
81
|
|
|
77
82
|
:returns: access token
|
|
83
|
+
|
|
84
|
+
Note:
|
|
85
|
+
Implements ``POST /oauth/token`` with ``grant_type=authorization_code``
|
|
86
|
+
(Mastodon, Pleroma, GotoSocial).
|
|
87
|
+
|
|
78
88
|
"""
|
|
79
89
|
logger.debug(
|
|
80
|
-
"
|
|
90
|
+
"APClient.validate_authorization_code(authorization_code=%s, client_id=%s, client_secret=<redacted>)",
|
|
81
91
|
authorization_code,
|
|
82
92
|
client_id,
|
|
83
93
|
)
|
|
@@ -95,7 +105,7 @@ class _AuthMixin:
|
|
|
95
105
|
try:
|
|
96
106
|
response = await client.post(url=f"{instance_url}/oauth/token", data=data)
|
|
97
107
|
logger.debug(
|
|
98
|
-
"
|
|
108
|
+
"APClient.validate_authorization_code - response:\n%s",
|
|
99
109
|
response,
|
|
100
110
|
)
|
|
101
111
|
await _check_exception(response)
|
|
@@ -104,7 +114,7 @@ class _AuthMixin:
|
|
|
104
114
|
raise NetworkError from error
|
|
105
115
|
|
|
106
116
|
logger.debug(
|
|
107
|
-
"
|
|
117
|
+
"APClient.validate_authorization_code - response.json: \n%s",
|
|
108
118
|
json.dumps(response_dict, indent=4),
|
|
109
119
|
)
|
|
110
120
|
return str(response_dict["access_token"])
|
|
@@ -128,9 +138,15 @@ class _AuthMixin:
|
|
|
128
138
|
:param client_website: Link to site for user_agent. Defaults to link to longwei on Pypi.org
|
|
129
139
|
|
|
130
140
|
:returns: The access token is being returned.
|
|
141
|
+
|
|
142
|
+
Note:
|
|
143
|
+
Implements ``POST /oauth/token`` with ``grant_type=password``
|
|
144
|
+
(Pleroma, GotoSocial). The password grant is not supported by most
|
|
145
|
+
Mastodon instances by default.
|
|
146
|
+
|
|
131
147
|
"""
|
|
132
148
|
logger.debug(
|
|
133
|
-
"
|
|
149
|
+
"APClient.get_auth_token(instance_url=%s, username=%s, password=<redacted>, client=..., "
|
|
134
150
|
"user_agent=%s, client_website=%s)",
|
|
135
151
|
instance_url,
|
|
136
152
|
username,
|
|
@@ -159,14 +175,14 @@ class _AuthMixin:
|
|
|
159
175
|
|
|
160
176
|
try:
|
|
161
177
|
response = await client.post(url=f"{instance_url}/oauth/token", data=data)
|
|
162
|
-
logger.debug("
|
|
178
|
+
logger.debug("APClient.get_auth_token - response:\n%s", response)
|
|
163
179
|
await _check_exception(response)
|
|
164
180
|
response_dict = response.json()
|
|
165
181
|
except HTTPError as error:
|
|
166
182
|
raise NetworkError from error
|
|
167
183
|
|
|
168
184
|
logger.debug(
|
|
169
|
-
"
|
|
185
|
+
"APClient.get_auth_token - response.json: \n%s",
|
|
170
186
|
json.dumps(response_dict, indent=4),
|
|
171
187
|
)
|
|
172
188
|
return str(response_dict["access_token"])
|
|
@@ -186,9 +202,13 @@ class _AuthMixin:
|
|
|
186
202
|
:param client_website: Link to site for user_agent. Defaults to link to longwei on Pypi.org
|
|
187
203
|
|
|
188
204
|
:returns: tuple(client_id, client_secret)
|
|
205
|
+
|
|
206
|
+
Note:
|
|
207
|
+
Implements ``POST /api/v1/apps`` (Mastodon, Pleroma, GotoSocial).
|
|
208
|
+
|
|
189
209
|
"""
|
|
190
210
|
logger.debug(
|
|
191
|
-
"
|
|
211
|
+
"APClient.create_app(instance_url=%s, client=..., user_agent=%s, client_website=%s)",
|
|
192
212
|
instance_url,
|
|
193
213
|
user_agent,
|
|
194
214
|
client_website,
|
|
@@ -205,14 +225,14 @@ class _AuthMixin:
|
|
|
205
225
|
}
|
|
206
226
|
try:
|
|
207
227
|
response = await client.post(url=f"{instance_url}/api/v1/apps", data=data)
|
|
208
|
-
logger.debug("
|
|
228
|
+
logger.debug("APClient.create_app response: \n%s", response)
|
|
209
229
|
await _check_exception(response)
|
|
210
230
|
response_dict = response.json()
|
|
211
231
|
except HTTPError as error:
|
|
212
232
|
raise NetworkError from error
|
|
213
233
|
|
|
214
234
|
logger.debug(
|
|
215
|
-
"
|
|
235
|
+
"APClient.create_app response.json: \n%s",
|
|
216
236
|
json.dumps(response_dict, indent=4),
|
|
217
237
|
)
|
|
218
238
|
return (response_dict["client_id"]), (response_dict["client_secret"])
|