示例#1
0
文件: justin.go 项目: homemade/justin
// Validate a set of supplied user credentials against the JustGiving database
func (svc *Service) Validate(account mail.Address, password string) (valid bool, err error) {

	method := "POST"

	path := bytes.NewBuffer([]byte(svc.BasePath))
	path.WriteString("/")
	path.WriteString(svc.APIKey)
	path.WriteString("/v1/account/validate")

	// mail.Address stores email in the format <*****@*****.**>, we don't want the `<` `>`
	em := account.String()
	em = em[1 : len(em)-1]

	data := struct {
		Email    string
		Password string
	}{em, password}

	sBody, body, err := api.BuildBody("Validate", data, ContentType)
	if err != nil {
		return false, err
	}

	req, err := api.BuildRequest(UserAgent, ContentType, method, path.String(), body)
	if err != nil {
		return false, err
	}

	res, resBody, err := api.Do(svc.client, svc.origin, "Validate", req, sBody, svc.HTTPLogger)
	if err != nil {
		return false, err
	}

	if res.StatusCode != 200 {
		return false, fmt.Errorf("invalid response %s", res.Status)
	}
	var result = struct {
		IsValid bool `json:"isValid"`
	}{}
	if err := json.Unmarshal([]byte(resBody), &result); err != nil {
		return false, fmt.Errorf("invalid response %v", err)
	}
	return result.IsValid, nil

}
示例#2
0
文件: justin.go 项目: homemade/justin
// AccountRegistration registers a new user account with JustGiving
func (svc *Service) AccountRegistration(account models.Account) (err error) {

	method := "PUT"

	path := bytes.NewBuffer([]byte(svc.BasePath))
	path.WriteString("/")
	path.WriteString(svc.APIKey)
	path.WriteString("/v1/account/")

	sBody, body, err := api.BuildBody("AccountRegistration", account, ContentType)
	if err != nil {
		return err
	}
	req, err := api.BuildRequest(UserAgent, ContentType, method, path.String(), body)
	if err != nil {
		return err
	}

	res, _, err := api.Do(svc.client, svc.origin, "AccountRegistration", req, sBody, svc.HTTPLogger)
	if err != nil {
		return err
	}

	if res.StatusCode != 200 {
		// run request validation on failure
		info := "no errors found"
		valid, err := account.HasValidCountry(svc)
		if err != nil {
			info = fmt.Sprintf("errors running validation %v", err)
		} else {
			if !valid {
				info = "invalid Country"
			}
		}
		return fmt.Errorf("invalid response %s, result of running validation on request payload was: %s", res.Status, info)
	}
	return nil

}
示例#3
0
文件: justin.go 项目: homemade/justin
// RegisterFundraisingPageForEvent registers a fundraising page on the JustGiving website
func (svc *Service) RegisterFundraisingPageForEvent(account mail.Address, password string, page models.FundraisingPageForEvent) (pageURL *url.URL, signOnURL *url.URL, err error) {

	method := "PUT"

	path := bytes.NewBuffer([]byte(svc.BasePath))
	path.WriteString("/")
	path.WriteString(svc.APIKey)
	path.WriteString("/v1/fundraising/pages")

	sBody, body, err := api.BuildBody("RegisterFundraisingPageForEvent", page, ContentType)
	if err != nil {
		return nil, nil, err
	}
	req, err := api.BuildRequest(UserAgent, ContentType, method, path.String(), body)
	if err != nil {
		return nil, nil, err
	}

	// This request requires authentication
	// mail.Address stores email in the format <*****@*****.**>, we don't want the `<` `>`
	em := account.String()
	em = em[1 : len(em)-1]
	req.SetBasicAuth(em, password)

	res, resBody, err := api.Do(svc.client, svc.origin, "RegisterFundraisingPageForEvent", req, sBody, svc.HTTPLogger)
	if err != nil {
		return nil, nil, err
	}

	//201 Created
	if res.StatusCode != 201 {
		// run request validation on failure
		var info string
		var valid bool
		valid, err = page.HasValidCurrencyCode(svc)
		if err != nil {
			info = fmt.Sprintf("errors running CurrencyCode validation %v; ", err)
		} else {
			if !valid {
				info = "invalid CurrencyCode; "
			}
		}
		valid = page.HasValidTargetAmount()
		if !valid {
			info += "invalid TargetAmount"
		}
		if info == "" {
			info = "no errors found"
		}
		return nil, nil, fmt.Errorf("invalid response %s, result of running validation on request payload was: %s", res.Status, info)
	}

	// Read page URL and signon URL from response
	var result = struct {
		SignOnURL string `json:"signOnUrl"`
		Page      struct {
			URL string `json:"uri"`
		} `json:"next"`
	}{}
	if err = json.Unmarshal([]byte(resBody), &result); err != nil {
		return nil, nil, fmt.Errorf("invalid response %v", err)
	}
	pageURL, err = url.Parse(result.Page.URL)
	if err != nil {
		return nil, nil, fmt.Errorf("invalid response %v", err)
	}
	signOnURL, err = url.Parse(result.SignOnURL)
	if err != nil {
		return nil, nil, fmt.Errorf("invalid response %v", err)
	}

	return pageURL, signOnURL, nil

}