···
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 do[T any](us *UnsignedClient, req *http.Request) (*T, error) {
56
-
resp, err := us.client.Do(req)
60
-
defer resp.Body.Close()
62
-
body, err := io.ReadAll(resp.Body)
64
-
log.Printf("Error reading response body: %v", err)
69
-
err = json.Unmarshal(body, &result)
71
-
log.Printf("Error unmarshalling response body: %v", err)
78
-
func (us *UnsignedClient) Index(ownerDid, repoName, ref string) (*types.RepoIndexResponse, error) {
83
-
endpoint := fmt.Sprintf("/%s/%s/tree/%s", ownerDid, repoName, ref)
85
-
endpoint = fmt.Sprintf("/%s/%s", ownerDid, repoName)
88
-
req, err := us.newRequest(Method, endpoint, nil, nil)
93
-
return do[types.RepoIndexResponse](us, req)
96
-
func (us *UnsignedClient) Log(ownerDid, repoName, ref string, page int) (*types.RepoLogResponse, error) {
101
-
endpoint := fmt.Sprintf("/%s/%s/log/%s", ownerDid, repoName, url.PathEscape(ref))
103
-
query := url.Values{}
104
-
query.Add("page", strconv.Itoa(page))
105
-
query.Add("per_page", strconv.Itoa(60))
107
-
req, err := us.newRequest(Method, endpoint, query, nil)
112
-
return do[types.RepoLogResponse](us, req)
115
-
func (us *UnsignedClient) Branches(ownerDid, repoName string) (*types.RepoBranchesResponse, error) {
120
-
endpoint := fmt.Sprintf("/%s/%s/branches", ownerDid, repoName)
122
-
req, err := us.newRequest(Method, endpoint, nil, nil)
127
-
return do[types.RepoBranchesResponse](us, req)
130
-
func (us *UnsignedClient) Tags(ownerDid, repoName string) (*types.RepoTagsResponse, error) {
135
-
endpoint := fmt.Sprintf("/%s/%s/tags", ownerDid, repoName)
137
-
req, err := us.newRequest(Method, endpoint, nil, nil)
142
-
return do[types.RepoTagsResponse](us, req)
145
-
func (us *UnsignedClient) Branch(ownerDid, repoName, branch string) (*types.RepoBranchResponse, error) {
150
-
endpoint := fmt.Sprintf("/%s/%s/branches/%s", ownerDid, repoName, url.PathEscape(branch))
152
-
req, err := us.newRequest(Method, endpoint, nil, nil)
157
-
return do[types.RepoBranchResponse](us, req)
160
-
func (us *UnsignedClient) DefaultBranch(ownerDid, repoName string) (*types.RepoDefaultBranchResponse, error) {
165
-
endpoint := fmt.Sprintf("/%s/%s/branches/default", ownerDid, repoName)
167
-
req, err := us.newRequest(Method, endpoint, nil, nil)
172
-
resp, err := us.client.Do(req)
176
-
defer resp.Body.Close()
178
-
var defaultBranch types.RepoDefaultBranchResponse
179
-
if err := json.NewDecoder(resp.Body).Decode(&defaultBranch); err != nil {
183
-
return &defaultBranch, nil
186
-
func (us *UnsignedClient) Capabilities() (*types.Capabilities, error) {
189
-
Endpoint = "/capabilities"
192
-
req, err := us.newRequest(Method, Endpoint, nil, nil)
197
-
resp, err := us.client.Do(req)
201
-
defer resp.Body.Close()
203
-
var capabilities types.Capabilities
204
-
if err := json.NewDecoder(resp.Body).Decode(&capabilities); err != nil {
208
-
return &capabilities, nil
211
-
func (us *UnsignedClient) Compare(ownerDid, repoName, rev1, rev2 string) (*types.RepoFormatPatchResponse, error) {
216
-
endpoint := fmt.Sprintf("/%s/%s/compare/%s/%s", ownerDid, repoName, url.PathEscape(rev1), url.PathEscape(rev2))
218
-
req, err := us.newRequest(Method, endpoint, nil, nil)
220
-
return nil, fmt.Errorf("Failed to create request.")
223
-
compareResp, err := us.client.Do(req)
225
-
return nil, fmt.Errorf("Failed to create request.")
227
-
defer compareResp.Body.Close()
229
-
switch compareResp.StatusCode {
232
-
return nil, fmt.Errorf("Branch comparisons not supported on this knot.")
235
-
respBody, err := io.ReadAll(compareResp.Body)
237
-
log.Println("failed to compare across branches")
238
-
return nil, fmt.Errorf("Failed to compare branches.")
240
-
defer compareResp.Body.Close()
242
-
var formatPatchResponse types.RepoFormatPatchResponse
243
-
err = json.Unmarshal(respBody, &formatPatchResponse)
245
-
log.Println("failed to unmarshal format-patch response", err)
246
-
return nil, fmt.Errorf("failed to compare branches.")
249
-
return &formatPatchResponse, nil
252
-
func (s *UnsignedClient) RepoLanguages(ownerDid, repoName, ref string) (*types.RepoLanguageResponse, error) {
256
-
endpoint := fmt.Sprintf("/%s/%s/languages/%s", ownerDid, repoName, url.PathEscape(ref))
258
-
req, err := s.newRequest(Method, endpoint, nil, nil)
263
-
resp, err := s.client.Do(req)
268
-
var result types.RepoLanguageResponse
269
-
if resp.StatusCode != http.StatusOK {
270
-
log.Println("failed to calculate languages", resp.Status)
271
-
return &types.RepoLanguageResponse{}, nil
274
-
body, err := io.ReadAll(resp.Body)
279
-
err = json.Unmarshal(body, &result)
284
-
return &result, nil