gam7 7.19.3__py3-none-any.whl → 7.28.2__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- gam/__init__.py +1836 -700
- gam/__main__.py +6 -1
- gam/gamlib/glapi.py +35 -19
- gam/gamlib/glcfg.py +16 -0
- gam/gamlib/glclargs.py +294 -5
- gam/gamlib/glentity.py +15 -3
- gam/gamlib/glglobals.py +6 -0
- gam/gamlib/glmsgs.py +5 -2
- gam/gamlib/glskus.py +1 -1
- gam/gamlib/glverlibs.py +1 -1
- gam/gamlib/yubikey.py +13 -12
- {gam7-7.19.3.dist-info → gam7-7.28.2.dist-info}/METADATA +10 -4
- {gam7-7.19.3.dist-info → gam7-7.28.2.dist-info}/RECORD +16 -34
- gam/googleapiclient/__init__.py +0 -27
- gam/googleapiclient/_auth.py +0 -167
- gam/googleapiclient/_helpers.py +0 -207
- gam/googleapiclient/channel.py +0 -315
- gam/googleapiclient/discovery.py +0 -1662
- gam/googleapiclient/discovery_cache/__init__.py +0 -78
- gam/googleapiclient/discovery_cache/appengine_memcache.py +0 -55
- gam/googleapiclient/discovery_cache/base.py +0 -46
- gam/googleapiclient/discovery_cache/file_cache.py +0 -145
- gam/googleapiclient/errors.py +0 -197
- gam/googleapiclient/http.py +0 -1962
- gam/googleapiclient/mimeparse.py +0 -183
- gam/googleapiclient/model.py +0 -429
- gam/googleapiclient/schema.py +0 -317
- gam/googleapiclient/version.py +0 -15
- gam/iso8601/__init__.py +0 -28
- gam/iso8601/iso8601.py +0 -160
- gam/six.py +0 -982
- {gam7-7.19.3.dist-info → gam7-7.28.2.dist-info}/WHEEL +0 -0
- {gam7-7.19.3.dist-info → gam7-7.28.2.dist-info}/entry_points.txt +0 -0
- {gam7-7.19.3.dist-info → gam7-7.28.2.dist-info}/licenses/LICENSE +0 -0
gam/googleapiclient/mimeparse.py
DELETED
|
@@ -1,183 +0,0 @@
|
|
|
1
|
-
# Copyright 2014 Joe Gregorio
|
|
2
|
-
#
|
|
3
|
-
# Licensed under the MIT License
|
|
4
|
-
|
|
5
|
-
"""MIME-Type Parser
|
|
6
|
-
|
|
7
|
-
This module provides basic functions for handling mime-types. It can handle
|
|
8
|
-
matching mime-types against a list of media-ranges. See section 14.1 of the
|
|
9
|
-
HTTP specification [RFC 2616] for a complete explanation.
|
|
10
|
-
|
|
11
|
-
http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.1
|
|
12
|
-
|
|
13
|
-
Contents:
|
|
14
|
-
- parse_mime_type(): Parses a mime-type into its component parts.
|
|
15
|
-
- parse_media_range(): Media-ranges are mime-types with wild-cards and a 'q'
|
|
16
|
-
quality parameter.
|
|
17
|
-
- quality(): Determines the quality ('q') of a mime-type when
|
|
18
|
-
compared against a list of media-ranges.
|
|
19
|
-
- quality_parsed(): Just like quality() except the second parameter must be
|
|
20
|
-
pre-parsed.
|
|
21
|
-
- best_match(): Choose the mime-type with the highest quality ('q')
|
|
22
|
-
from a list of candidates.
|
|
23
|
-
"""
|
|
24
|
-
from __future__ import absolute_import
|
|
25
|
-
|
|
26
|
-
from functools import reduce
|
|
27
|
-
|
|
28
|
-
__version__ = "0.1.3"
|
|
29
|
-
__author__ = "Joe Gregorio"
|
|
30
|
-
__email__ = "joe@bitworking.org"
|
|
31
|
-
__license__ = "MIT License"
|
|
32
|
-
__credits__ = ""
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
def parse_mime_type(mime_type):
|
|
36
|
-
"""Parses a mime-type into its component parts.
|
|
37
|
-
|
|
38
|
-
Carves up a mime-type and returns a tuple of the (type, subtype, params)
|
|
39
|
-
where 'params' is a dictionary of all the parameters for the media range.
|
|
40
|
-
For example, the media range 'application/xhtml;q=0.5' would get parsed
|
|
41
|
-
into:
|
|
42
|
-
|
|
43
|
-
('application', 'xhtml', {'q', '0.5'})
|
|
44
|
-
"""
|
|
45
|
-
parts = mime_type.split(";")
|
|
46
|
-
params = dict(
|
|
47
|
-
[tuple([s.strip() for s in param.split("=", 1)]) for param in parts[1:]]
|
|
48
|
-
)
|
|
49
|
-
full_type = parts[0].strip()
|
|
50
|
-
# Java URLConnection class sends an Accept header that includes a
|
|
51
|
-
# single '*'. Turn it into a legal wildcard.
|
|
52
|
-
if full_type == "*":
|
|
53
|
-
full_type = "*/*"
|
|
54
|
-
(type, subtype) = full_type.split("/")
|
|
55
|
-
|
|
56
|
-
return (type.strip(), subtype.strip(), params)
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
def parse_media_range(range):
|
|
60
|
-
"""Parse a media-range into its component parts.
|
|
61
|
-
|
|
62
|
-
Carves up a media range and returns a tuple of the (type, subtype,
|
|
63
|
-
params) where 'params' is a dictionary of all the parameters for the media
|
|
64
|
-
range. For example, the media range 'application/*;q=0.5' would get parsed
|
|
65
|
-
into:
|
|
66
|
-
|
|
67
|
-
('application', '*', {'q', '0.5'})
|
|
68
|
-
|
|
69
|
-
In addition this function also guarantees that there is a value for 'q'
|
|
70
|
-
in the params dictionary, filling it in with a proper default if
|
|
71
|
-
necessary.
|
|
72
|
-
"""
|
|
73
|
-
(type, subtype, params) = parse_mime_type(range)
|
|
74
|
-
if (
|
|
75
|
-
"q" not in params
|
|
76
|
-
or not params["q"]
|
|
77
|
-
or not float(params["q"])
|
|
78
|
-
or float(params["q"]) > 1
|
|
79
|
-
or float(params["q"]) < 0
|
|
80
|
-
):
|
|
81
|
-
params["q"] = "1"
|
|
82
|
-
|
|
83
|
-
return (type, subtype, params)
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
def fitness_and_quality_parsed(mime_type, parsed_ranges):
|
|
87
|
-
"""Find the best match for a mime-type amongst parsed media-ranges.
|
|
88
|
-
|
|
89
|
-
Find the best match for a given mime-type against a list of media_ranges
|
|
90
|
-
that have already been parsed by parse_media_range(). Returns a tuple of
|
|
91
|
-
the fitness value and the value of the 'q' quality parameter of the best
|
|
92
|
-
match, or (-1, 0) if no match was found. Just as for quality_parsed(),
|
|
93
|
-
'parsed_ranges' must be a list of parsed media ranges.
|
|
94
|
-
"""
|
|
95
|
-
best_fitness = -1
|
|
96
|
-
best_fit_q = 0
|
|
97
|
-
(target_type, target_subtype, target_params) = parse_media_range(mime_type)
|
|
98
|
-
for (type, subtype, params) in parsed_ranges:
|
|
99
|
-
type_match = type == target_type or type == "*" or target_type == "*"
|
|
100
|
-
subtype_match = (
|
|
101
|
-
subtype == target_subtype or subtype == "*" or target_subtype == "*"
|
|
102
|
-
)
|
|
103
|
-
if type_match and subtype_match:
|
|
104
|
-
param_matches = reduce(
|
|
105
|
-
lambda x, y: x + y,
|
|
106
|
-
[
|
|
107
|
-
1
|
|
108
|
-
for (key, value) in target_params.items()
|
|
109
|
-
if key != "q" and key in params and value == params[key]
|
|
110
|
-
],
|
|
111
|
-
0,
|
|
112
|
-
)
|
|
113
|
-
fitness = (type == target_type) and 100 or 0
|
|
114
|
-
fitness += (subtype == target_subtype) and 10 or 0
|
|
115
|
-
fitness += param_matches
|
|
116
|
-
if fitness > best_fitness:
|
|
117
|
-
best_fitness = fitness
|
|
118
|
-
best_fit_q = params["q"]
|
|
119
|
-
|
|
120
|
-
return best_fitness, float(best_fit_q)
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
def quality_parsed(mime_type, parsed_ranges):
|
|
124
|
-
"""Find the best match for a mime-type amongst parsed media-ranges.
|
|
125
|
-
|
|
126
|
-
Find the best match for a given mime-type against a list of media_ranges
|
|
127
|
-
that have already been parsed by parse_media_range(). Returns the 'q'
|
|
128
|
-
quality parameter of the best match, 0 if no match was found. This function
|
|
129
|
-
bahaves the same as quality() except that 'parsed_ranges' must be a list of
|
|
130
|
-
parsed media ranges.
|
|
131
|
-
"""
|
|
132
|
-
|
|
133
|
-
return fitness_and_quality_parsed(mime_type, parsed_ranges)[1]
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
def quality(mime_type, ranges):
|
|
137
|
-
"""Return the quality ('q') of a mime-type against a list of media-ranges.
|
|
138
|
-
|
|
139
|
-
Returns the quality 'q' of a mime-type when compared against the
|
|
140
|
-
media-ranges in ranges. For example:
|
|
141
|
-
|
|
142
|
-
>>> quality('text/html','text/*;q=0.3, text/html;q=0.7,
|
|
143
|
-
text/html;level=1, text/html;level=2;q=0.4, */*;q=0.5')
|
|
144
|
-
0.7
|
|
145
|
-
|
|
146
|
-
"""
|
|
147
|
-
parsed_ranges = [parse_media_range(r) for r in ranges.split(",")]
|
|
148
|
-
|
|
149
|
-
return quality_parsed(mime_type, parsed_ranges)
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
def best_match(supported, header):
|
|
153
|
-
"""Return mime-type with the highest quality ('q') from list of candidates.
|
|
154
|
-
|
|
155
|
-
Takes a list of supported mime-types and finds the best match for all the
|
|
156
|
-
media-ranges listed in header. The value of header must be a string that
|
|
157
|
-
conforms to the format of the HTTP Accept: header. The value of 'supported'
|
|
158
|
-
is a list of mime-types. The list of supported mime-types should be sorted
|
|
159
|
-
in order of increasing desirability, in case of a situation where there is
|
|
160
|
-
a tie.
|
|
161
|
-
|
|
162
|
-
>>> best_match(['application/xbel+xml', 'text/xml'],
|
|
163
|
-
'text/*;q=0.5,*/*; q=0.1')
|
|
164
|
-
'text/xml'
|
|
165
|
-
"""
|
|
166
|
-
split_header = _filter_blank(header.split(","))
|
|
167
|
-
parsed_header = [parse_media_range(r) for r in split_header]
|
|
168
|
-
weighted_matches = []
|
|
169
|
-
pos = 0
|
|
170
|
-
for mime_type in supported:
|
|
171
|
-
weighted_matches.append(
|
|
172
|
-
(fitness_and_quality_parsed(mime_type, parsed_header), pos, mime_type)
|
|
173
|
-
)
|
|
174
|
-
pos += 1
|
|
175
|
-
weighted_matches.sort()
|
|
176
|
-
|
|
177
|
-
return weighted_matches[-1][0][1] and weighted_matches[-1][2] or ""
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
def _filter_blank(i):
|
|
181
|
-
for s in i:
|
|
182
|
-
if s.strip():
|
|
183
|
-
yield s
|
gam/googleapiclient/model.py
DELETED
|
@@ -1,429 +0,0 @@
|
|
|
1
|
-
# Copyright 2014 Google Inc. All Rights Reserved.
|
|
2
|
-
#
|
|
3
|
-
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
4
|
-
# you may not use this file except in compliance with the License.
|
|
5
|
-
# You may obtain a copy of the License at
|
|
6
|
-
#
|
|
7
|
-
# http://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
-
#
|
|
9
|
-
# Unless required by applicable law or agreed to in writing, software
|
|
10
|
-
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
11
|
-
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
|
-
# See the License for the specific language governing permissions and
|
|
13
|
-
# limitations under the License.
|
|
14
|
-
|
|
15
|
-
"""Model objects for requests and responses.
|
|
16
|
-
|
|
17
|
-
Each API may support one or more serializations, such
|
|
18
|
-
as JSON, Atom, etc. The model classes are responsible
|
|
19
|
-
for converting between the wire format and the Python
|
|
20
|
-
object representation.
|
|
21
|
-
"""
|
|
22
|
-
from __future__ import absolute_import
|
|
23
|
-
|
|
24
|
-
__author__ = "jcgregorio@google.com (Joe Gregorio)"
|
|
25
|
-
|
|
26
|
-
import json
|
|
27
|
-
import logging
|
|
28
|
-
import platform
|
|
29
|
-
import urllib
|
|
30
|
-
import warnings
|
|
31
|
-
|
|
32
|
-
from googleapiclient import version as googleapiclient_version
|
|
33
|
-
from googleapiclient.errors import HttpError
|
|
34
|
-
|
|
35
|
-
try:
|
|
36
|
-
from google.api_core.version_header import API_VERSION_METADATA_KEY
|
|
37
|
-
|
|
38
|
-
HAS_API_VERSION = True
|
|
39
|
-
except ImportError:
|
|
40
|
-
HAS_API_VERSION = False
|
|
41
|
-
|
|
42
|
-
_LIBRARY_VERSION = googleapiclient_version.__version__
|
|
43
|
-
_PY_VERSION = platform.python_version()
|
|
44
|
-
|
|
45
|
-
LOGGER = logging.getLogger(__name__)
|
|
46
|
-
|
|
47
|
-
dump_request_response = False
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
def _abstract():
|
|
51
|
-
raise NotImplementedError("You need to override this function")
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
class Model(object):
|
|
55
|
-
"""Model base class.
|
|
56
|
-
|
|
57
|
-
All Model classes should implement this interface.
|
|
58
|
-
The Model serializes and de-serializes between a wire
|
|
59
|
-
format such as JSON and a Python object representation.
|
|
60
|
-
"""
|
|
61
|
-
|
|
62
|
-
def request(self, headers, path_params, query_params, body_value):
|
|
63
|
-
"""Updates outgoing requests with a serialized body.
|
|
64
|
-
|
|
65
|
-
Args:
|
|
66
|
-
headers: dict, request headers
|
|
67
|
-
path_params: dict, parameters that appear in the request path
|
|
68
|
-
query_params: dict, parameters that appear in the query
|
|
69
|
-
body_value: object, the request body as a Python object, which must be
|
|
70
|
-
serializable.
|
|
71
|
-
Returns:
|
|
72
|
-
A tuple of (headers, path_params, query, body)
|
|
73
|
-
|
|
74
|
-
headers: dict, request headers
|
|
75
|
-
path_params: dict, parameters that appear in the request path
|
|
76
|
-
query: string, query part of the request URI
|
|
77
|
-
body: string, the body serialized in the desired wire format.
|
|
78
|
-
"""
|
|
79
|
-
_abstract()
|
|
80
|
-
|
|
81
|
-
def response(self, resp, content):
|
|
82
|
-
"""Convert the response wire format into a Python object.
|
|
83
|
-
|
|
84
|
-
Args:
|
|
85
|
-
resp: httplib2.Response, the HTTP response headers and status
|
|
86
|
-
content: string, the body of the HTTP response
|
|
87
|
-
|
|
88
|
-
Returns:
|
|
89
|
-
The body de-serialized as a Python object.
|
|
90
|
-
|
|
91
|
-
Raises:
|
|
92
|
-
googleapiclient.errors.HttpError if a non 2xx response is received.
|
|
93
|
-
"""
|
|
94
|
-
_abstract()
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
class BaseModel(Model):
|
|
98
|
-
"""Base model class.
|
|
99
|
-
|
|
100
|
-
Subclasses should provide implementations for the "serialize" and
|
|
101
|
-
"deserialize" methods, as well as values for the following class attributes.
|
|
102
|
-
|
|
103
|
-
Attributes:
|
|
104
|
-
accept: The value to use for the HTTP Accept header.
|
|
105
|
-
content_type: The value to use for the HTTP Content-type header.
|
|
106
|
-
no_content_response: The value to return when deserializing a 204 "No
|
|
107
|
-
Content" response.
|
|
108
|
-
alt_param: The value to supply as the "alt" query parameter for requests.
|
|
109
|
-
"""
|
|
110
|
-
|
|
111
|
-
accept = None
|
|
112
|
-
content_type = None
|
|
113
|
-
no_content_response = None
|
|
114
|
-
alt_param = None
|
|
115
|
-
|
|
116
|
-
def _log_request(self, headers, path_params, query, body):
|
|
117
|
-
"""Logs debugging information about the request if requested."""
|
|
118
|
-
if dump_request_response:
|
|
119
|
-
LOGGER.info("--request-start--")
|
|
120
|
-
LOGGER.info("-headers-start-")
|
|
121
|
-
for h, v in headers.items():
|
|
122
|
-
LOGGER.info("%s: %s", h, v)
|
|
123
|
-
LOGGER.info("-headers-end-")
|
|
124
|
-
LOGGER.info("-path-parameters-start-")
|
|
125
|
-
for h, v in path_params.items():
|
|
126
|
-
LOGGER.info("%s: %s", h, v)
|
|
127
|
-
LOGGER.info("-path-parameters-end-")
|
|
128
|
-
LOGGER.info("body: %s", body)
|
|
129
|
-
LOGGER.info("query: %s", query)
|
|
130
|
-
LOGGER.info("--request-end--")
|
|
131
|
-
|
|
132
|
-
def request(self, headers, path_params, query_params, body_value, api_version=None):
|
|
133
|
-
"""Updates outgoing requests with a serialized body.
|
|
134
|
-
|
|
135
|
-
Args:
|
|
136
|
-
headers: dict, request headers
|
|
137
|
-
path_params: dict, parameters that appear in the request path
|
|
138
|
-
query_params: dict, parameters that appear in the query
|
|
139
|
-
body_value: object, the request body as a Python object, which must be
|
|
140
|
-
serializable by json.
|
|
141
|
-
api_version: str, The precise API version represented by this request,
|
|
142
|
-
which will result in an API Version header being sent along with the
|
|
143
|
-
HTTP request.
|
|
144
|
-
Returns:
|
|
145
|
-
A tuple of (headers, path_params, query, body)
|
|
146
|
-
|
|
147
|
-
headers: dict, request headers
|
|
148
|
-
path_params: dict, parameters that appear in the request path
|
|
149
|
-
query: string, query part of the request URI
|
|
150
|
-
body: string, the body serialized as JSON
|
|
151
|
-
"""
|
|
152
|
-
query = self._build_query(query_params)
|
|
153
|
-
headers["accept"] = self.accept
|
|
154
|
-
headers["accept-encoding"] = "gzip, deflate"
|
|
155
|
-
if "user-agent" in headers:
|
|
156
|
-
headers["user-agent"] += " "
|
|
157
|
-
else:
|
|
158
|
-
headers["user-agent"] = ""
|
|
159
|
-
headers["user-agent"] += "(gzip)"
|
|
160
|
-
if "x-goog-api-client" in headers:
|
|
161
|
-
headers["x-goog-api-client"] += " "
|
|
162
|
-
else:
|
|
163
|
-
headers["x-goog-api-client"] = ""
|
|
164
|
-
headers["x-goog-api-client"] += "gdcl/%s gl-python/%s" % (
|
|
165
|
-
_LIBRARY_VERSION,
|
|
166
|
-
_PY_VERSION,
|
|
167
|
-
)
|
|
168
|
-
|
|
169
|
-
if api_version and HAS_API_VERSION:
|
|
170
|
-
headers[API_VERSION_METADATA_KEY] = api_version
|
|
171
|
-
elif api_version:
|
|
172
|
-
warnings.warn(
|
|
173
|
-
"The `api_version` argument is ignored as a newer version of "
|
|
174
|
-
"`google-api-core` is required to use this feature."
|
|
175
|
-
"Please upgrade `google-api-core` to 2.19.0 or newer."
|
|
176
|
-
)
|
|
177
|
-
|
|
178
|
-
if body_value is not None:
|
|
179
|
-
headers["content-type"] = self.content_type
|
|
180
|
-
body_value = self.serialize(body_value)
|
|
181
|
-
self._log_request(headers, path_params, query, body_value)
|
|
182
|
-
return (headers, path_params, query, body_value)
|
|
183
|
-
|
|
184
|
-
def _build_query(self, params):
|
|
185
|
-
"""Builds a query string.
|
|
186
|
-
|
|
187
|
-
Args:
|
|
188
|
-
params: dict, the query parameters
|
|
189
|
-
|
|
190
|
-
Returns:
|
|
191
|
-
The query parameters properly encoded into an HTTP URI query string.
|
|
192
|
-
"""
|
|
193
|
-
if self.alt_param is not None:
|
|
194
|
-
params.update({"alt": self.alt_param})
|
|
195
|
-
astuples = []
|
|
196
|
-
for key, value in params.items():
|
|
197
|
-
if type(value) == type([]):
|
|
198
|
-
for x in value:
|
|
199
|
-
x = x.encode("utf-8")
|
|
200
|
-
astuples.append((key, x))
|
|
201
|
-
else:
|
|
202
|
-
if isinstance(value, str) and callable(value.encode):
|
|
203
|
-
value = value.encode("utf-8")
|
|
204
|
-
astuples.append((key, value))
|
|
205
|
-
return "?" + urllib.parse.urlencode(astuples)
|
|
206
|
-
|
|
207
|
-
def _log_response(self, resp, content):
|
|
208
|
-
"""Logs debugging information about the response if requested."""
|
|
209
|
-
if dump_request_response:
|
|
210
|
-
LOGGER.info("--response-start--")
|
|
211
|
-
for h, v in resp.items():
|
|
212
|
-
LOGGER.info("%s: %s", h, v)
|
|
213
|
-
if content:
|
|
214
|
-
LOGGER.info(content)
|
|
215
|
-
LOGGER.info("--response-end--")
|
|
216
|
-
|
|
217
|
-
def response(self, resp, content):
|
|
218
|
-
"""Convert the response wire format into a Python object.
|
|
219
|
-
|
|
220
|
-
Args:
|
|
221
|
-
resp: httplib2.Response, the HTTP response headers and status
|
|
222
|
-
content: string, the body of the HTTP response
|
|
223
|
-
|
|
224
|
-
Returns:
|
|
225
|
-
The body de-serialized as a Python object.
|
|
226
|
-
|
|
227
|
-
Raises:
|
|
228
|
-
googleapiclient.errors.HttpError if a non 2xx response is received.
|
|
229
|
-
"""
|
|
230
|
-
self._log_response(resp, content)
|
|
231
|
-
# Error handling is TBD, for example, do we retry
|
|
232
|
-
# for some operation/error combinations?
|
|
233
|
-
if resp.status < 300:
|
|
234
|
-
if resp.status == 204:
|
|
235
|
-
# A 204: No Content response should be treated differently
|
|
236
|
-
# to all the other success states
|
|
237
|
-
return self.no_content_response
|
|
238
|
-
return self.deserialize(content)
|
|
239
|
-
else:
|
|
240
|
-
LOGGER.debug("Content from bad request was: %r" % content)
|
|
241
|
-
raise HttpError(resp, content)
|
|
242
|
-
|
|
243
|
-
def serialize(self, body_value):
|
|
244
|
-
"""Perform the actual Python object serialization.
|
|
245
|
-
|
|
246
|
-
Args:
|
|
247
|
-
body_value: object, the request body as a Python object.
|
|
248
|
-
|
|
249
|
-
Returns:
|
|
250
|
-
string, the body in serialized form.
|
|
251
|
-
"""
|
|
252
|
-
_abstract()
|
|
253
|
-
|
|
254
|
-
def deserialize(self, content):
|
|
255
|
-
"""Perform the actual deserialization from response string to Python
|
|
256
|
-
object.
|
|
257
|
-
|
|
258
|
-
Args:
|
|
259
|
-
content: string, the body of the HTTP response
|
|
260
|
-
|
|
261
|
-
Returns:
|
|
262
|
-
The body de-serialized as a Python object.
|
|
263
|
-
"""
|
|
264
|
-
_abstract()
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
class JsonModel(BaseModel):
|
|
268
|
-
"""Model class for JSON.
|
|
269
|
-
|
|
270
|
-
Serializes and de-serializes between JSON and the Python
|
|
271
|
-
object representation of HTTP request and response bodies.
|
|
272
|
-
"""
|
|
273
|
-
|
|
274
|
-
accept = "application/json"
|
|
275
|
-
content_type = "application/json"
|
|
276
|
-
alt_param = "json"
|
|
277
|
-
|
|
278
|
-
def __init__(self, data_wrapper=False):
|
|
279
|
-
"""Construct a JsonModel.
|
|
280
|
-
|
|
281
|
-
Args:
|
|
282
|
-
data_wrapper: boolean, wrap requests and responses in a data wrapper
|
|
283
|
-
"""
|
|
284
|
-
self._data_wrapper = data_wrapper
|
|
285
|
-
|
|
286
|
-
def serialize(self, body_value):
|
|
287
|
-
if (
|
|
288
|
-
isinstance(body_value, dict)
|
|
289
|
-
and "data" not in body_value
|
|
290
|
-
and self._data_wrapper
|
|
291
|
-
):
|
|
292
|
-
body_value = {"data": body_value}
|
|
293
|
-
return json.dumps(body_value)
|
|
294
|
-
|
|
295
|
-
def deserialize(self, content):
|
|
296
|
-
try:
|
|
297
|
-
content = content.decode("utf-8")
|
|
298
|
-
except AttributeError:
|
|
299
|
-
pass
|
|
300
|
-
try:
|
|
301
|
-
body = json.loads(content)
|
|
302
|
-
except json.decoder.JSONDecodeError:
|
|
303
|
-
body = content
|
|
304
|
-
else:
|
|
305
|
-
if self._data_wrapper and "data" in body:
|
|
306
|
-
body = body["data"]
|
|
307
|
-
return body
|
|
308
|
-
|
|
309
|
-
@property
|
|
310
|
-
def no_content_response(self):
|
|
311
|
-
return {}
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
class RawModel(JsonModel):
|
|
315
|
-
"""Model class for requests that don't return JSON.
|
|
316
|
-
|
|
317
|
-
Serializes and de-serializes between JSON and the Python
|
|
318
|
-
object representation of HTTP request, and returns the raw bytes
|
|
319
|
-
of the response body.
|
|
320
|
-
"""
|
|
321
|
-
|
|
322
|
-
accept = "*/*"
|
|
323
|
-
content_type = "application/json"
|
|
324
|
-
alt_param = None
|
|
325
|
-
|
|
326
|
-
def deserialize(self, content):
|
|
327
|
-
return content
|
|
328
|
-
|
|
329
|
-
@property
|
|
330
|
-
def no_content_response(self):
|
|
331
|
-
return ""
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
class MediaModel(JsonModel):
|
|
335
|
-
"""Model class for requests that return Media.
|
|
336
|
-
|
|
337
|
-
Serializes and de-serializes between JSON and the Python
|
|
338
|
-
object representation of HTTP request, and returns the raw bytes
|
|
339
|
-
of the response body.
|
|
340
|
-
"""
|
|
341
|
-
|
|
342
|
-
accept = "*/*"
|
|
343
|
-
content_type = "application/json"
|
|
344
|
-
alt_param = "media"
|
|
345
|
-
|
|
346
|
-
def deserialize(self, content):
|
|
347
|
-
return content
|
|
348
|
-
|
|
349
|
-
@property
|
|
350
|
-
def no_content_response(self):
|
|
351
|
-
return ""
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
class ProtocolBufferModel(BaseModel):
|
|
355
|
-
"""Model class for protocol buffers.
|
|
356
|
-
|
|
357
|
-
Serializes and de-serializes the binary protocol buffer sent in the HTTP
|
|
358
|
-
request and response bodies.
|
|
359
|
-
"""
|
|
360
|
-
|
|
361
|
-
accept = "application/x-protobuf"
|
|
362
|
-
content_type = "application/x-protobuf"
|
|
363
|
-
alt_param = "proto"
|
|
364
|
-
|
|
365
|
-
def __init__(self, protocol_buffer):
|
|
366
|
-
"""Constructs a ProtocolBufferModel.
|
|
367
|
-
|
|
368
|
-
The serialized protocol buffer returned in an HTTP response will be
|
|
369
|
-
de-serialized using the given protocol buffer class.
|
|
370
|
-
|
|
371
|
-
Args:
|
|
372
|
-
protocol_buffer: The protocol buffer class used to de-serialize a
|
|
373
|
-
response from the API.
|
|
374
|
-
"""
|
|
375
|
-
self._protocol_buffer = protocol_buffer
|
|
376
|
-
|
|
377
|
-
def serialize(self, body_value):
|
|
378
|
-
return body_value.SerializeToString()
|
|
379
|
-
|
|
380
|
-
def deserialize(self, content):
|
|
381
|
-
return self._protocol_buffer.FromString(content)
|
|
382
|
-
|
|
383
|
-
@property
|
|
384
|
-
def no_content_response(self):
|
|
385
|
-
return self._protocol_buffer()
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
def makepatch(original, modified):
|
|
389
|
-
"""Create a patch object.
|
|
390
|
-
|
|
391
|
-
Some methods support PATCH, an efficient way to send updates to a resource.
|
|
392
|
-
This method allows the easy construction of patch bodies by looking at the
|
|
393
|
-
differences between a resource before and after it was modified.
|
|
394
|
-
|
|
395
|
-
Args:
|
|
396
|
-
original: object, the original deserialized resource
|
|
397
|
-
modified: object, the modified deserialized resource
|
|
398
|
-
Returns:
|
|
399
|
-
An object that contains only the changes from original to modified, in a
|
|
400
|
-
form suitable to pass to a PATCH method.
|
|
401
|
-
|
|
402
|
-
Example usage:
|
|
403
|
-
item = service.activities().get(postid=postid, userid=userid).execute()
|
|
404
|
-
original = copy.deepcopy(item)
|
|
405
|
-
item['object']['content'] = 'This is updated.'
|
|
406
|
-
service.activities.patch(postid=postid, userid=userid,
|
|
407
|
-
body=makepatch(original, item)).execute()
|
|
408
|
-
"""
|
|
409
|
-
patch = {}
|
|
410
|
-
for key, original_value in original.items():
|
|
411
|
-
modified_value = modified.get(key, None)
|
|
412
|
-
if modified_value is None:
|
|
413
|
-
# Use None to signal that the element is deleted
|
|
414
|
-
patch[key] = None
|
|
415
|
-
elif original_value != modified_value:
|
|
416
|
-
if type(original_value) == type({}):
|
|
417
|
-
# Recursively descend objects
|
|
418
|
-
patch[key] = makepatch(original_value, modified_value)
|
|
419
|
-
else:
|
|
420
|
-
# In the case of simple types or arrays we just replace
|
|
421
|
-
patch[key] = modified_value
|
|
422
|
-
else:
|
|
423
|
-
# Don't add anything to patch if there's no change
|
|
424
|
-
pass
|
|
425
|
-
for key in modified:
|
|
426
|
-
if key not in original:
|
|
427
|
-
patch[key] = modified[key]
|
|
428
|
-
|
|
429
|
-
return patch
|