tangled
alpha
login
or
join now
willdot.net
/
tangled-fork
forked from
tangled.org/core
0
fork
atom
Monorepo for Tangled
0
fork
atom
overview
issues
pulls
pipelines
knotclient: move unsigned endpoints into knotclient/unsigned
oppi.li
9 months ago
15da3874
0c02e343
+243
-231
2 changed files
expand all
collapse all
unified
split
knotclient
signer.go
unsigned.go
-231
knotclient/signer.go
···
7
7
"encoding/hex"
8
8
"encoding/json"
9
9
"fmt"
10
10
-
"io"
11
11
-
"log"
12
10
"net/http"
13
11
"net/url"
14
14
-
"strconv"
15
12
"time"
16
13
17
14
"tangled.sh/tangled.sh/core/types"
···
335
332
336
333
return s.client.Do(req)
337
334
}
338
338
-
339
339
-
type UnsignedClient struct {
340
340
-
Url *url.URL
341
341
-
client *http.Client
342
342
-
}
343
343
-
344
344
-
func NewUnsignedClient(domain string, dev bool) (*UnsignedClient, error) {
345
345
-
client := &http.Client{
346
346
-
Timeout: 5 * time.Second,
347
347
-
}
348
348
-
349
349
-
scheme := "https"
350
350
-
if dev {
351
351
-
scheme = "http"
352
352
-
}
353
353
-
url, err := url.Parse(fmt.Sprintf("%s://%s", scheme, domain))
354
354
-
if err != nil {
355
355
-
return nil, err
356
356
-
}
357
357
-
358
358
-
unsignedClient := &UnsignedClient{
359
359
-
client: client,
360
360
-
Url: url,
361
361
-
}
362
362
-
363
363
-
return unsignedClient, nil
364
364
-
}
365
365
-
366
366
-
func (us *UnsignedClient) newRequest(method, endpoint string, query url.Values, body []byte) (*http.Request, error) {
367
367
-
reqUrl := us.Url.JoinPath(endpoint)
368
368
-
369
369
-
// add query parameters
370
370
-
if query != nil {
371
371
-
reqUrl.RawQuery = query.Encode()
372
372
-
}
373
373
-
374
374
-
return http.NewRequest(method, reqUrl.String(), bytes.NewReader(body))
375
375
-
}
376
376
-
377
377
-
func (us *UnsignedClient) Index(ownerDid, repoName, ref string) (*http.Response, error) {
378
378
-
const (
379
379
-
Method = "GET"
380
380
-
)
381
381
-
382
382
-
endpoint := fmt.Sprintf("/%s/%s/tree/%s", ownerDid, repoName, ref)
383
383
-
if ref == "" {
384
384
-
endpoint = fmt.Sprintf("/%s/%s", ownerDid, repoName)
385
385
-
}
386
386
-
387
387
-
req, err := us.newRequest(Method, endpoint, nil, nil)
388
388
-
if err != nil {
389
389
-
return nil, err
390
390
-
}
391
391
-
392
392
-
return us.client.Do(req)
393
393
-
}
394
394
-
395
395
-
func (us *UnsignedClient) Log(ownerDid, repoName, ref string, page int) (*http.Response, error) {
396
396
-
const (
397
397
-
Method = "GET"
398
398
-
)
399
399
-
400
400
-
endpoint := fmt.Sprintf("/%s/%s/log/%s", ownerDid, repoName, url.PathEscape(ref))
401
401
-
402
402
-
query := url.Values{}
403
403
-
query.Add("page", strconv.Itoa(page))
404
404
-
query.Add("per_page", strconv.Itoa(60))
405
405
-
406
406
-
req, err := us.newRequest(Method, endpoint, query, nil)
407
407
-
if err != nil {
408
408
-
return nil, err
409
409
-
}
410
410
-
411
411
-
return us.client.Do(req)
412
412
-
}
413
413
-
414
414
-
func (us *UnsignedClient) Branches(ownerDid, repoName string) (*http.Response, error) {
415
415
-
const (
416
416
-
Method = "GET"
417
417
-
)
418
418
-
419
419
-
endpoint := fmt.Sprintf("/%s/%s/branches", ownerDid, repoName)
420
420
-
421
421
-
req, err := us.newRequest(Method, endpoint, nil, nil)
422
422
-
if err != nil {
423
423
-
return nil, err
424
424
-
}
425
425
-
426
426
-
return us.client.Do(req)
427
427
-
}
428
428
-
429
429
-
func (us *UnsignedClient) Tags(ownerDid, repoName string) (*types.RepoTagsResponse, error) {
430
430
-
const (
431
431
-
Method = "GET"
432
432
-
)
433
433
-
434
434
-
endpoint := fmt.Sprintf("/%s/%s/tags", ownerDid, repoName)
435
435
-
436
436
-
req, err := us.newRequest(Method, endpoint, nil, nil)
437
437
-
if err != nil {
438
438
-
return nil, err
439
439
-
}
440
440
-
441
441
-
resp, err := us.client.Do(req)
442
442
-
if err != nil {
443
443
-
return nil, err
444
444
-
}
445
445
-
446
446
-
body, err := io.ReadAll(resp.Body)
447
447
-
if err != nil {
448
448
-
return nil, err
449
449
-
}
450
450
-
451
451
-
var result types.RepoTagsResponse
452
452
-
err = json.Unmarshal(body, &result)
453
453
-
if err != nil {
454
454
-
return nil, err
455
455
-
}
456
456
-
457
457
-
return &result, nil
458
458
-
}
459
459
-
460
460
-
func (us *UnsignedClient) Branch(ownerDid, repoName, branch string) (*http.Response, error) {
461
461
-
const (
462
462
-
Method = "GET"
463
463
-
)
464
464
-
465
465
-
endpoint := fmt.Sprintf("/%s/%s/branches/%s", ownerDid, repoName, url.PathEscape(branch))
466
466
-
467
467
-
req, err := us.newRequest(Method, endpoint, nil, nil)
468
468
-
if err != nil {
469
469
-
return nil, err
470
470
-
}
471
471
-
472
472
-
return us.client.Do(req)
473
473
-
}
474
474
-
475
475
-
func (us *UnsignedClient) DefaultBranch(ownerDid, repoName string) (*types.RepoDefaultBranchResponse, error) {
476
476
-
const (
477
477
-
Method = "GET"
478
478
-
)
479
479
-
480
480
-
endpoint := fmt.Sprintf("/%s/%s/branches/default", ownerDid, repoName)
481
481
-
482
482
-
req, err := us.newRequest(Method, endpoint, nil, nil)
483
483
-
if err != nil {
484
484
-
return nil, err
485
485
-
}
486
486
-
487
487
-
resp, err := us.client.Do(req)
488
488
-
if err != nil {
489
489
-
return nil, err
490
490
-
}
491
491
-
defer resp.Body.Close()
492
492
-
493
493
-
var defaultBranch types.RepoDefaultBranchResponse
494
494
-
if err := json.NewDecoder(resp.Body).Decode(&defaultBranch); err != nil {
495
495
-
return nil, err
496
496
-
}
497
497
-
498
498
-
return &defaultBranch, nil
499
499
-
}
500
500
-
501
501
-
func (us *UnsignedClient) Capabilities() (*types.Capabilities, error) {
502
502
-
const (
503
503
-
Method = "GET"
504
504
-
Endpoint = "/capabilities"
505
505
-
)
506
506
-
507
507
-
req, err := us.newRequest(Method, Endpoint, nil, nil)
508
508
-
if err != nil {
509
509
-
return nil, err
510
510
-
}
511
511
-
512
512
-
resp, err := us.client.Do(req)
513
513
-
if err != nil {
514
514
-
return nil, err
515
515
-
}
516
516
-
defer resp.Body.Close()
517
517
-
518
518
-
var capabilities types.Capabilities
519
519
-
if err := json.NewDecoder(resp.Body).Decode(&capabilities); err != nil {
520
520
-
return nil, err
521
521
-
}
522
522
-
523
523
-
return &capabilities, nil
524
524
-
}
525
525
-
526
526
-
func (us *UnsignedClient) Compare(ownerDid, repoName, rev1, rev2 string) (*types.RepoFormatPatchResponse, error) {
527
527
-
const (
528
528
-
Method = "GET"
529
529
-
)
530
530
-
531
531
-
endpoint := fmt.Sprintf("/%s/%s/compare/%s/%s", ownerDid, repoName, url.PathEscape(rev1), url.PathEscape(rev2))
532
532
-
533
533
-
req, err := us.newRequest(Method, endpoint, nil, nil)
534
534
-
if err != nil {
535
535
-
return nil, fmt.Errorf("Failed to create request.")
536
536
-
}
537
537
-
538
538
-
compareResp, err := us.client.Do(req)
539
539
-
if err != nil {
540
540
-
return nil, fmt.Errorf("Failed to create request.")
541
541
-
}
542
542
-
defer compareResp.Body.Close()
543
543
-
544
544
-
switch compareResp.StatusCode {
545
545
-
case 404:
546
546
-
case 400:
547
547
-
return nil, fmt.Errorf("Branch comparisons not supported on this knot.")
548
548
-
}
549
549
-
550
550
-
respBody, err := io.ReadAll(compareResp.Body)
551
551
-
if err != nil {
552
552
-
log.Println("failed to compare across branches")
553
553
-
return nil, fmt.Errorf("Failed to compare branches.")
554
554
-
}
555
555
-
defer compareResp.Body.Close()
556
556
-
557
557
-
var formatPatchResponse types.RepoFormatPatchResponse
558
558
-
err = json.Unmarshal(respBody, &formatPatchResponse)
559
559
-
if err != nil {
560
560
-
log.Println("failed to unmarshal format-patch response", err)
561
561
-
return nil, fmt.Errorf("failed to compare branches.")
562
562
-
}
563
563
-
564
564
-
return &formatPatchResponse, nil
565
565
-
}
+243
knotclient/unsigned.go
···
1
1
+
package knotclient
2
2
+
3
3
+
import (
4
4
+
"bytes"
5
5
+
"encoding/json"
6
6
+
"fmt"
7
7
+
"io"
8
8
+
"log"
9
9
+
"net/http"
10
10
+
"net/url"
11
11
+
"strconv"
12
12
+
"time"
13
13
+
14
14
+
"tangled.sh/tangled.sh/core/types"
15
15
+
)
16
16
+
17
17
+
type UnsignedClient struct {
18
18
+
Url *url.URL
19
19
+
client *http.Client
20
20
+
}
21
21
+
22
22
+
func NewUnsignedClient(domain string, dev bool) (*UnsignedClient, error) {
23
23
+
client := &http.Client{
24
24
+
Timeout: 5 * time.Second,
25
25
+
}
26
26
+
27
27
+
scheme := "https"
28
28
+
if dev {
29
29
+
scheme = "http"
30
30
+
}
31
31
+
url, err := url.Parse(fmt.Sprintf("%s://%s", scheme, domain))
32
32
+
if err != nil {
33
33
+
return nil, err
34
34
+
}
35
35
+
36
36
+
unsignedClient := &UnsignedClient{
37
37
+
client: client,
38
38
+
Url: url,
39
39
+
}
40
40
+
41
41
+
return unsignedClient, nil
42
42
+
}
43
43
+
44
44
+
func (us *UnsignedClient) newRequest(method, endpoint string, query url.Values, body []byte) (*http.Request, error) {
45
45
+
reqUrl := us.Url.JoinPath(endpoint)
46
46
+
47
47
+
// add query parameters
48
48
+
if query != nil {
49
49
+
reqUrl.RawQuery = query.Encode()
50
50
+
}
51
51
+
52
52
+
return http.NewRequest(method, reqUrl.String(), bytes.NewReader(body))
53
53
+
}
54
54
+
55
55
+
func (us *UnsignedClient) Index(ownerDid, repoName, ref string) (*http.Response, error) {
56
56
+
const (
57
57
+
Method = "GET"
58
58
+
)
59
59
+
60
60
+
endpoint := fmt.Sprintf("/%s/%s/tree/%s", ownerDid, repoName, ref)
61
61
+
if ref == "" {
62
62
+
endpoint = fmt.Sprintf("/%s/%s", ownerDid, repoName)
63
63
+
}
64
64
+
65
65
+
req, err := us.newRequest(Method, endpoint, nil, nil)
66
66
+
if err != nil {
67
67
+
return nil, err
68
68
+
}
69
69
+
70
70
+
return us.client.Do(req)
71
71
+
}
72
72
+
73
73
+
func (us *UnsignedClient) Log(ownerDid, repoName, ref string, page int) (*http.Response, error) {
74
74
+
const (
75
75
+
Method = "GET"
76
76
+
)
77
77
+
78
78
+
endpoint := fmt.Sprintf("/%s/%s/log/%s", ownerDid, repoName, url.PathEscape(ref))
79
79
+
80
80
+
query := url.Values{}
81
81
+
query.Add("page", strconv.Itoa(page))
82
82
+
query.Add("per_page", strconv.Itoa(60))
83
83
+
84
84
+
req, err := us.newRequest(Method, endpoint, query, nil)
85
85
+
if err != nil {
86
86
+
return nil, err
87
87
+
}
88
88
+
89
89
+
return us.client.Do(req)
90
90
+
}
91
91
+
92
92
+
func (us *UnsignedClient) Branches(ownerDid, repoName string) (*http.Response, error) {
93
93
+
const (
94
94
+
Method = "GET"
95
95
+
)
96
96
+
97
97
+
endpoint := fmt.Sprintf("/%s/%s/branches", ownerDid, repoName)
98
98
+
99
99
+
req, err := us.newRequest(Method, endpoint, nil, nil)
100
100
+
if err != nil {
101
101
+
return nil, err
102
102
+
}
103
103
+
104
104
+
return us.client.Do(req)
105
105
+
}
106
106
+
107
107
+
func (us *UnsignedClient) Tags(ownerDid, repoName string) (*types.RepoTagsResponse, error) {
108
108
+
const (
109
109
+
Method = "GET"
110
110
+
)
111
111
+
112
112
+
endpoint := fmt.Sprintf("/%s/%s/tags", ownerDid, repoName)
113
113
+
114
114
+
req, err := us.newRequest(Method, endpoint, nil, nil)
115
115
+
if err != nil {
116
116
+
return nil, err
117
117
+
}
118
118
+
119
119
+
resp, err := us.client.Do(req)
120
120
+
if err != nil {
121
121
+
return nil, err
122
122
+
}
123
123
+
124
124
+
body, err := io.ReadAll(resp.Body)
125
125
+
if err != nil {
126
126
+
return nil, err
127
127
+
}
128
128
+
129
129
+
var result types.RepoTagsResponse
130
130
+
err = json.Unmarshal(body, &result)
131
131
+
if err != nil {
132
132
+
return nil, err
133
133
+
}
134
134
+
135
135
+
return &result, nil
136
136
+
}
137
137
+
138
138
+
func (us *UnsignedClient) Branch(ownerDid, repoName, branch string) (*http.Response, error) {
139
139
+
const (
140
140
+
Method = "GET"
141
141
+
)
142
142
+
143
143
+
endpoint := fmt.Sprintf("/%s/%s/branches/%s", ownerDid, repoName, url.PathEscape(branch))
144
144
+
145
145
+
req, err := us.newRequest(Method, endpoint, nil, nil)
146
146
+
if err != nil {
147
147
+
return nil, err
148
148
+
}
149
149
+
150
150
+
return us.client.Do(req)
151
151
+
}
152
152
+
153
153
+
func (us *UnsignedClient) DefaultBranch(ownerDid, repoName string) (*types.RepoDefaultBranchResponse, error) {
154
154
+
const (
155
155
+
Method = "GET"
156
156
+
)
157
157
+
158
158
+
endpoint := fmt.Sprintf("/%s/%s/branches/default", ownerDid, repoName)
159
159
+
160
160
+
req, err := us.newRequest(Method, endpoint, nil, nil)
161
161
+
if err != nil {
162
162
+
return nil, err
163
163
+
}
164
164
+
165
165
+
resp, err := us.client.Do(req)
166
166
+
if err != nil {
167
167
+
return nil, err
168
168
+
}
169
169
+
defer resp.Body.Close()
170
170
+
171
171
+
var defaultBranch types.RepoDefaultBranchResponse
172
172
+
if err := json.NewDecoder(resp.Body).Decode(&defaultBranch); err != nil {
173
173
+
return nil, err
174
174
+
}
175
175
+
176
176
+
return &defaultBranch, nil
177
177
+
}
178
178
+
179
179
+
func (us *UnsignedClient) Capabilities() (*types.Capabilities, error) {
180
180
+
const (
181
181
+
Method = "GET"
182
182
+
Endpoint = "/capabilities"
183
183
+
)
184
184
+
185
185
+
req, err := us.newRequest(Method, Endpoint, nil, nil)
186
186
+
if err != nil {
187
187
+
return nil, err
188
188
+
}
189
189
+
190
190
+
resp, err := us.client.Do(req)
191
191
+
if err != nil {
192
192
+
return nil, err
193
193
+
}
194
194
+
defer resp.Body.Close()
195
195
+
196
196
+
var capabilities types.Capabilities
197
197
+
if err := json.NewDecoder(resp.Body).Decode(&capabilities); err != nil {
198
198
+
return nil, err
199
199
+
}
200
200
+
201
201
+
return &capabilities, nil
202
202
+
}
203
203
+
204
204
+
func (us *UnsignedClient) Compare(ownerDid, repoName, rev1, rev2 string) (*types.RepoFormatPatchResponse, error) {
205
205
+
const (
206
206
+
Method = "GET"
207
207
+
)
208
208
+
209
209
+
endpoint := fmt.Sprintf("/%s/%s/compare/%s/%s", ownerDid, repoName, url.PathEscape(rev1), url.PathEscape(rev2))
210
210
+
211
211
+
req, err := us.newRequest(Method, endpoint, nil, nil)
212
212
+
if err != nil {
213
213
+
return nil, fmt.Errorf("Failed to create request.")
214
214
+
}
215
215
+
216
216
+
compareResp, err := us.client.Do(req)
217
217
+
if err != nil {
218
218
+
return nil, fmt.Errorf("Failed to create request.")
219
219
+
}
220
220
+
defer compareResp.Body.Close()
221
221
+
222
222
+
switch compareResp.StatusCode {
223
223
+
case 404:
224
224
+
case 400:
225
225
+
return nil, fmt.Errorf("Branch comparisons not supported on this knot.")
226
226
+
}
227
227
+
228
228
+
respBody, err := io.ReadAll(compareResp.Body)
229
229
+
if err != nil {
230
230
+
log.Println("failed to compare across branches")
231
231
+
return nil, fmt.Errorf("Failed to compare branches.")
232
232
+
}
233
233
+
defer compareResp.Body.Close()
234
234
+
235
235
+
var formatPatchResponse types.RepoFormatPatchResponse
236
236
+
err = json.Unmarshal(respBody, &formatPatchResponse)
237
237
+
if err != nil {
238
238
+
log.Println("failed to unmarshal format-patch response", err)
239
239
+
return nil, fmt.Errorf("failed to compare branches.")
240
240
+
}
241
241
+
242
242
+
return &formatPatchResponse, nil
243
243
+
}