1
- # ****************************** -*-
2
- # flake8: noqa
3
1
# =================================================================
4
2
#
5
3
# Authors: Sander Schaminee <sander.schaminee@geocat.net>
32
30
# =================================================================
33
31
34
32
from pydantic import BaseModel , Field
33
+ import pydantic
34
+
35
+ # Handle Pydantic v1/v2 compatibility
36
+ if pydantic .VERSION .startswith ('1' ):
37
+ model_validator = 'parse_obj'
38
+ model_fields = '__fields__'
39
+ regex_param = {'regex' : r'^\d+\.\d+\..+$' }
40
+ else :
41
+ model_validator = 'model_validate'
42
+ model_fields = 'model_fields'
43
+ regex_param = {'pattern' : r'^\d+\.\d+\..+$' }
35
44
36
45
37
46
class APIRules (BaseModel ):
38
- """ Pydantic model for API design rules that must be adhered to. """
39
- api_version : str = Field (pattern = r'^\d+\.\d+\..+$' ,
40
- description = "Semantic API version number." )
47
+ """
48
+ Pydantic model for API design rules that must be adhered to.
49
+ """
50
+ api_version : str = Field (** regex_param ,
51
+ description = 'Semantic API version number.' )
41
52
url_prefix : str = Field (
42
- "" ,
53
+ '' ,
43
54
description = "If set, pygeoapi routes will be prepended with the "
44
55
"given URL path prefix (e.g. '/v1'). "
45
56
"Defaults to an empty string (no prefix)."
46
57
)
47
58
version_header : str = Field (
48
- "" ,
59
+ '' ,
49
60
description = "If set, pygeoapi will set a response header with this "
50
61
"name and its value will hold the API version. "
51
62
"Defaults to an empty string (i.e. no header). "
@@ -59,47 +70,55 @@ class APIRules(BaseModel):
59
70
60
71
@staticmethod
61
72
def create (** rules_config ) -> 'APIRules' :
62
- """ Returns a new APIRules instance for the current API version
63
- and configured rules. """
73
+ """
74
+ Returns a new APIRules instance for the current API version
75
+ and configured rules.
76
+ """
64
77
obj = {
65
- k : v for k , v in rules_config .items () if k in APIRules .model_fields
78
+ k : v for k , v in rules_config .items ()
79
+ if k in getattr (APIRules , model_fields )
66
80
}
67
81
# Validation will fail if required `api_version` is missing
68
82
# or if `api_version` is not a semantic version number
69
- return APIRules .model_validate (obj )
83
+ model_validator_ = getattr (APIRules , model_validator )
84
+ return model_validator_ (obj )
70
85
71
86
@property
72
87
def response_headers (self ) -> dict :
73
- """ Gets a dictionary of additional response headers for the current
74
- API rules. Returns an empty dict if no rules apply. """
88
+ """
89
+ Gets a dictionary of additional response headers for the current
90
+ API rules. Returns an empty dict if no rules apply.
91
+ """
75
92
headers = {}
76
93
if self .version_header :
77
94
headers [self .version_header ] = self .api_version
78
95
return headers
79
96
80
- def get_url_prefix (self , style : str = None ) -> str :
97
+ def get_url_prefix (self , style : str = '' ) -> str :
81
98
"""
82
99
Returns an API URL prefix to use in all paths.
83
100
May include a (partial) API version. See docs for syntax.
101
+
84
102
:param style: Set to 'django', 'flask' or 'starlette' to return a
85
103
specific prefix formatted for those frameworks.
86
104
If not set, only the prefix itself will be returned.
87
105
"""
88
106
if not self .url_prefix :
89
- return ""
107
+ return ''
90
108
major , minor , build = self .api_version .split ('.' )
91
109
prefix = self .url_prefix .format (
92
110
api_version = self .api_version ,
93
111
api_major = major ,
94
112
api_minor = minor ,
95
113
api_build = build
96
114
).strip ('/' )
97
- style = ( style or '' ). lower ()
115
+
98
116
if style == 'django' :
99
117
# Django requires the slash at the end
100
- return rf" ^{ prefix } /"
118
+ return rf' ^{ prefix } /'
101
119
elif style in ('flask' , 'starlette' ):
102
120
# Flask and Starlette need the slash in front
103
- return f"/{ prefix } "
104
- # If no format is specified, return only the bare prefix
105
- return prefix
121
+ return f'/{ prefix } '
122
+ else :
123
+ # If no format is specified, return only the bare prefix
124
+ return prefix
0 commit comments