func GetToken(code string) (resp *napping.Response, err error) { endPoint := "oauth/device/token" header := http.Header{ "Content-type": []string{"application/json"}, "User-Agent": []string{clearance.UserAgent}, "Cookie": []string{clearance.Cookies}, } params := napping.Params{ "code": code, "client_id": ClientId, "client_secret": ClientSecret, }.AsUrlValues() req := napping.Request{ Url: fmt.Sprintf("%s/%s", ApiUrl, endPoint), Method: "POST", Params: ¶ms, Header: &header, } resp, err = napping.Send(&req) if err == nil && resp.Status() == 403 && retries < 3 { retries += 1 err = newClearance() if err == nil { resp, err = GetToken(code) } } return resp, err }
// do is an easy function for performing requests against Mandrill's API. func do(url string, data interface{}, result interface{}) error { // merr can store a the JSON object returned by mandrill on errors merr := newError() // prepare and send the request rr := &napping.Request{ Url: "https://mandrillapp.com/api/1.0" + url, Method: "POST", Payload: data, Result: result, Error: merr} res, err := napping.Send(rr) // network error if err != nil { return err } // mandrill error if res.Status() != 200 { if merr != nil { return merr } else { // a return JSON was not found/parsed fmt.Errorf("mandrill: unknown error happened") } } // no error happened return nil }
// LookupImporters gets the import paths of all downstream packages known by // GoDoc.org to import this Package. func (p *Package) LookupImporters() error { url := apiUrl + p.ImportPath var e apiError var result apiResponse var importers []string req := napping.Request{ Url: url, Method: "GET", Result: &result, Error: &e, } resp, err := napping.Send(&req) if err != nil { return err } if resp.Status() != 200 { msg := fmt.Sprintf("Unexpected status from server: %d", resp.Status()) return errors.New(msg) } for _, r := range result.Results { if r.Path == downtestPackage { continue // Don't test self } importers = append(importers, r.Path) } sort.Strings(importers) p.Importers = importers return nil }
func Post(endPoint string, payload *bytes.Buffer) (resp *napping.Response, err error) { header := http.Header{ "Content-type": []string{"application/json"}, "Authorization": []string{fmt.Sprintf("Bearer %s", config.Get().TraktToken)}, "trakt-api-key": []string{ClientId}, "trakt-api-version": []string{ApiVersion}, "User-Agent": []string{clearance.UserAgent}, "Cookie": []string{clearance.Cookies}, } req := napping.Request{ Url: fmt.Sprintf("%s/%s", ApiUrl, endPoint), Method: "POST", RawPayload: true, Payload: payload, Header: &header, } resp, err = napping.Send(&req) if err == nil && resp.Status() == 403 && retries < 3 { retries += 1 err = newClearance() if err == nil { resp, err = Post(endPoint, payload) } } return resp, err }
func GetCode() (code *Code, err error) { endPoint := "oauth/device/code" header := http.Header{ "Content-type": []string{"application/json"}, "User-Agent": []string{clearance.UserAgent}, "Cookie": []string{clearance.Cookies}, } params := napping.Params{ "client_id": ClientId, }.AsUrlValues() req := napping.Request{ Url: fmt.Sprintf("%s/%s", ApiUrl, endPoint), Method: "POST", Params: ¶ms, Header: &header, } resp, err := napping.Send(&req) if err == nil && resp.Status() == 403 && retries < 3 { retries += 1 err = newClearance() if err == nil { code, err = GetCode() } } else { resp.Unmarshal(&code) } if err == nil && resp.Status() != 200 { err = errors.New(fmt.Sprintf("Unable to get Trakt code: %d", resp.Status())) } return code, err }
func (int *InternalAPI) GetCurrentHead(env string) string { conf := (*int.Config)[env] if conf == nil { return "" } url := conf.BaseURL authKey := conf.AuthKey if url == "" || authKey == "" { return "" } result := struct { CurrentHead string `json:"current_head"` }{} req := napping.Request{ Url: fmt.Sprintf("%s%s", url, "/current_head"), Method: "GET", Result: &result, Header: &http.Header{}, } req.Header.Set("X-Internal-Key", authKey) _, err := napping.Send(&req) if err != nil { return "" } return result.CurrentHead }
func GetWithAuth(endPoint string, params url.Values) (resp *napping.Response, err error) { header := http.Header{ "Content-type": []string{"application/json"}, "Authorization": []string{fmt.Sprintf("Bearer %s", config.Get().TraktToken)}, "trakt-api-key": []string{ClientId}, "trakt-api-version": []string{ApiVersion}, "User-Agent": []string{clearance.UserAgent}, "Cookie": []string{clearance.Cookies}, } req := napping.Request{ Url: fmt.Sprintf("%s/%s", ApiUrl, endPoint), Method: "GET", Params: ¶ms, Header: &header, } resp, err = napping.Send(&req) if err == nil && resp.Status() == 403 && retries < 3 { retries += 1 err = newClearance() if err == nil { resp, err = GetWithAuth(endPoint, params) } } return resp, err }
func GetSurgeClearances() { header := http.Header{ "Content-type": []string{"application/json"}, } params := napping.Params{}.AsUrlValues() req := napping.Request{ Url: fmt.Sprintf("%s/%s", "https://cloudhole.surge.sh", "cloudhole.json"), Method: "GET", Params: ¶ms, Header: &header, } resp, err := napping.Send(&req) var tmpClearances []*Clearance if err == nil && resp.Status() == 200 { resp.Unmarshal(&tmpClearances) } apiKey := config.Get().CloudHoleKey for _, clearance := range tmpClearances { if clearance.Key == apiKey { clearances = append(clearances, clearance) } } }
// Authenticate with Stormpath using supplied credentials. Username may be // either a username or the user's email. func (app *Application) Authenticate(username, password string) (Account, error) { acct := Account{} s := username + ":" + password value := base64.URLEncoding.EncodeToString([]byte(s)) m := map[string]string{ "type": "basic", "value": value, } loginUrl := app.Href + "/loginAttempts" var resp struct { Account struct { Href string `json:"href"` } `json:"account"` } e := new(StormpathError) req := &napping.Request{ Userinfo: app.userinfo(), Url: loginUrl, Method: "POST", Payload: &m, Result: &resp, Error: &e, } res, err := napping.Send(req) if err != nil { return acct, err } if res.Status() != 200 { log.Println(res.Status()) log.Println(res) log.Println(e) return acct, InvalidUsernamePassword } return app.GetAccount(resp.Account.Href) }
// CreateAccount creates a new account accessible to the application. func (app *Application) CreateAccount(template Account) (Account, error) { /* data := &map[string]string{ "username": username, "password": password, "email": email, "surname": surname, "givenName": givenName, } */ url := app.Href + "/accounts" acct := Account{} e := new(StormpathError) req := &napping.Request{ Userinfo: app.userinfo(), Url: url, Method: "POST", Payload: &template, Result: &acct, Error: e, } res, err := napping.Send(req) if err != nil { return acct, err } acct.app = app if res.Status() != 201 { log.Println(res.Status()) log.Println(e) return acct, BadResponse } return acct, nil }
// Delete removes an account from Stormpath. func (a *Account) Delete() error { e := new(StormpathError) req := &napping.Request{ Userinfo: a.app.userinfo(), Url: a.Href, Method: "DELETE", Error: e, } res, err := napping.Send(req) if err != nil { return err } if res.Status() != 204 { log.Println(res.Status()) log.Println(e) return BadResponse } return nil // Successful deletion }
// Update saves the account to Stormpath. func (a *Account) Update() error { e := new(StormpathError) req := &napping.Request{ Userinfo: a.app.userinfo(), Url: a.Href, Method: "POST", Payload: &a, Error: e, } res, err := napping.Send(req) if err != nil { return err } if res.Status() != 200 { log.Println(res.Status()) log.Println(e) return BadResponse } return nil // Successful update }
// do is an easy function for performing requests against Mandrill's API. func do(url string, data interface{}, result interface{}) error { err := newError() rr := &napping.Request{ Url: "https://mandrillapp.com/api/1.0" + url, Method: "POST", Payload: data, Result: result, Error: err} status, errs := napping.Send(rr) if errs == nil { return nil } fmt.Println(status, rr.Error) return errs }
func GetClearance() (clearance *Clearance, err error) { if len(clearances) > 0 { clearance = clearances[rand.Intn(len(clearances))] return clearance, nil } apiKey := config.Get().CloudHoleKey if apiKey == "" { return defaultClearance, nil } header := http.Header{ "Content-type": []string{"application/json"}, "Authorization": []string{apiKey}, } params := napping.Params{}.AsUrlValues() req := napping.Request{ Url: fmt.Sprintf("%s/%s", "https://cloudhole.herokuapp.com", "clearances"), Method: "GET", Params: ¶ms, Header: &header, } resp, err := napping.Send(&req) if err == nil && resp.Status() == 200 { resp.Unmarshal(&clearances) } else if resp.Status() == 503 { GetSurgeClearances() } if len(clearances) > 0 { clearance = clearances[rand.Intn(len(clearances))] } else { err = errors.New("Failed to get new clearance.") clearance = defaultClearance } return clearance, err }
// GetAccount returns the specified account object, if it exists. func (app *Application) GetAccount(href string) (Account, error) { acct := Account{} e := new(StormpathError) req := &napping.Request{ Userinfo: app.userinfo(), Url: href, Method: "GET", Result: &acct, Error: e, } res, err := napping.Send(req) if err != nil { return acct, err } acct.app = app if res.Status() != 200 { log.Println(res.Status()) log.Println(e) return acct, BadResponse } return acct, nil }