Skip to content
This repository was archived by the owner on Aug 24, 2024. It is now read-only.

Commit

Permalink
GetPages
Browse files Browse the repository at this point in the history
  • Loading branch information
nint8835 committed Feb 25, 2024
1 parent db437de commit b218638
Show file tree
Hide file tree
Showing 3 changed files with 111 additions and 0 deletions.
28 changes: 28 additions & 0 deletions client.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package instatus_go

import (
"encoding/json"
"fmt"
"net/http"
"net/url"
)

type authenticatedRoundtripper struct {
Expand All @@ -17,6 +20,31 @@ type Client struct {
httpClient *http.Client
}

func (c *Client) get(url *url.URL, target any) error {
resp, err := c.httpClient.Get(url.String())
if err != nil {
return err
}
defer resp.Body.Close()

if resp.StatusCode != http.StatusOK {
var e Error
err = json.NewDecoder(resp.Body).Decode(&e)
if err != nil {
return fmt.Errorf("error decoding error response: %w", err)
}

return e
}

err = json.NewDecoder(resp.Body).Decode(target)
if err != nil {
return fmt.Errorf("error decoding response: %w", err)
}

return nil
}

func New(key string) *Client {
return &Client{
httpClient: &http.Client{
Expand Down
23 changes: 23 additions & 0 deletions common.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package instatus_go

import (
"fmt"
"net/url"
)

const BaseUrl = "https://api.instatus.com"
Expand All @@ -16,3 +17,25 @@ type Error struct {
func (e Error) Error() string {
return fmt.Sprintf("%s: %s", e.Details.Code, e.Details.Message)
}

type PageDetails struct {
Page int
PerPage int
}

func applyPagination(targetUrl *url.URL, details PageDetails) {
page := details.Page
if page == 0 {
page = 1
}

perPage := details.PerPage
if perPage == 0 {
perPage = 50
}

q := targetUrl.Query()
q.Set("page", fmt.Sprintf("%d", page))
q.Set("per_page", fmt.Sprintf("%d", perPage))
targetUrl.RawQuery = q.Encode()
}
60 changes: 60 additions & 0 deletions pages.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package instatus_go

import (
"net/url"
"time"
)

type Page struct {
Id string `json:"id"`
Subdomain string `json:"subdomain"`
Name string `json:"name"`
WorkspaceId string `json:"workspaceId"`
LogoUrl *string `json:"logoUrl"`
FaviconUrl *string `json:"faviconUrl"`
WebsiteUrl *string `json:"websiteUrl"`
CustomDomain *string `json:"customDomain"`
PublicEmail *string `json:"publicEmail"`
Twitter *string `json:"twitter"`
Status string `json:"status"`
SubscribeBySms bool `json:"subscribeBySms"`
SendSmsNotifications bool `json:"sendSmsNotifications"`
Language string `json:"language"`
UseLargeHeader bool `json:"useLargeHeader"`
BrandColor string `json:"brandColor"`
OkColor string `json:"okColor"`
DisruptedColor string `json:"disruptedColor"`
DegradedColor string `json:"degradedColor"`
DownColor string `json:"downColor"`
NoticeColor string `json:"noticeColor"`
UnknownColor string `json:"unknownColor"`
GoogleAnalytics *string `json:"googleAnalytics"`
SmsService *string `json:"smsService"`
HtmlInMeta *string `json:"htmlInMeta"`
HtmlAboveHeader *string `json:"htmlAboveHeader"`
HtmlBelowHeader *string `json:"htmlBelowHeader"`
HtmlAboveFooter *string `json:"htmlAboveFooter"`
HtmlBelowFooter *string `json:"htmlBelowFooter"`
HtmlBelowSummary *string `json:"htmlBelowSummary"`
UptimeDaysDisplay string `json:"uptimeDaysDisplay"`
UptimeOutageDisplay string `json:"uptimeOutageDisplay"`
LaunchDate *time.Time `json:"launchDate"`
CssGlobal *string `json:"cssGlobal"`
Onboarded *bool `json:"onboarded"`
CreatedAt time.Time `json:"createdAt"`
UpdatedAt time.Time `json:"updatedAt"`
}

type GetPagesRequest struct {
PageDetails
}

func (c *Client) GetPages(params GetPagesRequest) ([]Page, error) {
targetUrl, _ := url.Parse(BaseUrl + "/v2/pages")
applyPagination(targetUrl, params.PageDetails)

var pages []Page
err := c.get(targetUrl, &pages)

return pages, err
}

0 comments on commit b218638

Please sign in to comment.