// Parses a JSON encoded HTTP response into the supplied interface. func (r APIResponse) Parse(out interface{}) (err error) { var b []byte switch r.StatusCode { case STATUS_NOTFOUND: fallthrough case STATUS_GATEWAY: fallthrough case STATUS_INVALID: e := &Errors{} defer r.Body.Close() if b, err = ioutil.ReadAll(r.Body); err == nil { json.Unmarshal(b, e) err = *e } return case STATUS_LIMIT: err = RateLimitError{ Limit: r.RateLimit(), Remaining: r.RateLimitRemaining(), Reset: r.RateLimitReset(), } return } defer r.Body.Close() if b, err = ioutil.ReadAll(r.Body); err == nil { err = json.Unmarshal(b, out) } if err == io.EOF { err = nil } return }
// Parses a JSON encoded HTTP response into the supplied interface. func (r APIResponse) Parse(out interface{}) (err error) { var b []byte switch r.StatusCode { case STATUS_UNAUTHORIZED: fallthrough case STATUS_NOTFOUND: fallthrough case STATUS_GATEWAY: fallthrough case STATUS_FORBIDDEN: fallthrough case STATUS_INVALID: e := &Errors{} if b, err = r.readBody(); err != nil { return } if err = json.Unmarshal(b, e); err != nil { err = NewResponseError(r.StatusCode, string(b)) } else { err = *e } return case STATUS_LIMIT: err = RateLimitError{ Limit: r.RateLimit(), Remaining: r.RateLimitRemaining(), Reset: r.RateLimitReset(), } // consume the request body even if we don't need it r.readBody() return case STATUS_NO_CONTENT: return case STATUS_CREATED: fallthrough case STATUS_ACCEPTED: fallthrough case STATUS_OK: if b, err = r.readBody(); err != nil { return } err = json.Unmarshal(b, out) if err == io.EOF { err = nil } default: if b, err = r.readBody(); err != nil { return } err = NewResponseError(r.StatusCode, string(b)) } return }
func filterStream(client *twittergo.Client, path string, query url.Values) (err error) { var ( resp *twittergo.APIResponse ) sc := NewStreamConn(300) resp, err = Connect(client, path, query) done := make(chan bool) stream := make(chan []byte, 1000) go func() { for data := range stream { tweet := &twittergo.Tweet{} err := json.Unmarshal(data, tweet) if err == nil { fmt.Printf("ID: %v\n", tweet.Id()) fmt.Printf("User: %v\n", tweet.User().ScreenName()) fmt.Printf("Tweet: %v\n", tweet.Text()) } } }() readStream(client, sc, path, query, resp, func(line []byte) { stream <- line }, done) return }
// Parses a JSON encoded HTTP response into the supplied interface. func (r APIResponse) Parse(out interface{}) (err error) { var b []byte switch r.StatusCode { case STATUS_UNAUTHORIZED: fallthrough case STATUS_NOTFOUND: fallthrough case STATUS_GATEWAY: fallthrough case STATUS_FORBIDDEN: fallthrough case STATUS_INVALID: e := &Errors{} if b, err = r.readBody(); err != nil { return } if err = json.Unmarshal(b, e); err != nil { err = fmt.Errorf("%v", string(b)) } else { err = *e } return case STATUS_LIMIT: err = RateLimitError{ Limit: r.RateLimit(), Remaining: r.RateLimitRemaining(), Reset: r.RateLimitReset(), } return case STATUS_OK: if b, err = r.readBody(); err != nil { return } err = json.Unmarshal(b, out) if err == io.EOF { err = nil } default: if b, err = r.readBody(); err != nil { return } err = fmt.Errorf("%v", string(b)) } return }
func filterStream(client *twittergo.Client, path string, dbname string, query url.Values) (err error) { var ( resp *twittergo.APIResponse ) sc := NewStreamConn(300) resp, err = Connect(client, path, query) done := make(chan bool) stream := make(chan []byte, 1000) go func() { session, err := mgo.Dial("localhost") if err != nil { panic(err) } defer session.Close() session.SetMode(mgo.Monotonic, true) db := session.DB("twitter") colls, err := db.CollectionNames() if err != nil { panic(err) } c := db.C(dbname) if !tableExists(dbname, colls) { info := &mgo.CollectionInfo{} c.Create(info) } for data := range stream { tweet := &twittergo.Tweet{} fmt.Printf("%s\n", data) err := json.Unmarshal(data, tweet) if err == nil { fmt.Printf("%s\n", tweet.Text()) c.Insert(tweet) } } }() readStream(client, sc, path, query, resp, func(line []byte) { stream <- line }, done) return }