| OLD | NEW |
| (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 } |
| OLD | NEW |