@@ -133,31 +133,59 @@ class {{ service.name }}RestTransport({{ service.name }}Transport):
133133 {% - endif %}
134134 """
135135
136- {% - if 'body' in method .http_opt .keys () %}
137- # Jsonify the input
138- data = {{ method.output.ident }}.to_json(
139- {% - if method .http_opt ['body' ] == '*' %}
136+ {# TODO(yon-mg): refactor when implementing grpc transcoding
137+ - parse request pb & assign body, path params
138+ - shove leftovers into query params
139+ - make sure dotted nested fields preserved
140+ - format url and send the request
141+ #}
142+ {% - if 'body' in method .http_opt %}
143+ # Jsonify the request body
144+ {% - if method .http_opt ['body' ] != '*' %}
145+ body = {{ method.input.fields[method.http_opt['body']] .type.ident }}.to_json(
146+ request.{{ method.http_opt['body'] }},
147+ including_default_value_fields=False
148+ )
149+ {% - else %}
150+ body = {{ method.input.ident }}.to_json(
140151 request
141- {% - else %}
142- request.body
143- {% - endif %}
144152 )
145153 {% - endif %}
154+ {% - endif %}
146155
147156 {# TODO(yon-mg): Write helper method for handling grpc transcoding url #}
148157 # TODO(yon-mg): need to handle grpc transcoding and parse url correctly
149- # current impl assumes simpler version of grpc transcoding
150- # Send the request
158+ # current impl assumes basic case of grpc transcoding
151159 url = 'https://{host}{{ method.http_opt['url'] }}'.format(
152160 host=self._host,
153- {% - for field in method .input . fields . keys () %}
161+ {% - for field in method .path_params %}
154162 {{ field }}=request.{{ field }},
155163 {% - endfor %}
156164 )
165+
166+ {# TODO(yon-mg): move all query param logic out of wrappers into here to handle
167+ nested fields correctly (can't just use set of top level fields
168+ #}
169+ # TODO(yon-mg): handle nested fields corerctly rather than using only top level fields
170+ # not required for GCE
171+ query_params = {
172+ {% - for field in method .query_params %}
173+ '{{ field|camel_case }}': request.{{ field }},
174+ {% - endfor %}
175+ }
176+ # TODO(yon-mg): further discussion needed whether 'python truthiness' is appropriate here
177+ # discards default values
178+ # TODO(yon-mg): add test for proper url encoded strings
179+ query_params = ((k, v) for k, v in query_params.items() if v)
180+ for i, (param_name, param_value) in enumerate(query_params):
181+ q = '?' if i == 0 else '& '
182+ url += "{q}{name}={value}".format(q=q, name=param_name, value=param_value.replace(' ', '+'))
183+
184+ # Send the request
157185 {% if not method .void %} response = {% endif %} self._session.{{ method.http_opt['verb'] }}(
158- url,
159- {% - if 'body' in method .http_opt . keys () %}
160- json=data ,
186+ url
187+ {% - if 'body' in method .http_opt %} ,
188+ json=body ,
161189 {% - endif %}
162190 )
163191 {% - if not method .void %}
0 commit comments