Skip to content

Commit

Permalink
Add AmqpDsn class (#3254)
Browse files Browse the repository at this point in the history
* Add RabbitmqDsn

Will update once finished - haven't looked at the CONTRIB yet. Want to test locally to see if it works.

* added tests; added to docs

* added changes

* fixed import in networks.py

* fixed linting issues; fixed __init__.py import issue

* sorted imports

* added trailing comma on imports

* Merge master

* Change class name from RabbitmqDsn to RabbitMqDsn

* Format code

* Rename change file and prettify content

* Fix RabbitMQ name on documentation

* Add a trivial test

* Address Samuel and Nuno's comments

* Refactor AMQP tests according to Redis tests style

* Update docs/examples/settings_main.py

* cleanup

Co-authored-by: Thomas <thomas@9bitbyte.com>
Co-authored-by: Thomas Crha <tom.crha@dragonflytechnologies.com>
Co-authored-by: Samuel Colvin <s@muelcolvin.com>
  • Loading branch information
4 people committed Dec 18, 2021
1 parent c834f34 commit 7eaa582
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 0 deletions.
1 change: 1 addition & 0 deletions changes/3254-kludex.md
@@ -0,0 +1 @@
Add `AmqpDsn` class
2 changes: 2 additions & 0 deletions docs/examples/settings_main.py
Expand Up @@ -6,6 +6,7 @@
PyObject,
RedisDsn,
PostgresDsn,
AmqpDsn,
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'
amqp_dsn: AmqpDsn = 'amqp://user:pass@localhost:5672/'

special_function: PyObject = 'math.cos'

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

`RabbitMqDsn`
: an `AMQP` DSN style URL as used by RabbitMQ, StormMQ, ActiveMQ etc.; see [URLs](#urls)

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

Expand Down Expand Up @@ -633,6 +636,7 @@ For URI/URL validation the following types are available:
- `postgresql+psycopg2cffi`
- `postgresql+py-postgresql`
- `postgresql+pygresql`
- `AmqpDsn`: schema `amqp` or `amqps`, user info not required, TLD not required, host 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',
'AmqpDsn',
'RedisDsn',
'KafkaDsn',
'validate_email',
Expand Down
6 changes: 6 additions & 0 deletions pydantic/networks.py
Expand Up @@ -68,6 +68,7 @@ class Parts(TypedDict, total=False):
'IPvAnyInterface',
'IPvAnyNetwork',
'PostgresDsn',
'AmqpDsn',
'RedisDsn',
'KafkaDsn',
'validate_email',
Expand Down Expand Up @@ -351,6 +352,11 @@ class PostgresDsn(AnyUrl):
user_required = True


class AmqpDsn(AnyUrl):
allowed_schemes = {'amqp', 'amqps'}
host_required = False


class RedisDsn(AnyUrl):
allowed_schemes = {'redis', 'rediss'}
host_required = False
Expand Down
32 changes: 32 additions & 0 deletions tests/test_networks.py
@@ -1,6 +1,7 @@
import pytest

from pydantic import (
AmqpDsn,
AnyUrl,
BaseModel,
EmailStr,
Expand Down Expand Up @@ -443,6 +444,37 @@ class Model(BaseModel):
assert error == {'loc': ('a',), 'msg': 'URL host invalid', 'type': 'value_error.url.host'}


def test_amqp_dsns():
class Model(BaseModel):
a: AmqpDsn

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

m = Model(a='amqps://user:pass@localhost:5432//')
assert m.a == 'amqps://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'

# Password is not required for AMQP protocol
m = Model(a='amqp://localhost:1234/app')
assert m.a == 'amqp://localhost:1234/app'
assert m.a.user is None
assert m.a.password is None

# Only schema is required for AMQP protocol.
# https://www.rabbitmq.com/uri-spec.html
m = Model(a='amqps://')
assert m.a.scheme == 'amqps'
assert m.a.host is None
assert m.a.port is None
assert m.a.path is None


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

0 comments on commit 7eaa582

Please sign in to comment.