| 1 |
// Copyright 2023-2024 The Archivista Contributors |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of the License at |
| 6 |
// |
| 7 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
// |
| 9 |
// Unless required by applicable law or agreed to in writing, software |
| 10 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
// See the License for the specific language governing permissions and |
| 13 |
// limitations under the License. |
| 14 |
|
| 15 |
package api |
| 16 |
|
| 17 |
import ( |
| 18 |
"bytes" |
| 19 |
"context" |
| 20 |
"encoding/json" |
| 21 |
"errors" |
| 22 |
"io" |
| 23 |
"net/http" |
| 24 |
"net/url" |
| 25 |
|
| 26 |
"github.com/in-toto/go-witness/dsse" |
| 27 |
) |
| 28 |
|
| 29 |
type UploadResponse struct { |
| 30 |
Gitoid string `json:"gitoid"` |
| 31 |
} |
| 32 |
|
| 33 |
// Deprecated: Use UploadResponse instead. It will be removed in version >= v0.6.0 |
| 34 |
type StoreResponse = UploadResponse |
| 35 |
|
| 36 |
// Deprecated: Use Store instead. It will be removed in version >= v0.6.0 |
| 37 |
func Upload(ctx context.Context, baseURL string, envelope dsse.Envelope, requestOptions ...RequestOption) (UploadResponse, error) { |
| 38 |
return Store(ctx, baseURL, envelope, requestOptions...) |
| 39 |
} |
| 40 |
|
| 41 |
func Store(ctx context.Context, baseURL string, envelope dsse.Envelope, requestOptions ...RequestOption) (StoreResponse, error) { |
| 42 |
buf := &bytes.Buffer{} |
| 43 |
enc := json.NewEncoder(buf) |
| 44 |
if err := enc.Encode(envelope); err != nil { |
| 45 |
return StoreResponse{}, err |
| 46 |
} |
| 47 |
|
| 48 |
return StoreWithReader(ctx, baseURL, buf, requestOptions...) |
| 49 |
} |
| 50 |
|
| 51 |
func StoreWithReader(ctx context.Context, baseURL string, r io.Reader, requestOptions ...RequestOption) (StoreResponse, error) { |
| 52 |
return StoreWithReaderWithHTTPClient(ctx, &http.Client{}, baseURL, r, requestOptions...) |
| 53 |
} |
| 54 |
|
| 55 |
func StoreWithReaderWithHTTPClient(ctx context.Context, client *http.Client, baseURL string, r io.Reader, requestOptions ...RequestOption) (StoreResponse, error) { |
| 56 |
uploadPath, err := url.JoinPath(baseURL, "upload") |
| 57 |
if err != nil { |
| 58 |
return UploadResponse{}, err |
| 59 |
} |
| 60 |
|
| 61 |
req, err := http.NewRequestWithContext(ctx, "POST", uploadPath, r) |
| 62 |
if err != nil { |
| 63 |
return UploadResponse{}, err |
| 64 |
} |
| 65 |
|
| 66 |
req = applyRequestOptions(req, requestOptions...) |
| 67 |
req.Header.Set("Content-Type", "application/json") |
| 68 |
hc := &http.Client{} |
| 69 |
resp, err := hc.Do(req) |
| 70 |
if err != nil { |
| 71 |
return UploadResponse{}, err |
| 72 |
} |
| 73 |
|
| 74 |
defer resp.Body.Close() |
| 75 |
bodyBytes, err := io.ReadAll(resp.Body) |
| 76 |
if err != nil { |
| 77 |
return UploadResponse{}, err |
| 78 |
} |
| 79 |
|
| 80 |
if resp.StatusCode != http.StatusOK { |
| 81 |
return UploadResponse{}, errors.New(string(bodyBytes)) |
| 82 |
} |
| 83 |
|
| 84 |
uploadResp := UploadResponse{} |
| 85 |
if err := json.Unmarshal(bodyBytes, &uploadResp); err != nil { |
| 86 |
return UploadResponse{}, err |
| 87 |
} |
| 88 |
|
| 89 |
return uploadResp, nil |
| 90 |
} |