Skip to content

Commit

Permalink
Add UpsertArticle to the go-client (#14)
Browse files Browse the repository at this point in the history
  • Loading branch information
nlathia authored Aug 12, 2024
2 parents 6768266 + 521a40a commit 983d90b
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 0 deletions.
32 changes: 32 additions & 0 deletions article.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package client

// Visibility describes who can view the given item (e.g. help article)
type Visibility string

const (
// VisibilityPublic means that the item is available to the general
// public. For example, it is published on a public website.
VisibilityPublic Visibility = "public"

// VisibilityUsers means that the item is only available to the
// company's customers. For example, it is only accessible via an
// app after sign-up.
VisibilityUsers Visibility = "users"

// VisibilityInternal means that the item is only available to
// the company's employees. For example, it is a procedure or SOP
// that customers do not have access to.
VisibilityInternal Visibility = "internal"
)

// PublicationStatus describes the status of a help article.
type PublicationStatus string

const (
// StatusDraft means that the article is being written or
// edited and is not published.
StatusDraft PublicationStatus = "draft"

// StatusPublished means that the article is published.
StatusPublished PublicationStatus = "published"
)
60 changes: 60 additions & 0 deletions article_upsert.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package client

import (
"context"
"encoding/json"
"net/http"
"time"
)

type UpsertArticleParams struct {
// AuthorID optionally identifies the (current) author of the article
AuthorID string `json:"author_id"`

// ID is the identifier that the company uses for this article
ID string `json:"id"`

// Title is the article's title. It may be empty.
Title string `json:"title"`

// Description is an article's tagline. It may be empty.
Description string `json:"description"`

// Body is the main contents of an article. It may be empty.
Body string `json:"body"`

// Visibility describes who can access this article, ranging from the
// whole world (public) through to employees only (internal).
Visibility Visibility `json:"visibility"`

// TopicExternalID optionally identifies the topic that this
// article is associated with. If given, you must have created
// the topic first
TopicID string `json:"topic_id"`

// Status describes whether this article is published or not.
Status PublicationStatus `json:"status"`

// Data optionally gives additional meta-data about the article.
Data json.RawMessage `json:"data"`

// Created is when the article was first authored.
Created time.Time `json:"created"`

// Updated is when the article was last changed.
Updated time.Time `json:"updated"`
}

// UpsertArticle inserts or updates a help article
func (c *Client) UpsertArticle(ctx context.Context, p *UpsertArticleParams) error {
rsp, err := c.makeRequest(ctx, http.MethodPost, "articles", p)
if err != nil {
return err
}
defer rsp.Body.Close()

if err := responseError(rsp); err != nil {
return err
}
return nil
}

0 comments on commit 983d90b

Please sign in to comment.