Mercurial > hg > Members > anatofuz > growsync
view client/page.go @ 11:194dd547e5c5
bump api
author | anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp> |
---|---|
date | Mon, 14 Dec 2020 22:54:23 +0900 |
parents | 90376341ed28 |
children | 2bfcdd284ce5 |
line wrap: on
line source
package client import ( "context" "encoding/json" "errors" "fmt" "net/http" "net/url" "time" "golang.org/x/xerrors" ) type PagesService service type User struct { IsGravatarEnabled bool `json:"isGravatarEnabled"` IsEmailPublished bool `json:"isEmailPublished"` Lang string `json:"lang"` Status int `json:"status"` Admin bool `json:"admin"` ID string `json:"_id"` CreatedAt time.Time `json:"createdAt"` Name string `json:"name"` Username string `json:"username"` Email string `json:"email"` LastLoginAt time.Time `json:"lastLoginAt"` ImageURLCached string `json:"imageUrlCached"` } type Revision struct { Format string `json:"format"` ID string `json:"_id"` CreatedAt time.Time `json:"createdAt"` Path string `json:"path"` Body string `json:"body"` Author User `json:"author"` HasDiffToPrev bool `json:"hasDiffToPrev"` V int `json:"__v"` } type Page struct { Status string `json:"status"` Grant int `json:"grant"` GrantedUsers []interface{} `json:"grantedUsers"` Liker []interface{} `json:"liker"` SeenUsers []string `json:"seenUsers"` CommentCount int `json:"commentCount"` Extended string `json:"extended"` SubID string `json:"_id"` CreatedAt time.Time `json:"createdAt"` UpdatedAt time.Time `json:"updatedAt"` Path string `json:"path"` Creator User `json:"creator"` LastUpdateUser User `json:"lastUpdateUser"` RedirectTo interface{} `json:"redirectTo"` GrantedGroup interface{} `json:"grantedGroup"` V int `json:"__v"` ID string `json:"id"` Revision Revision `json:"revision"` } type PagesGet struct { Page Page `json:"page"` Ok bool `json:"ok"` Error string `json:"error"` } type PagesCreate struct { Page struct { Status string `json:"status"` Grant int `json:"grant"` GrantedUsers []string `json:"grantedUsers"` Liker []interface{} `json:"liker"` SeenUsers []interface{} `json:"seenUsers"` CommentCount int `json:"commentCount"` Extended string `json:"extended"` SubID string `json:"_id"` CreatedAt time.Time `json:"createdAt"` UpdatedAt time.Time `json:"updatedAt"` Path string `json:"path"` Creator User `json:"creator"` LastUpdateUser User `json:"lastUpdateUser"` RedirectTo interface{} `json:"redirectTo"` GrantedGroup interface{} `json:"grantedGroup"` V int `json:"__v"` Revision string `json:"revision"` ID string `json:"id"` } `json:"page"` Revision Revision `json:"revision"` Ok bool `json:"ok"` Error string `json:"error"` } type UpdateParams struct { PageID string RevisionID string } var ErrorPageNotFOund = errors.New("page not found") const CREATE_ENDPOINT string = "/_api/v3/pages.create" // Create makes a page in your Crowi. The request requires // the path and page content used for the page name func (p *PagesService) Create(ctx context.Context, path, body string) (*PagesCreate, error) { fmt.Printf("path %s, body %s\n", path, body) params := url.Values{} params.Add("access_token", p.client.config.Token) params.Add("path", path) params.Add("body", body) res, err := p.client.newRequest(ctx, http.MethodPost, CREATE_ENDPOINT, ¶ms) if err != nil { return nil, xerrors.Errorf("[error] failed send client request %s, %+w", string(res), err) } createPages := PagesCreate{} if err := json.Unmarshal(res, &createPages); err != nil { return nil, xerrors.Errorf("[error] failed unmarshal json %s, %+w", string(res), err) } return &createPages, nil } const GET_ENDPOINT string = "/_api/v3/pages.get" func (p *PagesService) Get(ctx context.Context, path string) (*Page, error) { params := url.Values{} params.Add("access_token", p.client.config.Token) params.Add("path", path) res, err := p.client.newRequest(ctx, http.MethodGet, GET_ENDPOINT, ¶ms) if err != nil { return nil, xerrors.Errorf("failed get page %+w", err) } pagesGet := PagesGet{} if err := json.Unmarshal(res, &pagesGet); err != nil { return nil, err } if !pagesGet.Ok { return nil, ErrorPageNotFOund } return &pagesGet.Page, nil } const UpdateEndpoint string = "/_api/v3/pages.update" func (p *PagesService) Update(ctx context.Context, pageID, revisionID, body string) (*Page, error) { params := url.Values{} params.Add("access_token", p.client.config.Token) params.Add("page_id", pageID) params.Add("revision_id", revisionID) params.Add("body", body) res, err := p.client.newRequest(ctx, http.MethodPost, UpdateEndpoint, ¶ms) if err != nil { return nil, err } page := Page{} if err := json.Unmarshal(res, &page); err != nil { return nil, xerrors.Errorf("[error] failed unmarshal json %s, %+w", string(res), err) } return &page, nil }