Creates a method for attaching to a Resource. Args: methodName: string, name of the method to use. methodDesc: object, fragment of deserialized discovery document that describes the method. rootDesc: object, the entire deserialized discovery document. schema: obj
(methodName, methodDesc, rootDesc, schema)
| 1074 | |
| 1075 | |
| 1076 | def createMethod(methodName, methodDesc, rootDesc, schema): |
| 1077 | """Creates a method for attaching to a Resource. |
| 1078 | |
| 1079 | Args: |
| 1080 | methodName: string, name of the method to use. |
| 1081 | methodDesc: object, fragment of deserialized discovery document that |
| 1082 | describes the method. |
| 1083 | rootDesc: object, the entire deserialized discovery document. |
| 1084 | schema: object, mapping of schema names to schema descriptions. |
| 1085 | """ |
| 1086 | methodName = fix_method_name(methodName) |
| 1087 | ( |
| 1088 | pathUrl, |
| 1089 | httpMethod, |
| 1090 | methodId, |
| 1091 | accept, |
| 1092 | maxSize, |
| 1093 | mediaPathUrl, |
| 1094 | ) = _fix_up_method_description(methodDesc, rootDesc, schema) |
| 1095 | |
| 1096 | parameters = ResourceMethodParameters(methodDesc) |
| 1097 | |
| 1098 | def method(self, **kwargs): |
| 1099 | # Don't bother with doc string, it will be over-written by createMethod. |
| 1100 | |
| 1101 | # Validate credentials for the configured universe. |
| 1102 | self._validate_credentials() |
| 1103 | |
| 1104 | for name in kwargs: |
| 1105 | if name not in parameters.argmap: |
| 1106 | raise TypeError("Got an unexpected keyword argument {}".format(name)) |
| 1107 | |
| 1108 | # Remove args that have a value of None. |
| 1109 | keys = list(kwargs.keys()) |
| 1110 | for name in keys: |
| 1111 | if kwargs[name] is None: |
| 1112 | del kwargs[name] |
| 1113 | |
| 1114 | for name in parameters.required_params: |
| 1115 | if name not in kwargs: |
| 1116 | # temporary workaround for non-paging methods incorrectly requiring |
| 1117 | # page token parameter (cf. drive.changes.watch vs. drive.changes.list) |
| 1118 | if name not in _PAGE_TOKEN_NAMES or _findPageTokenName( |
| 1119 | _methodProperties(methodDesc, schema, "response") |
| 1120 | ): |
| 1121 | raise TypeError('Missing required parameter "%s"' % name) |
| 1122 | |
| 1123 | for name, regex in parameters.pattern_params.items(): |
| 1124 | if name in kwargs: |
| 1125 | if isinstance(kwargs[name], str): |
| 1126 | pvalues = [kwargs[name]] |
| 1127 | else: |
| 1128 | pvalues = kwargs[name] |
| 1129 | for pvalue in pvalues: |
| 1130 | if re.match(regex, pvalue) is None: |
| 1131 | raise TypeError( |
| 1132 | 'Parameter "%s" value "%s" does not match the pattern "%s"' |
| 1133 | % (name, pvalue, regex) |
no test coverage detected
searching dependent graphs…