knotclient: move unsigned endpoints into knotclient/unsigned

authored by oppi.li and committed by Tangled 62713287 a8daf9c5

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