Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add AmqpDsn class #3254

Merged
merged 19 commits into from Dec 18, 2021
Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions changes/3254-kludex.md
@@ -0,0 +1 @@
Add RabbitMqDsn class
2 changes: 2 additions & 0 deletions docs/examples/settings_main.py
Expand Up @@ -6,6 +6,7 @@
PyObject,
RedisDsn,
PostgresDsn,
RabbitMqDsn,
Field,
)

Expand All @@ -21,6 +22,7 @@ class Settings(BaseSettings):

redis_dsn: RedisDsn = 'redis://user:pass@localhost:6379/1'
pg_dsn: PostgresDsn = 'postgres://user:pass@localhost:5432/foobar'
rabbitmq_dsn: RabbitMqDsn = 'amqp://guest:guest@rabbitmq:5672//'
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

/ is the default vhost. Should I explain something about it? If yes, where?


special_function: PyObject = 'math.cos'

Expand Down
4 changes: 4 additions & 0 deletions docs/usage/types.md
Expand Up @@ -492,6 +492,9 @@ _(This script is complete, it should run "as is")_
`PostgresDsn`
: a postgres DSN style URL; see [URLs](#urls)

`RabbitMqDsn`
Kludex marked this conversation as resolved.
Show resolved Hide resolved
: a RabbitMQ DSN style URL; see [URLs](#urls)
Kludex marked this conversation as resolved.
Show resolved Hide resolved

`RedisDsn`
: a redis DSN style URL; see [URLs](#urls)

Expand Down Expand Up @@ -584,6 +587,7 @@ For URI/URL validation the following types are available:
- `postgresql+psycopg2cffi`
- `postgresql+py-postgresql`
- `postgresql+pygresql`
- `RabbitMqDsn`: schema `amqp`, user info required, TLD not required
- `RedisDsn`: scheme `redis` or `rediss`, user info not required, tld not required, host not required (CHANGED: user info
not required from **v1.6** onwards), user info may be passed without user part (e.g., `rediss://:pass@localhost`)
- `stricturl`: method with the following keyword arguments:
Expand Down
1 change: 1 addition & 0 deletions pydantic/__init__.py
Expand Up @@ -57,6 +57,7 @@
'IPvAnyInterface',
'IPvAnyNetwork',
'PostgresDsn',
'RabbitMqDsn',
'RedisDsn',
'KafkaDsn',
'validate_email',
Expand Down
6 changes: 6 additions & 0 deletions pydantic/networks.py
Expand Up @@ -69,6 +69,7 @@ class Parts(TypedDict, total=False):
'IPvAnyInterface',
'IPvAnyNetwork',
'PostgresDsn',
'RabbitMqDsn',
'RedisDsn',
'KafkaDsn',
'validate_email',
Expand Down Expand Up @@ -352,6 +353,11 @@ class PostgresDsn(AnyUrl):
user_required = True


class RabbitMqDsn(AnyUrl):
allowed_schemes = {'amqp'}
user_required = True
Kludex marked this conversation as resolved.
Show resolved Hide resolved


class RedisDsn(AnyUrl):
allowed_schemes = {'redis', 'rediss'}
host_required = False
Expand Down
19 changes: 19 additions & 0 deletions tests/test_networks.py
Expand Up @@ -9,6 +9,7 @@
KafkaDsn,
NameEmail,
PostgresDsn,
RabbitMqDsn,
RedisDsn,
ValidationError,
stricturl,
Expand Down Expand Up @@ -443,6 +444,24 @@ class Model(BaseModel):
assert error == {'loc': ('a',), 'msg': 'URL host invalid', 'type': 'value_error.url.host'}


def test_rabbitmq_dsns():
class Model(BaseModel):
a: RabbitMqDsn

assert Model(a='amqp://user:pass@localhost:5432/app').a == 'amqp://user:pass@localhost:5432/app'
assert Model(a='amqp://user:pass@localhost:5432//').a == 'amqp://user:pass@localhost:5432//'

with pytest.raises(ValidationError) as exc_info:
Model(a='http://example.org')
assert exc_info.value.errors()[0]['type'] == 'value_error.url.scheme'
assert exc_info.value.json().startswith('[')

with pytest.raises(ValidationError) as exc_info:
Model(a='amqp://localhost:5432/app')
error = exc_info.value.errors()[0]
assert error == {'loc': ('a',), 'msg': 'userinfo required in URL but missing', 'type': 'value_error.url.userinfo'}


def test_redis_dsns():
class Model(BaseModel):
a: RedisDsn
Expand Down