Skip to content

Commit

Permalink
chore: create generic clickup/Client.get method
Browse files Browse the repository at this point in the history
  • Loading branch information
prgres committed May 25, 2024
1 parent 6744b78 commit 53e9823
Show file tree
Hide file tree
Showing 8 changed files with 146 additions and 253 deletions.
18 changes: 17 additions & 1 deletion api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package api

import (
"log/slog"
"slices"

"github.com/charmbracelet/log"

Expand Down Expand Up @@ -61,7 +62,7 @@ func (m *Api) GetSpaces(teamId string) ([]clickup.Space, error) {
client := m.Clickup

m.logger.Debugf("Fetching spaces from API")
spaces, err := client.GetSpaces(teamId)
spaces, err := client.GetSpacesFromTeam(teamId)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -272,6 +273,7 @@ func (m *Api) GetViewsFromFolder(folderId string) ([]clickup.View, error) {
if err != nil {
return nil, err
}
views = filterViews(views, []clickup.ViewType{clickup.ViewTypeList})
m.logger.Debugf("Found %d views in folder %s", len(views), folderId)

m.Cache.Set(cacheNamespace, folderId, views)
Expand Down Expand Up @@ -301,6 +303,7 @@ func (m *Api) GetViewsFromList(listId string) ([]clickup.View, error) {
if err != nil {
return nil, err
}
views = filterViews(views, []clickup.ViewType{clickup.ViewTypeList})
m.logger.Debugf("Found %d views in folder %s", len(views), listId)

m.Cache.Set(cacheNamespace, listId, views)
Expand Down Expand Up @@ -331,6 +334,7 @@ func (m *Api) GetViewsFromSpace(spaceId string) ([]clickup.View, error) {
if err != nil {
return nil, err
}
views = filterViews(views, []clickup.ViewType{clickup.ViewTypeList})
m.logger.Debugf("Found %d views in space %s", len(views), spaceId)

m.Cache.Set(cacheNamespace, spaceId, views)
Expand Down Expand Up @@ -383,6 +387,7 @@ func (m *Api) GetViewsFromWorkspace(workspaceId string) ([]clickup.View, error)
if err != nil {
return nil, err
}
views = filterViews(views, []clickup.ViewType{clickup.ViewTypeList})
m.logger.Debugf("Found %d views in workspace %s", len(views), workspaceId)

m.Cache.Set(cacheNamespace, workspaceId, views)
Expand Down Expand Up @@ -466,3 +471,14 @@ func (m *Api) InvalidateCache() error {
}
return nil
}

func filterViews(views []clickup.View, filters []clickup.ViewType) []clickup.View {
result := []clickup.View{}
for i := range views {
if slices.Contains(filters, views[i].Type) {
result = append(result, views[i])
}
}

return result
}
25 changes: 25 additions & 0 deletions pkg/clickup/clickup.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,3 +101,28 @@ func (c *Client) parseQueryParams(p ...string) (string, error) {

return v.Encode(), nil
}

type RequestGet interface {
Error() string
}

func (c *Client) get(url string, objmap RequestGet) error {
errMsg := "Error occurs while getting resources from url: %s. Error: %s. Raw data: %s"
errApiMsg := errMsg + " API response: %s"

rawData, err := c.requestGet(url)
if err != nil {
return fmt.Errorf(errMsg, url, err, "none")
}

if err := json.Unmarshal(rawData, objmap); err != nil {
return fmt.Errorf(errApiMsg, url, err, string(rawData))
}

if objmap.Error() != "" {
return fmt.Errorf(
errMsg, url, "API response contains error.", string(rawData))
}

return nil
}
17 changes: 9 additions & 8 deletions pkg/clickup/folder.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package clickup

import "encoding/json"

type Folder struct {
Id string `json:"id"`
Name string `json:"name"`
Expand All @@ -21,17 +19,20 @@ type FolderSpace struct {

type RequestGetFolders struct {
Folders []Folder `json:"folders"`
Err string `json:"err"`
}

func (r RequestGetFolders) Error() string {
return r.Err
}

func (c *Client) GetFolders(spaceId string) ([]Folder, error) {
rawData, err := c.requestGet("/space/" + spaceId + "/folder")
if err != nil {
return nil, err
}
return c.getFolders("/space/" + spaceId + "/folder")
}

func (c *Client) getFolders(url string) ([]Folder, error) {
var objmap RequestGetFolders

if err := json.Unmarshal(rawData, &objmap); err != nil {
if err := c.get(url, &objmap); err != nil {
return nil, err
}

Expand Down
31 changes: 15 additions & 16 deletions pkg/clickup/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package clickup

import (
"encoding/json"
"fmt"
)

type ListFolder struct {
Expand Down Expand Up @@ -36,33 +35,33 @@ type List struct {
}

type RequestGetLists struct {
Err string `json:"err"`
Lists []List `json:"lists"`
Err string `json:"err"`
}

func (r RequestGetLists) Error() string {
return r.Err
}

func (c *Client) GetListsFromFolder(folderId string) ([]List, error) {
rawData, err := c.requestGet("/folder/" + folderId + "/list")
if err != nil {
return nil, err
}
var objmap RequestGetLists
return c.getLists("/folder/" + folderId + "/list")
}

if err := json.Unmarshal(rawData, &objmap); err != nil {
func (c *Client) getLists(url string) ([]List, error) {
var objmap RequestGetLists
if err := c.get(url, &objmap); err != nil {
return nil, err
}

if objmap.Err != "" {
return nil, fmt.Errorf(
"error occurs while getting lists from folders: %s. API response: %s",
folderId, string(rawData))
}

return objmap.Lists, nil
}

type RequestGetList struct {
Err string `json:"err"`
List List `json:"list"`
Err string `json:"err"`
}

func (r RequestGetList) Error() string {
return r.Err
}

func (c *Client) GetList(listId string) (List, error) {
Expand Down
27 changes: 10 additions & 17 deletions pkg/clickup/space.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
package clickup

import (
"encoding/json"
"fmt"
)

type Space struct {
Id string `json:"id"`
Name string `json:"name"`
Expand All @@ -22,24 +17,22 @@ type SpaceStatus struct {
}

type RequestGetSpaces struct {
Err string `json:"err"`
Spaces []Space `json:"spaces"`
Err string `json:"err"`
}

func (c *Client) GetSpaces(teamId string) ([]Space, error) {
rawData, err := c.requestGet("/team/" + teamId + "/space")
if err != nil {
return nil, err
}
func (r RequestGetSpaces) Error() string {
return r.Err
}

func (c *Client) GetSpacesFromTeam(teamId string) ([]Space, error) {
return c.getSpaces("/team/" + teamId + "/space")
}

func (c *Client) getSpaces(url string) ([]Space, error) {
var objmap RequestGetSpaces
if err := json.Unmarshal(rawData, &objmap); err != nil {
if err := c.get(url, &objmap); err != nil {
return nil, err
}

if objmap.Err != "" {
return nil, fmt.Errorf(objmap.Err)
}

return objmap.Spaces, nil
}
56 changes: 20 additions & 36 deletions pkg/clickup/task.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,54 +108,30 @@ type Creator struct {
}

type RequestGetTasks struct {
Err string `json:"err"`
Tasks []Task `json:"tasks"`
LastPage bool `json:"last_page"`
Err string `json:"err"`
}

func (r RequestGetTasks) Error() string {
return r.Err
}

type RequestGetTask struct {
Err string `json:"err"`
Task Task `json:"task"`
Err string `json:"err"`
}

func (c *Client) GetTasksFromView(viewId string) ([]Task, error) {
rawData, err := c.requestGet("/view/" + viewId + "/task")
if err != nil {
return nil, err
}
var objmap RequestGetTasks

if err := json.Unmarshal(rawData, &objmap); err != nil {
return nil, err
}

if objmap.Err != "" {
return nil, fmt.Errorf(
"error occurs while getting tasks from view: %s. API response: %s",
viewId, string(rawData))
}
func (r RequestGetTask) Error() string {
return r.Err
}

return objmap.Tasks, nil
func (c *Client) GetTasksFromView(viewId string) ([]Task, error) {
return c.getTasks("/view/" + viewId + "/task")
}

func (c *Client) GetTasksFromList(listId string) ([]Task, error) {
rawData, err := c.requestGet("/list/" + listId + "/task")
if err != nil {
return nil, err
}
var objmap RequestGetTasks

if err := json.Unmarshal(rawData, &objmap); err != nil {
return nil, err
}

if objmap.Err != "" {
return nil, fmt.Errorf(
"error occurs while getting tasks from list: %s. API response: %s",
listId, string(rawData))
}

return objmap.Tasks, nil
return c.getTasks("/list/" + listId + "/task")
}

func (c *Client) GetTask(taskId string) (Task, error) {
Expand All @@ -171,3 +147,11 @@ func (c *Client) GetTask(taskId string) (Task, error) {

return objmap, nil
}

func (c *Client) getTasks(url string) ([]Task, error) {
var objmap RequestGetTasks
if err := c.get(url, &objmap); err != nil {
return nil, err
}
return objmap.Tasks, nil
}
17 changes: 9 additions & 8 deletions pkg/clickup/team.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package clickup

import "encoding/json"

type Workspace = Team

type Team struct {
Expand All @@ -14,18 +12,21 @@ type Team struct {

type RequestGetTeams struct {
Teams []Team `json:"teams"`
Err string `json:"err"`
}

func (r RequestGetTeams) Error() string {
return r.Err
}

func (c *Client) GetTeams() ([]Team, error) {
rawData, err := c.requestGet("/team")
if err != nil {
return nil, err
}
return c.getTeams("/team")
}

func (c *Client) getTeams(url string) ([]Team, error) {
var objmap RequestGetTeams
if err := json.Unmarshal(rawData, &objmap); err != nil {
if err := c.get(url, &objmap); err != nil {
return nil, err
}

return objmap.Teams, nil
}
Loading

0 comments on commit 53e9823

Please sign in to comment.