Coverage for filip/models/base.py: 94%

47 statements  

« prev     ^ index     » next       coverage.py v7.4.4, created at 2024-11-20 16:54 +0000

1""" 

2Shared data models 

3""" 

4 

5from aenum import Enum 

6from pydantic import ConfigDict, BaseModel, Field, field_validator 

7 

8from filip.utils.validators import (validate_fiware_service_path, 

9 validate_fiware_service) 

10 

11 

12class NgsiVersion(str, Enum): 

13 """ 

14 Version of NGSI-Specification that should be used within the target system. 

15 Note: 

16 Currently, the library only supports functionality for NGSI-v2 

17 """ 

18 

19 v2 = "v2" 

20 ld = "ld" 

21 

22 

23class DataType(str, Enum): 

24 """ 

25 When possible reuse schema.org data types 

26 (Text, Number, DateTime, StructuredValue, etc.). 

27 Remember that null is not allowed in NGSI-LD and 

28 therefore should be avoided as a value. 

29 

30 https://schema.org/DataType 

31 """ 

32 

33 _init_ = "value __doc__" 

34 

35 BOOLEAN = "Boolean", "True or False." 

36 DATE = "Date", "A date value in ISO 8601 date format." 

37 DATETIME = ( 

38 "DateTime", 

39 "A combination of date and time of day in the form " 

40 "[-]CCYY-MM-DDThh:mm:ss[Z|(+|-)hh:mm] " 

41 "(see Chapter 5.4 of ISO 8601).", 

42 ) 

43 NUMBER = ( 

44 "Number", 

45 "Use values from 0123456789 (Unicode 'DIGIT ZERO' " 

46 "(U+0030) to 'DIGIT NINE' (U+0039)) rather than " 

47 "superficially similar Unicode symbols. Use '.' " 

48 "(Unicode 'FULL STOP' (U+002E)) rather than ',' to " 

49 "indicate a decimal point. Avoid using these symbols " 

50 "as a readability separator.", 

51 ) 

52 INTEGER = "Integer", "Integer number" 

53 FLOAT = "Float", "Floating number. Please check 'DataType.Number'" 

54 TEXT = "Text", "https://schema.org/Text" 

55 TIME = ( 

56 "Time", 

57 "A point in time recurring on multiple days in the form " 

58 "hh:mm:ss[Z|(+|-)hh:mm] (see XML schema for details).", 

59 ) 

60 RELATIONSHIP = "Relationship", "Reference to another context entity" 

61 STRUCTUREDVALUE = "StructuredValue", ("Structured datatype must be an " 

62 "array or object" 

63 "serializable") 

64 ARRAY = "Array", "Array of the types above" 

65 OBJECT = "Object", "JSON-Object of the types above, i.e. a dictionary" 

66 COMMAND = "command", "A command for IoT Devices" 

67 COMMAND_RESULT = ( 

68 "commandResult", 

69 "An entity containing a command, " 

70 "contains an autogenerated attribute" 

71 "of this type", 

72 ) 

73 COMMAND_STATUS = ( 

74 "commandStatus", 

75 "An entity containing a command, " 

76 "contains an autogenerated attribute " 

77 "of this type", 

78 ) 

79 GEOJSON = "geo:json", "GeoJson object" 

80 

81 

82class PaginationMethod(str, Enum): 

83 """ 

84 Options for the internal pagination methods 

85 """ 

86 

87 GET = "GET" 

88 POST = "POST" 

89 

90 

91class FiwareHeader(BaseModel): 

92 """ 

93 Define entity service paths which are supported by the NGSI 

94 Context Brokers to support hierarchical scopes: 

95 https://fiware-orion.readthedocs.io/en/master/user/service_path/index.html 

96 """ 

97 

98 model_config = ConfigDict(populate_by_name=True, validate_assignment=True) 

99 

100 service: str = Field( 

101 alias="fiware-service", 

102 default="", 

103 max_length=50, 

104 description="Fiware service used for multi-tenancy", 

105 pattern=r"\w*$", 

106 ) 

107 service_path: str = Field( 

108 alias="fiware-servicepath", 

109 default="/", 

110 description="Fiware service path", 

111 max_length=51, 

112 ) 

113 valid_service = field_validator("service")(validate_fiware_service) 

114 valid_service_path = field_validator("service_path")(validate_fiware_service_path) 

115 

116 

117class FiwareHeaderSecure(FiwareHeader): 

118 """ 

119 Defines entity service paths and an authorization via Bearer-Token which are 

120 supported by the NGSI 

121 Context Brokers to support hierarchical scopes: 

122 https://fiware-orion.readthedocs.io/en/master/user/service_path/index.html 

123 """ 

124 

125 authorization: str = Field( 

126 alias="authorization", 

127 default="", 

128 max_length=3000, 

129 description="authorization key", 

130 pattern=r".*" 

131 ) 

132 

133 

134class LogLevel(str, Enum): 

135 CRITICAL = "CRITICAL" 

136 ERROR = "ERROR" 

137 WARNING = "WARNING" 

138 INFO = "INFO" 

139 DEBUG = "DEBUG" 

140 NOTSET = "NOTSET" 

141 

142 @classmethod 

143 def _missing_name_(cls, name): 

144 """ 

145 Class method to realize case-insensitive args 

146 

147 Args: 

148 name: missing argument 

149 

150 Returns: 

151 valid member of enum 

152 """ 

153 for member in cls: 

154 if member.value.casefold() == name.casefold(): 

155 return member