Example #1
0
// FetchTeams fetches teams that the authenticated user belongs.
func FetchTeams(client api.Client) (teams Teams, err error) {
	body, _, err := client.Get("", "/teams", nil)
	if err != nil {
		return
	}
	err = json.Unmarshal(body, &teams)
	return
}
Example #2
0
// Delete deletes a post in Qiita.
func (post *Post) Delete(client api.Client) (err error) {
	if post.ID == "" {
		err = EmptyIDError{}
		return
	}

	subDomain := ""
	if post.Team != nil {
		subDomain = post.Team.ID
	}
	_, _, err = client.Delete(subDomain, fmt.Sprintf("/items/%s", post.ID), post)
	return
}
Example #3
0
// FetchPost fetches a post from Qiita.
func FetchPost(client api.Client, team *Team, id string) (post Post, err error) {
	subDomain := ""
	if team != nil {
		subDomain = team.ID
	}
	body, _, err := client.Get(subDomain, fmt.Sprintf("/items/%s", id), nil)
	if err != nil {
		return
	}
	err = json.Unmarshal(body, &post)
	if err != nil {
		return
	}
	post.Team = team
	return
}
Example #4
0
// Update updates a post in Qiita.
func (post *Post) Update(client api.Client) (err error) {
	if post.ID == "" {
		err = EmptyIDError{}
		return
	}

	subDomain := ""
	if post.Team != nil {
		subDomain = post.Team.ID
	}
	body, _, err := client.Patch(subDomain, fmt.Sprintf("/items/%s", post.ID), post)
	if err != nil {
		return
	}
	err = json.Unmarshal(body, post)
	if err != nil {
		return
	}
	return
}
Example #5
0
// Create creates a new post in Qiita.
func (post *Post) Create(client api.Client, opts CreationOptions) (err error) {
	subDomain := ""
	if post.Team != nil {
		subDomain = post.Team.ID
	}

	cPost := CreationPost{
		Post:            *post,
		CreationOptions: opts,
	}
	body, _, err := client.Post(subDomain, "/items", cPost)
	if err != nil {
		return
	}

	err = json.Unmarshal(body, post)
	if err != nil {
		return
	}
	return
}
Example #6
0
// FetchPosts fetches posts from Qiita and Qiita:Team.
func FetchPosts(client api.Client, team *Team) (posts Posts, err error) {
	subDomain := ""
	if team != nil {
		subDomain = team.ID
	}

	v := url.Values{}
	v.Set("per_page", strconv.Itoa(perPage))
	for page := 1; ; page++ {
		v.Set("page", strconv.Itoa(page))

		body, header, err := client.Get(subDomain, "/authenticated_user/items", &v)
		if err != nil {
			return nil, err
		}

		var ps Posts
		err = json.Unmarshal(body, &ps)
		if err != nil {
			return nil, err
		}
		posts = append(posts, ps...)

		totalCount, err := strconv.Atoi(header.Get("Total-Count"))
		if err != nil {
			return nil, err
		}
		if perPage*page >= totalCount {
			break
		}
	}
	for i, post := range posts {
		post.Team = team
		posts[i] = post
	}
	return
}