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

Side by Side 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, 1 month 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 unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 package gitiles
6
7 import (
8 "bufio"
9 "bytes"
10 "encoding/json"
11 "io"
12 "net/http"
13 "reflect"
14 )
15
16 // Private /////////////////////////////////////////////////////////////////////
17
18 // Types ///////////////////////////////////////////////////////////////////////
19
20 type jsonResult struct {
21 err error
22 dataPtr interface{}
23 }
24
25 type jsonRequest struct {
26 urlPath string
27 typ reflect.Type
28 resultChan chan<- jsonResult
29 }
30
31 // Member functions ////////////////////////////////////////////////////////////
32
33 func (j jsonRequest) URLPath() string { return j.urlPath + "?format=JSON" }
34 func (j jsonRequest) Method() string { return "GET" }
35 func (j jsonRequest) Body() io.Reader { return nil }
36
37 func (j jsonRequest) Process(resp *http.Response, err error) {
38 var rslt jsonResult
39 if err != nil {
40 rslt.err = err
41 } else {
42 bufreader := bufio.NewReader(resp.Body)
43 firstFour, err := bufreader.Peek(4)
44 if err != nil {
45 rslt.err = err
46 } else {
47 if bytes.Equal(firstFour, []byte(")]}'")) {
48 bufreader.Read(make([]byte, 4))
49 }
50
51 data := reflect.New(j.typ).Interface()
52 err = json.NewDecoder(bufreader).Decode(data)
53 if err != nil {
54 rslt.err = err
55 } else {
56 rslt.dataPtr = data
57 }
58 }
59 }
60 j.resultChan <- rslt
61 }
OLDNEW
« 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