Skip to content

Commit

Permalink
google-api-go-generator: respect HttpBody for GET requests
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
broady committed May 1, 2019
1 parent 358d122 commit 42f40b5
Show file tree
Hide file tree
Showing 3 changed files with 184 additions and 7 deletions.
32 changes: 25 additions & 7 deletions google-api-go-generator/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -1688,7 +1688,7 @@ func (meth *Method) generateCode() {
pn("\n// method id %q:", meth.Id())

retType := responseType(a, meth.m)
if meth.IsRawHTTP() {
if meth.IsRawResponse() {
retType = "*http.Response"
}
retTypeComma := retType
Expand Down Expand Up @@ -1916,7 +1916,7 @@ func (meth *Method) generateCode() {
pn("}")
}
pn("var body io.Reader = nil")
if meth.IsRawHTTP() {
if meth.IsRawRequest() {
pn("body = c.body_")
} else {
if ba := args.bodyArg(); ba != nil && httpMethod != "GET" {
Expand Down Expand Up @@ -1996,7 +1996,7 @@ func (meth *Method) generateCode() {

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

func (meth *Method) IsRawHTTP() bool {
func (meth *Method) IsRawRequest() bool {
if meth.m.Request == nil {
return false
}
// TODO(cbro): enable across other APIs.
return meth.api.Name == "healthcare" && meth.m.Request.Ref == "HttpBody"
if meth.api.Name != "healthcare" {
return false
}
return meth.m.Request.Ref == "HttpBody"
}

func (meth *Method) IsRawResponse() bool {
if meth.m.Response == nil {
return false
}
if meth.IsRawRequest() {
// always match raw requests with raw responses.
return true
}
// TODO(cbro): enable across other APIs.
if meth.api.Name != "healthcare" {
return false
}
return meth.m.Response.Ref == "HttpBody"
}

func (meth *Method) NewArguments() *arguments {
Expand All @@ -2213,7 +2231,7 @@ func (meth *Method) NewArguments() *arguments {
args.AddArg(arg)
}
if rs := meth.m.Request; rs != nil {
if meth.IsRawHTTP() {
if meth.IsRawRequest() {
args.AddArg(&argument{
goname: "body_",
gotype: "io.Reader",
Expand Down
25 changes: 25 additions & 0 deletions google-api-go-generator/testdata/http-body.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,31 @@
"flatPath": "v1beta1/projects/{projectsId}/locations/{locationsId}/datasets/{datasetsId}/fhirStores/{fhirStoresId}/fhir/{fhirId}",
"path": "v1beta1/{+parent}/fhir/{+type}",
"id": "healthcare.projects.locations.datasets.fhirStores.fhir.createResource"
},
"read": {
"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.",
"flatPath": "v1beta1/projects/{projectsId}/locations/{locationsId}/datasets/{datasetsId}/fhirStores/{fhirStoresId}/fhir/{fhirId}/{fhirId1}",
"httpMethod": "GET",
"id": "healthcare.projects.locations.datasets.fhirStores.fhir.read",
"parameterOrder": [
"name"
],
"parameters": {
"name": {
"description": "The name of the resource to retrieve.",
"location": "path",
"pattern": "^projects/[^/]+/locations/[^/]+/datasets/[^/]+/fhirStores/[^/]+/fhir/[^/]+/[^/]+$",
"required": true,
"type": "string"
}
},
"path": "v1beta1/{+name}",
"response": {
"$ref": "HttpBody"
},
"scopes": [
"https://www.googleapis.com/auth/cloud-platform"
]
}
}
}
Expand Down
134 changes: 134 additions & 0 deletions google-api-go-generator/testdata/http-body.want
Original file line number Diff line number Diff line change
Expand Up @@ -384,3 +384,137 @@ func (c *ProjectsLocationsDatasetsFhirStoresFhirCreateResourceCall) Do(opts ...g
// }

}

// method id "healthcare.projects.locations.datasets.fhirStores.fhir.read":

type ProjectsLocationsDatasetsFhirStoresFhirReadCall struct {
s *Service
name string
urlParams_ gensupport.URLParams
ifNoneMatch_ string
ctx_ context.Context
header_ http.Header
}

// Read: Gets the contents of a FHIR resource.
//
// Implements the FHIR standard
// [read
// interaction](http://hl7.org/implement/standards/fhir/STU3/http.h
// tml#read).
//
// Also supports the FHIR standard [conditional
// read
// interaction](http://hl7.org/implement/standards/fhir/STU3/http.ht
// ml#cread)
// specified by supplying an `If-Modified-Since` header with a date/time
// value
// or an `If-None-Match` header with an ETag value.
//
// On success, the response body will contain a JSON-encoded
// representation
// of the resource.
// Errors generated by the FHIR store will contain a
// JSON-encoded
// `OperationOutcome` resource describing the reason for the error. If
// the
// request cannot be mapped to a valid API method on a FHIR store, a
// generic
// GCP error might be returned instead.
func (r *ProjectsLocationsDatasetsFhirStoresFhirService) Read(name string) *ProjectsLocationsDatasetsFhirStoresFhirReadCall {
c := &ProjectsLocationsDatasetsFhirStoresFhirReadCall{s: r.s, urlParams_: make(gensupport.URLParams)}
c.name = name
return c
}

// Fields allows partial responses to be retrieved. See
// https://developers.google.com/gdata/docs/2.0/basics#PartialResponse
// for more information.
func (c *ProjectsLocationsDatasetsFhirStoresFhirReadCall) Fields(s ...googleapi.Field) *ProjectsLocationsDatasetsFhirStoresFhirReadCall {
c.urlParams_.Set("fields", googleapi.CombineFields(s))
return c
}

// IfNoneMatch sets the optional parameter which makes the operation
// fail if the object's ETag matches the given value. This is useful for
// getting updates only after the object has changed since the last
// request. Use googleapi.IsNotModified to check whether the response
// error from Do is the result of In-None-Match.
func (c *ProjectsLocationsDatasetsFhirStoresFhirReadCall) IfNoneMatch(entityTag string) *ProjectsLocationsDatasetsFhirStoresFhirReadCall {
c.ifNoneMatch_ = entityTag
return c
}

// Context sets the context to be used in this call's Do method. Any
// pending HTTP request will be aborted if the provided context is
// canceled.
func (c *ProjectsLocationsDatasetsFhirStoresFhirReadCall) Context(ctx context.Context) *ProjectsLocationsDatasetsFhirStoresFhirReadCall {
c.ctx_ = ctx
return c
}

// Header returns an http.Header that can be modified by the caller to
// add HTTP headers to the request.
func (c *ProjectsLocationsDatasetsFhirStoresFhirReadCall) Header() http.Header {
if c.header_ == nil {
c.header_ = make(http.Header)
}
return c.header_
}

func (c *ProjectsLocationsDatasetsFhirStoresFhirReadCall) doRequest(alt string) (*http.Response, error) {
reqHeaders := make(http.Header)
for k, v := range c.header_ {
reqHeaders[k] = v
}
reqHeaders.Set("User-Agent", c.s.userAgent())
if c.ifNoneMatch_ != "" {
reqHeaders.Set("If-None-Match", c.ifNoneMatch_)
}
var body io.Reader = nil
c.urlParams_.Set("alt", alt)
c.urlParams_.Set("prettyPrint", "false")
urls := googleapi.ResolveRelative(c.s.BasePath, "v1beta1/{+name}")
urls += "?" + c.urlParams_.Encode()
req, err := http.NewRequest("GET", urls, body)
if err != nil {
return nil, err
}
req.Header = reqHeaders
googleapi.Expand(req.URL, map[string]string{
"name": c.name,
})
return gensupport.SendRequest(c.ctx_, c.s.client, req)
}

// Do executes the "healthcare.projects.locations.datasets.fhirStores.fhir.read" call.
func (c *ProjectsLocationsDatasetsFhirStoresFhirReadCall) Do(opts ...googleapi.CallOption) (*http.Response, error) {
gensupport.SetOptions(c.urlParams_, opts...)
return c.doRequest("")
// {
// "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.",
// "flatPath": "v1beta1/projects/{projectsId}/locations/{locationsId}/datasets/{datasetsId}/fhirStores/{fhirStoresId}/fhir/{fhirId}/{fhirId1}",
// "httpMethod": "GET",
// "id": "healthcare.projects.locations.datasets.fhirStores.fhir.read",
// "parameterOrder": [
// "name"
// ],
// "parameters": {
// "name": {
// "description": "The name of the resource to retrieve.",
// "location": "path",
// "pattern": "^projects/[^/]+/locations/[^/]+/datasets/[^/]+/fhirStores/[^/]+/fhir/[^/]+/[^/]+$",
// "required": true,
// "type": "string"
// }
// },
// "path": "v1beta1/{+name}",
// "response": {
// "$ref": "HttpBody"
// },
// "scopes": [
// "https://www.googleapis.com/auth/cloud-platform"
// ]
// }

}

0 comments on commit 42f40b5

Please sign in to comment.