···
14
+
"tangled.sh/tangled.sh/core/types"
17
+
type UnsignedClient struct {
22
+
func NewUnsignedClient(domain string, dev bool) (*UnsignedClient, error) {
23
+
client := &http.Client{
24
+
Timeout: 5 * time.Second,
31
+
url, err := url.Parse(fmt.Sprintf("%s://%s", scheme, domain))
36
+
unsignedClient := &UnsignedClient{
41
+
return unsignedClient, nil
44
+
func (us *UnsignedClient) newRequest(method, endpoint string, query url.Values, body []byte) (*http.Request, error) {
45
+
reqUrl := us.Url.JoinPath(endpoint)
47
+
// add query parameters
49
+
reqUrl.RawQuery = query.Encode()
52
+
return http.NewRequest(method, reqUrl.String(), bytes.NewReader(body))
55
+
func (us *UnsignedClient) Index(ownerDid, repoName, ref string) (*http.Response, error) {
60
+
endpoint := fmt.Sprintf("/%s/%s/tree/%s", ownerDid, repoName, ref)
62
+
endpoint = fmt.Sprintf("/%s/%s", ownerDid, repoName)
65
+
req, err := us.newRequest(Method, endpoint, nil, nil)
70
+
return us.client.Do(req)
73
+
func (us *UnsignedClient) Log(ownerDid, repoName, ref string, page int) (*http.Response, error) {
78
+
endpoint := fmt.Sprintf("/%s/%s/log/%s", ownerDid, repoName, url.PathEscape(ref))
80
+
query := url.Values{}
81
+
query.Add("page", strconv.Itoa(page))
82
+
query.Add("per_page", strconv.Itoa(60))
84
+
req, err := us.newRequest(Method, endpoint, query, nil)
89
+
return us.client.Do(req)
92
+
func (us *UnsignedClient) Branches(ownerDid, repoName string) (*http.Response, error) {
97
+
endpoint := fmt.Sprintf("/%s/%s/branches", ownerDid, repoName)
99
+
req, err := us.newRequest(Method, endpoint, nil, nil)
104
+
return us.client.Do(req)
107
+
func (us *UnsignedClient) Tags(ownerDid, repoName string) (*types.RepoTagsResponse, error) {
112
+
endpoint := fmt.Sprintf("/%s/%s/tags", ownerDid, repoName)
114
+
req, err := us.newRequest(Method, endpoint, nil, nil)
119
+
resp, err := us.client.Do(req)
124
+
body, err := io.ReadAll(resp.Body)
129
+
var result types.RepoTagsResponse
130
+
err = json.Unmarshal(body, &result)
135
+
return &result, nil
138
+
func (us *UnsignedClient) Branch(ownerDid, repoName, branch string) (*http.Response, error) {
143
+
endpoint := fmt.Sprintf("/%s/%s/branches/%s", ownerDid, repoName, url.PathEscape(branch))
145
+
req, err := us.newRequest(Method, endpoint, nil, nil)
150
+
return us.client.Do(req)
153
+
func (us *UnsignedClient) DefaultBranch(ownerDid, repoName string) (*types.RepoDefaultBranchResponse, error) {
158
+
endpoint := fmt.Sprintf("/%s/%s/branches/default", ownerDid, repoName)
160
+
req, err := us.newRequest(Method, endpoint, nil, nil)
165
+
resp, err := us.client.Do(req)
169
+
defer resp.Body.Close()
171
+
var defaultBranch types.RepoDefaultBranchResponse
172
+
if err := json.NewDecoder(resp.Body).Decode(&defaultBranch); err != nil {
176
+
return &defaultBranch, nil
179
+
func (us *UnsignedClient) Capabilities() (*types.Capabilities, error) {
182
+
Endpoint = "/capabilities"
185
+
req, err := us.newRequest(Method, Endpoint, nil, nil)
190
+
resp, err := us.client.Do(req)
194
+
defer resp.Body.Close()
196
+
var capabilities types.Capabilities
197
+
if err := json.NewDecoder(resp.Body).Decode(&capabilities); err != nil {
201
+
return &capabilities, nil
204
+
func (us *UnsignedClient) Compare(ownerDid, repoName, rev1, rev2 string) (*types.RepoFormatPatchResponse, error) {
209
+
endpoint := fmt.Sprintf("/%s/%s/compare/%s/%s", ownerDid, repoName, url.PathEscape(rev1), url.PathEscape(rev2))
211
+
req, err := us.newRequest(Method, endpoint, nil, nil)
213
+
return nil, fmt.Errorf("Failed to create request.")
216
+
compareResp, err := us.client.Do(req)
218
+
return nil, fmt.Errorf("Failed to create request.")
220
+
defer compareResp.Body.Close()
222
+
switch compareResp.StatusCode {
225
+
return nil, fmt.Errorf("Branch comparisons not supported on this knot.")
228
+
respBody, err := io.ReadAll(compareResp.Body)
230
+
log.Println("failed to compare across branches")
231
+
return nil, fmt.Errorf("Failed to compare branches.")
233
+
defer compareResp.Body.Close()
235
+
var formatPatchResponse types.RepoFormatPatchResponse
236
+
err = json.Unmarshal(respBody, &formatPatchResponse)
238
+
log.Println("failed to unmarshal format-patch response", err)
239
+
return nil, fmt.Errorf("failed to compare branches.")
242
+
return &formatPatchResponse, nil