MCPcopy
hub / github.com/dgraph-io/dgraph / queryWithGz

Function queryWithGz

dgraph/cmd/alpha/http_test.go:86–153  ·  view source on GitHub ↗
(queryText, contentType, debug, timeout string, gzReq, gzResp bool)

Source from the content-addressed store, hash-verified

84}
85
86func queryWithGz(queryText, contentType, debug, timeout string, gzReq, gzResp bool) (
87 string, *http.Response, error) {
88
89 params := make([]string, 0, 2)
90 if debug != "" {
91 params = append(params, "debug="+debug)
92 }
93 if timeout != "" {
94 params = append(params, fmt.Sprintf("timeout=%v", timeout))
95 }
96 url := addr + "/query?" + strings.Join(params, "&")
97
98 var buf *bytes.Buffer
99 if gzReq {
100 var b bytes.Buffer
101 gz := gzip.NewWriter(&b)
102 _, _ = gz.Write([]byte(queryText))
103 _ = gz.Close()
104 buf = &b
105 } else {
106 buf = bytes.NewBufferString(queryText)
107 }
108
109 resp, err := runGzipWithRetry(contentType, url, buf, gzReq, gzResp)
110 if err != nil {
111 return "", nil, err
112 }
113
114 defer resp.Body.Close()
115 rd := resp.Body
116 if err != nil {
117 return "", nil, err
118 }
119
120 if gzResp {
121 if strings.Contains(resp.Header.Get("Content-Encoding"), "gzip") {
122 rd, err = gzip.NewReader(rd)
123 if err != nil {
124 return "", nil, err
125 }
126 defer rd.Close()
127 } else {
128 return "", resp, errors.Errorf("Response not compressed")
129 }
130 }
131 body, err := io.ReadAll(rd)
132 if err != nil {
133 return "", nil, err
134 }
135
136 var r res
137 if err := json.Unmarshal(body, &r); err != nil {
138 return "", nil, err
139 }
140
141 // Check for errors
142 if len(r.Errors) != 0 {
143 return "", nil, errors.New(r.Errors[0].Message)

Callers 3

TestDebugSupportFunction · 0.85
TestContentTypeCharsetFunction · 0.85

Calls 5

WriteMethod · 0.95
runGzipWithRetryFunction · 0.85
CloseMethod · 0.65
GetMethod · 0.65
ErrorfMethod · 0.45

Tested by

no test coverage detected