Skip to content

Commit 42f40b5

Browse files
committed
google-api-go-generator: respect HttpBody for GET requests
CL 39310 didn't generate *http.Response return values for GET requests, only for those requests that had an HttpBody request body. Change-Id: Ibfabfdf27075391e0bd15f047d79ab585813cfc2 Reviewed-on: https://code-review.googlesource.com/c/google-api-go-client/+/40590 Reviewed-by: Tyler Bui-Palsulich <[email protected]> Reviewed-by: Chris Broadfoot <[email protected]> Reviewed-by: kokoro <[email protected]>
1 parent 358d122 commit 42f40b5

File tree

3 files changed

+184
-7
lines changed

3 files changed

+184
-7
lines changed

google-api-go-generator/gen.go

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1688,7 +1688,7 @@ func (meth *Method) generateCode() {
16881688
pn("\n// method id %q:", meth.Id())
16891689

16901690
retType := responseType(a, meth.m)
1691-
if meth.IsRawHTTP() {
1691+
if meth.IsRawResponse() {
16921692
retType = "*http.Response"
16931693
}
16941694
retTypeComma := retType
@@ -1916,7 +1916,7 @@ func (meth *Method) generateCode() {
19161916
pn("}")
19171917
}
19181918
pn("var body io.Reader = nil")
1919-
if meth.IsRawHTTP() {
1919+
if meth.IsRawRequest() {
19201920
pn("body = c.body_")
19211921
} else {
19221922
if ba := args.bodyArg(); ba != nil && httpMethod != "GET" {
@@ -1996,7 +1996,7 @@ func (meth *Method) generateCode() {
19961996

19971997
mapRetType := strings.HasPrefix(retTypeComma, "map[")
19981998
pn("\n// Do executes the %q call.", meth.m.ID)
1999-
if retTypeComma != "" && !mapRetType && !meth.IsRawHTTP() {
1999+
if retTypeComma != "" && !mapRetType && !meth.IsRawResponse() {
20002000
commentFmtStr := "Exactly one of %v or error will be non-nil. " +
20012001
"Any non-2xx status code is an error. " +
20022002
"Response headers are in either %v.ServerResponse.Header " +
@@ -2012,7 +2012,7 @@ func (meth *Method) generateCode() {
20122012
nilRet = "nil, "
20132013
}
20142014
pn(`gensupport.SetOptions(c.urlParams_, opts...)`)
2015-
if meth.IsRawHTTP() {
2015+
if meth.IsRawResponse() {
20162016
pn(`return c.doRequest("")`)
20172017
} else {
20182018
pn(`res, err := c.doRequest("json")`)
@@ -2187,12 +2187,30 @@ func resolveRelative(basestr, relstr string) string {
21872187
return u.String()
21882188
}
21892189

2190-
func (meth *Method) IsRawHTTP() bool {
2190+
func (meth *Method) IsRawRequest() bool {
21912191
if meth.m.Request == nil {
21922192
return false
21932193
}
21942194
// TODO(cbro): enable across other APIs.
2195-
return meth.api.Name == "healthcare" && meth.m.Request.Ref == "HttpBody"
2195+
if meth.api.Name != "healthcare" {
2196+
return false
2197+
}
2198+
return meth.m.Request.Ref == "HttpBody"
2199+
}
2200+
2201+
func (meth *Method) IsRawResponse() bool {
2202+
if meth.m.Response == nil {
2203+
return false
2204+
}
2205+
if meth.IsRawRequest() {
2206+
// always match raw requests with raw responses.
2207+
return true
2208+
}
2209+
// TODO(cbro): enable across other APIs.
2210+
if meth.api.Name != "healthcare" {
2211+
return false
2212+
}
2213+
return meth.m.Response.Ref == "HttpBody"
21962214
}
21972215

21982216
func (meth *Method) NewArguments() *arguments {
@@ -2213,7 +2231,7 @@ func (meth *Method) NewArguments() *arguments {
22132231
args.AddArg(arg)
22142232
}
22152233
if rs := meth.m.Request; rs != nil {
2216-
if meth.IsRawHTTP() {
2234+
if meth.IsRawRequest() {
22172235
args.AddArg(&argument{
22182236
goname: "body_",
22192237
gotype: "io.Reader",

google-api-go-generator/testdata/http-body.json

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,31 @@
4848
"flatPath": "v1beta1/projects/{projectsId}/locations/{locationsId}/datasets/{datasetsId}/fhirStores/{fhirStoresId}/fhir/{fhirId}",
4949
"path": "v1beta1/{+parent}/fhir/{+type}",
5050
"id": "healthcare.projects.locations.datasets.fhirStores.fhir.createResource"
51+
},
52+
"read": {
53+
"description": "Gets the contents of a FHIR resource.\n\nImplements the FHIR standard [read\ninteraction](http://hl7.org/implement/standards/fhir/STU3/http.html#read).\n\nAlso supports the FHIR standard [conditional read\ninteraction](http://hl7.org/implement/standards/fhir/STU3/http.html#cread)\nspecified by supplying an `If-Modified-Since` header with a date/time value\nor an `If-None-Match` header with an ETag value.\n\nOn success, the response body will contain a JSON-encoded representation\nof the resource.\nErrors generated by the FHIR store will contain a JSON-encoded\n`OperationOutcome` resource describing the reason for the error. If the\nrequest cannot be mapped to a valid API method on a FHIR store, a generic\nGCP error might be returned instead.",
54+
"flatPath": "v1beta1/projects/{projectsId}/locations/{locationsId}/datasets/{datasetsId}/fhirStores/{fhirStoresId}/fhir/{fhirId}/{fhirId1}",
55+
"httpMethod": "GET",
56+
"id": "healthcare.projects.locations.datasets.fhirStores.fhir.read",
57+
"parameterOrder": [
58+
"name"
59+
],
60+
"parameters": {
61+
"name": {
62+
"description": "The name of the resource to retrieve.",
63+
"location": "path",
64+
"pattern": "^projects/[^/]+/locations/[^/]+/datasets/[^/]+/fhirStores/[^/]+/fhir/[^/]+/[^/]+$",
65+
"required": true,
66+
"type": "string"
67+
}
68+
},
69+
"path": "v1beta1/{+name}",
70+
"response": {
71+
"$ref": "HttpBody"
72+
},
73+
"scopes": [
74+
"https://www.googleapis.com/auth/cloud-platform"
75+
]
5176
}
5277
}
5378
}

google-api-go-generator/testdata/http-body.want

Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -384,3 +384,137 @@ func (c *ProjectsLocationsDatasetsFhirStoresFhirCreateResourceCall) Do(opts ...g
384384
// }
385385

386386
}
387+
388+
// method id "healthcare.projects.locations.datasets.fhirStores.fhir.read":
389+
390+
type ProjectsLocationsDatasetsFhirStoresFhirReadCall struct {
391+
s *Service
392+
name string
393+
urlParams_ gensupport.URLParams
394+
ifNoneMatch_ string
395+
ctx_ context.Context
396+
header_ http.Header
397+
}
398+
399+
// Read: Gets the contents of a FHIR resource.
400+
//
401+
// Implements the FHIR standard
402+
// [read
403+
// interaction](http://hl7.org/implement/standards/fhir/STU3/http.h
404+
// tml#read).
405+
//
406+
// Also supports the FHIR standard [conditional
407+
// read
408+
// interaction](http://hl7.org/implement/standards/fhir/STU3/http.ht
409+
// ml#cread)
410+
// specified by supplying an `If-Modified-Since` header with a date/time
411+
// value
412+
// or an `If-None-Match` header with an ETag value.
413+
//
414+
// On success, the response body will contain a JSON-encoded
415+
// representation
416+
// of the resource.
417+
// Errors generated by the FHIR store will contain a
418+
// JSON-encoded
419+
// `OperationOutcome` resource describing the reason for the error. If
420+
// the
421+
// request cannot be mapped to a valid API method on a FHIR store, a
422+
// generic
423+
// GCP error might be returned instead.
424+
func (r *ProjectsLocationsDatasetsFhirStoresFhirService) Read(name string) *ProjectsLocationsDatasetsFhirStoresFhirReadCall {
425+
c := &ProjectsLocationsDatasetsFhirStoresFhirReadCall{s: r.s, urlParams_: make(gensupport.URLParams)}
426+
c.name = name
427+
return c
428+
}
429+
430+
// Fields allows partial responses to be retrieved. See
431+
// https://developers.google.com/gdata/docs/2.0/basics#PartialResponse
432+
// for more information.
433+
func (c *ProjectsLocationsDatasetsFhirStoresFhirReadCall) Fields(s ...googleapi.Field) *ProjectsLocationsDatasetsFhirStoresFhirReadCall {
434+
c.urlParams_.Set("fields", googleapi.CombineFields(s))
435+
return c
436+
}
437+
438+
// IfNoneMatch sets the optional parameter which makes the operation
439+
// fail if the object's ETag matches the given value. This is useful for
440+
// getting updates only after the object has changed since the last
441+
// request. Use googleapi.IsNotModified to check whether the response
442+
// error from Do is the result of In-None-Match.
443+
func (c *ProjectsLocationsDatasetsFhirStoresFhirReadCall) IfNoneMatch(entityTag string) *ProjectsLocationsDatasetsFhirStoresFhirReadCall {
444+
c.ifNoneMatch_ = entityTag
445+
return c
446+
}
447+
448+
// Context sets the context to be used in this call's Do method. Any
449+
// pending HTTP request will be aborted if the provided context is
450+
// canceled.
451+
func (c *ProjectsLocationsDatasetsFhirStoresFhirReadCall) Context(ctx context.Context) *ProjectsLocationsDatasetsFhirStoresFhirReadCall {
452+
c.ctx_ = ctx
453+
return c
454+
}
455+
456+
// Header returns an http.Header that can be modified by the caller to
457+
// add HTTP headers to the request.
458+
func (c *ProjectsLocationsDatasetsFhirStoresFhirReadCall) Header() http.Header {
459+
if c.header_ == nil {
460+
c.header_ = make(http.Header)
461+
}
462+
return c.header_
463+
}
464+
465+
func (c *ProjectsLocationsDatasetsFhirStoresFhirReadCall) doRequest(alt string) (*http.Response, error) {
466+
reqHeaders := make(http.Header)
467+
for k, v := range c.header_ {
468+
reqHeaders[k] = v
469+
}
470+
reqHeaders.Set("User-Agent", c.s.userAgent())
471+
if c.ifNoneMatch_ != "" {
472+
reqHeaders.Set("If-None-Match", c.ifNoneMatch_)
473+
}
474+
var body io.Reader = nil
475+
c.urlParams_.Set("alt", alt)
476+
c.urlParams_.Set("prettyPrint", "false")
477+
urls := googleapi.ResolveRelative(c.s.BasePath, "v1beta1/{+name}")
478+
urls += "?" + c.urlParams_.Encode()
479+
req, err := http.NewRequest("GET", urls, body)
480+
if err != nil {
481+
return nil, err
482+
}
483+
req.Header = reqHeaders
484+
googleapi.Expand(req.URL, map[string]string{
485+
"name": c.name,
486+
})
487+
return gensupport.SendRequest(c.ctx_, c.s.client, req)
488+
}
489+
490+
// Do executes the "healthcare.projects.locations.datasets.fhirStores.fhir.read" call.
491+
func (c *ProjectsLocationsDatasetsFhirStoresFhirReadCall) Do(opts ...googleapi.CallOption) (*http.Response, error) {
492+
gensupport.SetOptions(c.urlParams_, opts...)
493+
return c.doRequest("")
494+
// {
495+
// "description": "Gets the contents of a FHIR resource.\n\nImplements the FHIR standard [read\ninteraction](http://hl7.org/implement/standards/fhir/STU3/http.html#read).\n\nAlso supports the FHIR standard [conditional read\ninteraction](http://hl7.org/implement/standards/fhir/STU3/http.html#cread)\nspecified by supplying an `If-Modified-Since` header with a date/time value\nor an `If-None-Match` header with an ETag value.\n\nOn success, the response body will contain a JSON-encoded representation\nof the resource.\nErrors generated by the FHIR store will contain a JSON-encoded\n`OperationOutcome` resource describing the reason for the error. If the\nrequest cannot be mapped to a valid API method on a FHIR store, a generic\nGCP error might be returned instead.",
496+
// "flatPath": "v1beta1/projects/{projectsId}/locations/{locationsId}/datasets/{datasetsId}/fhirStores/{fhirStoresId}/fhir/{fhirId}/{fhirId1}",
497+
// "httpMethod": "GET",
498+
// "id": "healthcare.projects.locations.datasets.fhirStores.fhir.read",
499+
// "parameterOrder": [
500+
// "name"
501+
// ],
502+
// "parameters": {
503+
// "name": {
504+
// "description": "The name of the resource to retrieve.",
505+
// "location": "path",
506+
// "pattern": "^projects/[^/]+/locations/[^/]+/datasets/[^/]+/fhirStores/[^/]+/fhir/[^/]+/[^/]+$",
507+
// "required": true,
508+
// "type": "string"
509+
// }
510+
// },
511+
// "path": "v1beta1/{+name}",
512+
// "response": {
513+
// "$ref": "HttpBody"
514+
// },
515+
// "scopes": [
516+
// "https://www.googleapis.com/auth/cloud-platform"
517+
// ]
518+
// }
519+
520+
}

0 commit comments

Comments
 (0)