Exemplo n.º 1
0
func callFacebookAPI(client *http.Client, url string, response interface{}) {
	fmt.Printf("Fetching %s...\n", url)
	resp := retry.Request(url, func() (*http.Response, error) {
		req, err := http.NewRequest("GET", url, nil)
		d.Chk.NoError(err)
		return client.Do(req)
	})

	msg := func() string {
		body := &bytes.Buffer{}
		_, err := io.Copy(body, resp.Body)
		d.Chk.NoError(err)
		return fmt.Sprintf("could not load %s: %d: %s", url, resp.StatusCode, body)
	}

	switch resp.StatusCode / 100 {
	case 4:
		d.Exp.Fail(msg())
	case 5:
		d.Chk.Fail(msg())
	}

	err := json.NewDecoder(resp.Body).Decode(response)
	d.Chk.NoError(err)
}
Exemplo n.º 2
0
func (api liveFlickrAPI) Call(method string, response interface{}, args *map[string]string) error {
	restURL := "https://api.flickr.com/services/rest/"

	values := url.Values{
		"method":         []string{method},
		"format":         []string{"json"},
		"nojsoncallback": []string{"1"},
	}

	if args != nil {
		for k, v := range *args {
			values[k] = []string{v}
		}
	}

	res := retry.Request(restURL, func() (*http.Response, error) {
		return oauthClient.Get(nil, api.tokenCred, restURL, values)
	})

	defer res.Body.Close()
	buff, err := ioutil.ReadAll(res.Body)
	d.Chk.NoError(err)
	if err = json.Unmarshal(buff, response); err != nil {
		return err
	}

	status := reflect.ValueOf(response).Elem().FieldByName("Stat").Interface().(string)
	if status != "ok" {
		err = errors.New(fmt.Sprintf("Failed flickr API call: %v, status: %v", method, status))
	}
	return nil
}
Exemplo n.º 3
0
func callPicasaURL(client *http.Client, url string) io.ReadCloser {
	return retry.Request(url, func() (*http.Response, error) {
		req, err := http.NewRequest("GET", url, nil)
		d.Chk.NoError(err)
		req.Header.Add("GData-Version", "2")
		return client.Do(req)
	}).Body
}