MakeAndDecodeHTTPRequest sends an HTTP request using the given url and body and then decodes the response correctly based on whether it was a GraphQL or REST request. It returns the decoded response along with either soft or hard errors. Soft error means one can assume that the response is valid and
(client *http.Client, url string,
body interface{}, field Field)
| 62 | // errors. Any other kind of error is a hard error. |
| 63 | // For REST requests, any error is a hard error, including those returned from the remote endpoint. |
| 64 | func (fconf *FieldHTTPConfig) MakeAndDecodeHTTPRequest(client *http.Client, url string, |
| 65 | body interface{}, field Field) (interface{}, x.GqlErrorList, x.GqlErrorList) { |
| 66 | var b []byte |
| 67 | var err error |
| 68 | // need this check to make sure that we don't send body as []byte(`null`) |
| 69 | if body != nil { |
| 70 | b, err = json.Marshal(body) |
| 71 | if err != nil { |
| 72 | return nil, nil, x.GqlErrorList{jsonMarshalError(err, field, body)} |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | // Make the request to external HTTP endpoint using the URL and body |
| 77 | resp, err := MakeHttpRequest(client, fconf.Method, url, fconf.ForwardHeaders, b) |
| 78 | if err != nil { |
| 79 | return nil, nil, x.GqlErrorList{externalRequestError(err, field)} |
| 80 | } |
| 81 | |
| 82 | defer func() { |
| 83 | if err := resp.Body.Close(); err != nil { |
| 84 | glog.Warningf("error closing body: %v", err) |
| 85 | } |
| 86 | }() |
| 87 | b, err = io.ReadAll(resp.Body) |
| 88 | if err != nil { |
| 89 | return nil, nil, x.GqlErrorList{externalRequestError(err, field)} |
| 90 | } |
| 91 | |
| 92 | // Decode the HTTP response |
| 93 | var response interface{} |
| 94 | var softErrs x.GqlErrorList |
| 95 | graphqlResp := graphqlResp{} |
| 96 | if fconf.RemoteGqlQueryName != "" { |
| 97 | // if it was a GraphQL request we need to decode the response as a GraphQL response |
| 98 | if err = Unmarshal(b, &graphqlResp); err != nil { |
| 99 | return nil, nil, x.GqlErrorList{jsonUnmarshalError(err, field)} |
| 100 | } |
| 101 | // if the GraphQL response has any errors, save them to be reported later |
| 102 | softErrs = graphqlResp.Errors |
| 103 | |
| 104 | // find out the data returned for the GraphQL query |
| 105 | var ok bool |
| 106 | if response, ok = graphqlResp.Data[fconf.RemoteGqlQueryName]; !ok { |
| 107 | return nil, nil, append(softErrs, keyNotFoundError(field, fconf.RemoteGqlQueryName)) |
| 108 | } |
| 109 | } else { |
| 110 | // this was a REST request |
| 111 | if resp.StatusCode >= 200 && resp.StatusCode < 300 { |
| 112 | // if this was a successful request, lets try to unmarshal the response |
| 113 | if err = Unmarshal(b, &response); err != nil { |
| 114 | return nil, nil, x.GqlErrorList{jsonUnmarshalError(err, field)} |
| 115 | |
| 116 | } |
| 117 | } else { |
| 118 | // if we get unsuccessful response from the REST api, lets try to see if |
| 119 | // it sent any errors in the form expected for GraphQL errors. |
| 120 | if err = Unmarshal(b, &graphqlResp); err != nil { |
| 121 | err = fmt.Errorf("unexpected error with: %v", resp.StatusCode) |
no test coverage detected