// Extract a RequestToken from the response body func ParseRequestToken(response string) (*RequestToken, error) { val, err := url.ParseQuery(strings.TrimSpace(response)) if err != nil { return nil, err } ret := &RequestToken{} oauth_problem := val.Get("oauth_problem") if oauth_problem != "" { ret.OAuthProblem = oauth_problem return ret, flickErr.NewError(flickErr.RequestTokenError, oauth_problem) } confirmed, _ := strconv.ParseBool(val.Get("oauth_callback_confirmed")) ret.OauthCallbackConfirmed = confirmed ret.OauthToken = val.Get("oauth_token") ret.OauthTokenSecret = val.Get("oauth_token_secret") return ret, nil }
// Extract a OAuthToken from the response body func ParseOAuthToken(response string) (*OAuthToken, error) { val, err := url.ParseQuery(strings.TrimSpace(response)) if err != nil { return nil, err } ret := &OAuthToken{} oauth_problem := val.Get("oauth_problem") if oauth_problem != "" { ret.OAuthProblem = oauth_problem return ret, flickErr.NewError(flickErr.OAuthTokenError, oauth_problem) } ret.OAuthToken = val.Get("oauth_token") ret.OAuthTokenSecret = val.Get("oauth_token_secret") ret.Fullname = val.Get("fullname") ret.UserNsid = val.Get("user_nsid") ret.Username = val.Get("username") return ret, nil }
// Given an http.Response retrieved from Flickr, unmarshal results // into a FlickrResponse struct. func parseApiResponse(res *http.Response, r FlickrResponse) error { defer res.Body.Close() responseBody, err := ioutil.ReadAll(res.Body) if err != nil { return err } err = xml.Unmarshal(responseBody, r) if err != nil { // In case of OAuth errors (signature, parameters, etc) Flicker does not // return a REST response but raw text (!), so the unmarshalling could fail. // We need to artificially build a FlickrResponse and manually fill in // the error string. r.SetErrorStatus(true) r.SetErrorCode(-1) r.SetErrorMsg(string(responseBody)) } if r.HasErrors() { return flickErr.NewError(flickErr.ApiError, r.ErrorMsg()) } return nil }