Пример #1
0
// GetDeveloper retrieves the developer for the given token.
func GetDeveloper(token string) (*schemas.Developer, error) {
	res, err := http.Get(Host + strings.Replace(MePath, "{token}", token, -1))
	if err != nil {
		return nil, errors.NewStackError(err)
	}
	defer res.Body.Close()

	// Decode json response.
	devRes := new(responses.DeveloperRes)
	decoder := json.NewDecoder(res.Body)
	err = decoder.Decode(devRes)
	if err != nil {
		return nil, errors.NewStackError(err)
	}

	// Found so return the developer.
	if devRes.Status == "found" {
		return devRes.Developer, nil
	}

	if strings.Contains(devRes.Error(), "Invalid Token") {
		return nil, errors.ErrInvalidToken
	}

	// Non "found" status indicates error.
	return nil, errors.NewStackError(devRes)
}
Пример #2
0
// CreateDeveloper creates a new developer.
func CreateDeveloper(name, email, password string) (*schemas.Developer, error) {
	var body bytes.Buffer
	bodyReq := &LoginReq{Name: name, Email: email, Password: password}

	encoder := json.NewEncoder(&body)
	err := encoder.Encode(bodyReq)
	if err != nil {
		return nil, errors.NewStackError(err)
	}

	res, err := http.Post(Host+CreateDeveloperPath, "application/json", &body)
	if err != nil {
		return nil, errors.NewStackError(err)
	}
	defer res.Body.Close()

	// Decode json response.
	createRes := new(responses.DeveloperRes)
	decoder := json.NewDecoder(res.Body)
	err = decoder.Decode(createRes)
	if err != nil {
		return nil, errors.NewStackError(err)
	}

	// Created, just return token.
	if createRes.Status == "created" {
		return createRes.Developer, nil
	}

	// If the error is about developer existing, don't create stack error.
	if strings.Contains(createRes.Error(), "email already exists") {
		return nil, errors.ErrDeveloperExists
	}

	// Check for license issues.
	if strings.Contains(createRes.Error(), "License expired.") ||
		strings.Contains(createRes.Error(), "License user limit reached.") {
		return nil, createRes
	}

	// Non "created" status indicates error, just return invalid.
	return nil, errors.NewStackError(createRes)
}