···
-
"tangled.sh/tangled.sh/core/types"
-
type UnsignedClient struct {
-
func NewUnsignedClient(domain string, dev bool) (*UnsignedClient, error) {
-
client := &http.Client{
-
Timeout: 5 * time.Second,
-
url, err := url.Parse(fmt.Sprintf("%s://%s", scheme, domain))
-
unsignedClient := &UnsignedClient{
-
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
-
reqUrl.RawQuery = query.Encode()
-
return http.NewRequest(method, reqUrl.String(), bytes.NewReader(body))
-
func do[T any](us *UnsignedClient, req *http.Request) (*T, error) {
-
resp, err := us.client.Do(req)
-
defer resp.Body.Close()
-
body, err := io.ReadAll(resp.Body)
-
log.Printf("Error reading response body: %v", err)
-
err = json.Unmarshal(body, &result)
-
log.Printf("Error unmarshalling response body: %v", err)
-
func (us *UnsignedClient) Index(ownerDid, repoName, ref string) (*types.RepoIndexResponse, error) {
-
endpoint := fmt.Sprintf("/%s/%s/tree/%s", ownerDid, repoName, ref)
-
endpoint = fmt.Sprintf("/%s/%s", ownerDid, repoName)
-
req, err := us.newRequest(Method, endpoint, nil, nil)
-
return do[types.RepoIndexResponse](us, req)
-
func (us *UnsignedClient) Log(ownerDid, repoName, ref string, page int) (*types.RepoLogResponse, error) {
-
endpoint := fmt.Sprintf("/%s/%s/log/%s", ownerDid, repoName, url.PathEscape(ref))
-
query.Add("page", strconv.Itoa(page))
-
query.Add("per_page", strconv.Itoa(60))
-
req, err := us.newRequest(Method, endpoint, query, nil)
-
return do[types.RepoLogResponse](us, req)
-
func (us *UnsignedClient) Branches(ownerDid, repoName string) (*types.RepoBranchesResponse, error) {
-
endpoint := fmt.Sprintf("/%s/%s/branches", ownerDid, repoName)
-
req, err := us.newRequest(Method, endpoint, nil, nil)
-
return do[types.RepoBranchesResponse](us, req)
-
func (us *UnsignedClient) Tags(ownerDid, repoName string) (*types.RepoTagsResponse, error) {
-
endpoint := fmt.Sprintf("/%s/%s/tags", ownerDid, repoName)
-
req, err := us.newRequest(Method, endpoint, nil, nil)
-
return do[types.RepoTagsResponse](us, req)
-
func (us *UnsignedClient) Branch(ownerDid, repoName, branch string) (*types.RepoBranchResponse, error) {
-
endpoint := fmt.Sprintf("/%s/%s/branches/%s", ownerDid, repoName, url.PathEscape(branch))
-
req, err := us.newRequest(Method, endpoint, nil, nil)
-
return do[types.RepoBranchResponse](us, req)
-
func (us *UnsignedClient) DefaultBranch(ownerDid, repoName string) (*types.RepoDefaultBranchResponse, error) {
-
endpoint := fmt.Sprintf("/%s/%s/branches/default", ownerDid, repoName)
-
req, err := us.newRequest(Method, endpoint, nil, nil)
-
resp, err := us.client.Do(req)
-
defer resp.Body.Close()
-
var defaultBranch types.RepoDefaultBranchResponse
-
if err := json.NewDecoder(resp.Body).Decode(&defaultBranch); err != nil {
-
return &defaultBranch, nil
-
func (us *UnsignedClient) Capabilities() (*types.Capabilities, error) {
-
Endpoint = "/capabilities"
-
req, err := us.newRequest(Method, Endpoint, nil, nil)
-
resp, err := us.client.Do(req)
-
defer resp.Body.Close()
-
var capabilities types.Capabilities
-
if err := json.NewDecoder(resp.Body).Decode(&capabilities); err != nil {
-
return &capabilities, nil
-
func (us *UnsignedClient) Compare(ownerDid, repoName, rev1, rev2 string) (*types.RepoFormatPatchResponse, error) {
-
endpoint := fmt.Sprintf("/%s/%s/compare/%s/%s", ownerDid, repoName, url.PathEscape(rev1), url.PathEscape(rev2))
-
req, err := us.newRequest(Method, endpoint, nil, nil)
-
return nil, fmt.Errorf("Failed to create request.")
-
compareResp, err := us.client.Do(req)
-
return nil, fmt.Errorf("Failed to create request.")
-
defer compareResp.Body.Close()
-
switch compareResp.StatusCode {
-
return nil, fmt.Errorf("Branch comparisons not supported on this knot.")
-
respBody, err := io.ReadAll(compareResp.Body)
-
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)
-
log.Println("failed to unmarshal format-patch response", err)
-
return nil, fmt.Errorf("failed to compare branches.")
-
return &formatPatchResponse, nil
-
func (s *UnsignedClient) RepoLanguages(ownerDid, repoName, ref string) (*types.RepoLanguageResponse, error) {
-
endpoint := fmt.Sprintf("/%s/%s/languages/%s", ownerDid, repoName, url.PathEscape(ref))
-
req, err := s.newRequest(Method, endpoint, nil, nil)
-
resp, err := s.client.Do(req)
-
var result types.RepoLanguageResponse
-
if resp.StatusCode != http.StatusOK {
-
log.Println("failed to calculate languages", resp.Status)
-
return &types.RepoLanguageResponse{}, nil
-
body, err := io.ReadAll(resp.Body)
-
err = json.Unmarshal(body, &result)