From 6271328740143067ec263af66be0493e7ca58693 Mon Sep 17 00:00:00 2001 From: oppiliappan Date: Tue, 20 May 2025 17:40:29 +0100 Subject: [PATCH] knotclient: move unsigned endpoints into knotclient/unsigned --- knotclient/signer.go | 231 --------------------------------------- knotclient/unsigned.go | 243 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 243 insertions(+), 231 deletions(-) create mode 100644 knotclient/unsigned.go diff --git a/knotclient/signer.go b/knotclient/signer.go index 1cded40c..9098ad1e 100644 --- a/knotclient/signer.go +++ b/knotclient/signer.go @@ -7,11 +7,8 @@ import ( "encoding/hex" "encoding/json" "fmt" - "io" - "log" "net/http" "net/url" - "strconv" "time" "tangled.sh/tangled.sh/core/types" @@ -335,231 +332,3 @@ func (s *SignedClient) NewHiddenRef(ownerDid, targetRepo, forkBranch, remoteBran return s.client.Do(req) } - -type UnsignedClient struct { - Url *url.URL - client *http.Client -} - -func NewUnsignedClient(domain string, dev bool) (*UnsignedClient, error) { - client := &http.Client{ - Timeout: 5 * time.Second, - } - - scheme := "https" - if dev { - scheme = "http" - } - url, err := url.Parse(fmt.Sprintf("%s://%s", scheme, domain)) - if err != nil { - return nil, err - } - - unsignedClient := &UnsignedClient{ - client: client, - Url: url, - } - - return unsignedClient, nil -} - -func (us *UnsignedClient) newRequest(method, endpoint string, query url.Values, body []byte) (*http.Request, error) { - reqUrl := us.Url.JoinPath(endpoint) - - // add query parameters - if query != nil { - reqUrl.RawQuery = query.Encode() - } - - return http.NewRequest(method, reqUrl.String(), bytes.NewReader(body)) -} - -func (us *UnsignedClient) Index(ownerDid, repoName, ref string) (*http.Response, error) { - const ( - Method = "GET" - ) - - endpoint := fmt.Sprintf("/%s/%s/tree/%s", ownerDid, repoName, ref) - if ref == "" { - endpoint = fmt.Sprintf("/%s/%s", ownerDid, repoName) - } - - req, err := us.newRequest(Method, endpoint, nil, nil) - if err != nil { - return nil, err - } - - return us.client.Do(req) -} - -func (us *UnsignedClient) Log(ownerDid, repoName, ref string, page int) (*http.Response, error) { - const ( - Method = "GET" - ) - - endpoint := fmt.Sprintf("/%s/%s/log/%s", ownerDid, repoName, url.PathEscape(ref)) - - query := url.Values{} - query.Add("page", strconv.Itoa(page)) - query.Add("per_page", strconv.Itoa(60)) - - req, err := us.newRequest(Method, endpoint, query, nil) - if err != nil { - return nil, err - } - - return us.client.Do(req) -} - -func (us *UnsignedClient) Branches(ownerDid, repoName string) (*http.Response, error) { - const ( - Method = "GET" - ) - - endpoint := fmt.Sprintf("/%s/%s/branches", ownerDid, repoName) - - req, err := us.newRequest(Method, endpoint, nil, nil) - if err != nil { - return nil, err - } - - return us.client.Do(req) -} - -func (us *UnsignedClient) Tags(ownerDid, repoName string) (*types.RepoTagsResponse, error) { - const ( - Method = "GET" - ) - - endpoint := fmt.Sprintf("/%s/%s/tags", ownerDid, repoName) - - req, err := us.newRequest(Method, endpoint, nil, nil) - if err != nil { - return nil, err - } - - resp, err := us.client.Do(req) - if err != nil { - return nil, err - } - - body, err := io.ReadAll(resp.Body) - if err != nil { - return nil, err - } - - var result types.RepoTagsResponse - err = json.Unmarshal(body, &result) - if err != nil { - return nil, err - } - - return &result, nil -} - -func (us *UnsignedClient) Branch(ownerDid, repoName, branch string) (*http.Response, error) { - const ( - Method = "GET" - ) - - endpoint := fmt.Sprintf("/%s/%s/branches/%s", ownerDid, repoName, url.PathEscape(branch)) - - req, err := us.newRequest(Method, endpoint, nil, nil) - if err != nil { - return nil, err - } - - return us.client.Do(req) -} - -func (us *UnsignedClient) DefaultBranch(ownerDid, repoName string) (*types.RepoDefaultBranchResponse, error) { - const ( - Method = "GET" - ) - - endpoint := fmt.Sprintf("/%s/%s/branches/default", ownerDid, repoName) - - req, err := us.newRequest(Method, endpoint, nil, nil) - if err != nil { - return nil, err - } - - resp, err := us.client.Do(req) - if err != nil { - return nil, err - } - defer resp.Body.Close() - - var defaultBranch types.RepoDefaultBranchResponse - if err := json.NewDecoder(resp.Body).Decode(&defaultBranch); err != nil { - return nil, err - } - - return &defaultBranch, nil -} - -func (us *UnsignedClient) Capabilities() (*types.Capabilities, error) { - const ( - Method = "GET" - Endpoint = "/capabilities" - ) - - req, err := us.newRequest(Method, Endpoint, nil, nil) - if err != nil { - return nil, err - } - - resp, err := us.client.Do(req) - if err != nil { - return nil, err - } - defer resp.Body.Close() - - var capabilities types.Capabilities - if err := json.NewDecoder(resp.Body).Decode(&capabilities); err != nil { - return nil, err - } - - return &capabilities, nil -} - -func (us *UnsignedClient) Compare(ownerDid, repoName, rev1, rev2 string) (*types.RepoFormatPatchResponse, error) { - const ( - Method = "GET" - ) - - endpoint := fmt.Sprintf("/%s/%s/compare/%s/%s", ownerDid, repoName, url.PathEscape(rev1), url.PathEscape(rev2)) - - req, err := us.newRequest(Method, endpoint, nil, nil) - if err != nil { - return nil, fmt.Errorf("Failed to create request.") - } - - compareResp, err := us.client.Do(req) - if err != nil { - return nil, fmt.Errorf("Failed to create request.") - } - defer compareResp.Body.Close() - - switch compareResp.StatusCode { - case 404: - case 400: - return nil, fmt.Errorf("Branch comparisons not supported on this knot.") - } - - respBody, err := io.ReadAll(compareResp.Body) - if err != nil { - log.Println("failed to compare across branches") - return nil, fmt.Errorf("Failed to compare branches.") - } - defer compareResp.Body.Close() - - var formatPatchResponse types.RepoFormatPatchResponse - err = json.Unmarshal(respBody, &formatPatchResponse) - if err != nil { - log.Println("failed to unmarshal format-patch response", err) - return nil, fmt.Errorf("failed to compare branches.") - } - - return &formatPatchResponse, nil -} diff --git a/knotclient/unsigned.go b/knotclient/unsigned.go new file mode 100644 index 00000000..f9d31dc2 --- /dev/null +++ b/knotclient/unsigned.go @@ -0,0 +1,243 @@ +package knotclient + +import ( + "bytes" + "encoding/json" + "fmt" + "io" + "log" + "net/http" + "net/url" + "strconv" + "time" + + "tangled.sh/tangled.sh/core/types" +) + +type UnsignedClient struct { + Url *url.URL + client *http.Client +} + +func NewUnsignedClient(domain string, dev bool) (*UnsignedClient, error) { + client := &http.Client{ + Timeout: 5 * time.Second, + } + + scheme := "https" + if dev { + scheme = "http" + } + url, err := url.Parse(fmt.Sprintf("%s://%s", scheme, domain)) + if err != nil { + return nil, err + } + + unsignedClient := &UnsignedClient{ + client: client, + Url: url, + } + + return unsignedClient, nil +} + +func (us *UnsignedClient) newRequest(method, endpoint string, query url.Values, body []byte) (*http.Request, error) { + reqUrl := us.Url.JoinPath(endpoint) + + // add query parameters + if query != nil { + reqUrl.RawQuery = query.Encode() + } + + return http.NewRequest(method, reqUrl.String(), bytes.NewReader(body)) +} + +func (us *UnsignedClient) Index(ownerDid, repoName, ref string) (*http.Response, error) { + const ( + Method = "GET" + ) + + endpoint := fmt.Sprintf("/%s/%s/tree/%s", ownerDid, repoName, ref) + if ref == "" { + endpoint = fmt.Sprintf("/%s/%s", ownerDid, repoName) + } + + req, err := us.newRequest(Method, endpoint, nil, nil) + if err != nil { + return nil, err + } + + return us.client.Do(req) +} + +func (us *UnsignedClient) Log(ownerDid, repoName, ref string, page int) (*http.Response, error) { + const ( + Method = "GET" + ) + + endpoint := fmt.Sprintf("/%s/%s/log/%s", ownerDid, repoName, url.PathEscape(ref)) + + query := url.Values{} + query.Add("page", strconv.Itoa(page)) + query.Add("per_page", strconv.Itoa(60)) + + req, err := us.newRequest(Method, endpoint, query, nil) + if err != nil { + return nil, err + } + + return us.client.Do(req) +} + +func (us *UnsignedClient) Branches(ownerDid, repoName string) (*http.Response, error) { + const ( + Method = "GET" + ) + + endpoint := fmt.Sprintf("/%s/%s/branches", ownerDid, repoName) + + req, err := us.newRequest(Method, endpoint, nil, nil) + if err != nil { + return nil, err + } + + return us.client.Do(req) +} + +func (us *UnsignedClient) Tags(ownerDid, repoName string) (*types.RepoTagsResponse, error) { + const ( + Method = "GET" + ) + + endpoint := fmt.Sprintf("/%s/%s/tags", ownerDid, repoName) + + req, err := us.newRequest(Method, endpoint, nil, nil) + if err != nil { + return nil, err + } + + resp, err := us.client.Do(req) + if err != nil { + return nil, err + } + + body, err := io.ReadAll(resp.Body) + if err != nil { + return nil, err + } + + var result types.RepoTagsResponse + err = json.Unmarshal(body, &result) + if err != nil { + return nil, err + } + + return &result, nil +} + +func (us *UnsignedClient) Branch(ownerDid, repoName, branch string) (*http.Response, error) { + const ( + Method = "GET" + ) + + endpoint := fmt.Sprintf("/%s/%s/branches/%s", ownerDid, repoName, url.PathEscape(branch)) + + req, err := us.newRequest(Method, endpoint, nil, nil) + if err != nil { + return nil, err + } + + return us.client.Do(req) +} + +func (us *UnsignedClient) DefaultBranch(ownerDid, repoName string) (*types.RepoDefaultBranchResponse, error) { + const ( + Method = "GET" + ) + + endpoint := fmt.Sprintf("/%s/%s/branches/default", ownerDid, repoName) + + req, err := us.newRequest(Method, endpoint, nil, nil) + if err != nil { + return nil, err + } + + resp, err := us.client.Do(req) + if err != nil { + return nil, err + } + defer resp.Body.Close() + + var defaultBranch types.RepoDefaultBranchResponse + if err := json.NewDecoder(resp.Body).Decode(&defaultBranch); err != nil { + return nil, err + } + + return &defaultBranch, nil +} + +func (us *UnsignedClient) Capabilities() (*types.Capabilities, error) { + const ( + Method = "GET" + Endpoint = "/capabilities" + ) + + req, err := us.newRequest(Method, Endpoint, nil, nil) + if err != nil { + return nil, err + } + + resp, err := us.client.Do(req) + if err != nil { + return nil, err + } + defer resp.Body.Close() + + var capabilities types.Capabilities + if err := json.NewDecoder(resp.Body).Decode(&capabilities); err != nil { + return nil, err + } + + return &capabilities, nil +} + +func (us *UnsignedClient) Compare(ownerDid, repoName, rev1, rev2 string) (*types.RepoFormatPatchResponse, error) { + const ( + Method = "GET" + ) + + endpoint := fmt.Sprintf("/%s/%s/compare/%s/%s", ownerDid, repoName, url.PathEscape(rev1), url.PathEscape(rev2)) + + req, err := us.newRequest(Method, endpoint, nil, nil) + if err != nil { + return nil, fmt.Errorf("Failed to create request.") + } + + compareResp, err := us.client.Do(req) + if err != nil { + return nil, fmt.Errorf("Failed to create request.") + } + defer compareResp.Body.Close() + + switch compareResp.StatusCode { + case 404: + case 400: + return nil, fmt.Errorf("Branch comparisons not supported on this knot.") + } + + respBody, err := io.ReadAll(compareResp.Body) + if err != nil { + log.Println("failed to compare across branches") + return nil, fmt.Errorf("Failed to compare branches.") + } + defer compareResp.Body.Close() + + var formatPatchResponse types.RepoFormatPatchResponse + err = json.Unmarshal(respBody, &formatPatchResponse) + if err != nil { + log.Println("failed to unmarshal format-patch response", err) + return nil, fmt.Errorf("failed to compare branches.") + } + + return &formatPatchResponse, nil +} -- 2.51.2