File tree Expand file tree Collapse file tree 1 file changed +35
-0
lines changed
examples/providers/configuration Expand file tree Collapse file tree 1 file changed +35
-0
lines changed Original file line number Diff line number Diff line change
1
+ """`Configuration` provider values loading example."""
2
+
3
+ import os
4
+
5
+ from dependency_injector import containers , providers
6
+ from pydantic import BaseSettings , Field
7
+
8
+ # Emulate environment variables
9
+ os .environ ["AWS_ACCESS_KEY_ID" ] = "KEY"
10
+ os .environ ["AWS_SECRET_ACCESS_KEY" ] = "SECRET"
11
+
12
+
13
+ class AwsSettings (BaseSettings ):
14
+
15
+ access_key_id : str = Field (env = "aws_access_key_id" )
16
+ secret_access_key : str = Field (env = "aws_secret_access_key" )
17
+
18
+
19
+ class Settings (BaseSettings ):
20
+
21
+ aws : AwsSettings = AwsSettings ()
22
+ optional : str = Field (default = "default_value" )
23
+
24
+
25
+ class Container (containers .DeclarativeContainer ):
26
+
27
+ config = providers .Configuration (pydantic_settings = [Settings ()])
28
+
29
+
30
+ if __name__ == "__main__" :
31
+ container = Container ()
32
+
33
+ assert container .config .aws .access_key_id () == "KEY"
34
+ assert container .config .aws .secret_access_key () == "SECRET"
35
+ assert container .config .optional () == "default_value"
You can’t perform that action at this time.
0 commit comments