Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(738)

Unified Diff: go/src/infra/libs/gitiles/priv_jsonRequest.go

Issue 662113003: Drover's back, baby! (Closed) Base URL: https://chromium.googlesource.com/infra/infra.git/+/master
Patch Set: more tests and refactors Created 6 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « go/src/infra/libs/gitiles/priv_jsonCommitDiff.go ('k') | go/src/infra/libs/gitiles/priv_objRequest.go » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: go/src/infra/libs/gitiles/priv_jsonRequest.go
diff --git a/go/src/infra/libs/gitiles/priv_jsonRequest.go b/go/src/infra/libs/gitiles/priv_jsonRequest.go
new file mode 100644
index 0000000000000000000000000000000000000000..5a796df671d4ce6aacc304ac2cad88ae2071b6d2
--- /dev/null
+++ b/go/src/infra/libs/gitiles/priv_jsonRequest.go
@@ -0,0 +1,61 @@
+// Copyright 2014 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+package gitiles
+
+import (
+ "bufio"
+ "bytes"
+ "encoding/json"
+ "io"
+ "net/http"
+ "reflect"
+)
+
+// Private /////////////////////////////////////////////////////////////////////
+
+// Types ///////////////////////////////////////////////////////////////////////
+
+type jsonResult struct {
+ err error
+ dataPtr interface{}
+}
+
+type jsonRequest struct {
+ urlPath string
+ typ reflect.Type
+ resultChan chan<- jsonResult
+}
+
+// Member functions ////////////////////////////////////////////////////////////
+
+func (j jsonRequest) URLPath() string { return j.urlPath + "?format=JSON" }
+func (j jsonRequest) Method() string { return "GET" }
+func (j jsonRequest) Body() io.Reader { return nil }
+
+func (j jsonRequest) Process(resp *http.Response, err error) {
+ var rslt jsonResult
+ if err != nil {
+ rslt.err = err
+ } else {
+ bufreader := bufio.NewReader(resp.Body)
+ firstFour, err := bufreader.Peek(4)
+ if err != nil {
+ rslt.err = err
+ } else {
+ if bytes.Equal(firstFour, []byte(")]}'")) {
+ bufreader.Read(make([]byte, 4))
+ }
+
+ data := reflect.New(j.typ).Interface()
+ err = json.NewDecoder(bufreader).Decode(data)
+ if err != nil {
+ rslt.err = err
+ } else {
+ rslt.dataPtr = data
+ }
+ }
+ }
+ j.resultChan <- rslt
+}
« no previous file with comments | « go/src/infra/libs/gitiles/priv_jsonCommitDiff.go ('k') | go/src/infra/libs/gitiles/priv_objRequest.go » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698