| 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 |
"fmt" |
| 23 |
"io" |
| 24 |
"net/http" |
| 25 |
"net/url" |
| 26 |
) |
| 27 |
|
| 28 |
const RetrieveSubjectsQuery = `query($gitoid: String!) { |
| 29 |
subjects( |
| 30 |
where: { |
| 31 |
hasStatementWith:{ |
| 32 |
hasDsseWith:{ |
| 33 |
gitoidSha256: $gitoid |
| 34 |
} |
| 35 |
} |
| 36 |
} |
| 37 |
) { |
| 38 |
edges { |
| 39 |
node{ |
| 40 |
name |
| 41 |
subjectDigests{ |
| 42 |
algorithm |
| 43 |
value |
| 44 |
} |
| 45 |
} |
| 46 |
} |
| 47 |
} |
| 48 |
}` |
| 49 |
|
| 50 |
const SearchQuery = `query($algo: String!, $digest: String!) { |
| 51 |
dsses( |
| 52 |
where: { |
| 53 |
hasStatementWith: { |
| 54 |
hasSubjectsWith: { |
| 55 |
hasSubjectDigestsWith: { |
| 56 |
value: $digest, |
| 57 |
algorithm: $algo |
| 58 |
} |
| 59 |
} |
| 60 |
} |
| 61 |
} |
| 62 |
) { |
| 63 |
edges { |
| 64 |
node { |
| 65 |
gitoidSha256 |
| 66 |
statement { |
| 67 |
attestationCollections { |
| 68 |
name |
| 69 |
attestations { |
| 70 |
type |
| 71 |
} |
| 72 |
} |
| 73 |
} |
| 74 |
} |
| 75 |
} |
| 76 |
} |
| 77 |
}` |
| 78 |
|
| 79 |
func GraphQlQuery[TRes any, TVars any](ctx context.Context, baseUrl, query string, vars TVars, requestOptions ...RequestOption) (TRes, error) { |
| 80 |
var response TRes |
| 81 |
queryUrl, err := url.JoinPath(baseUrl, "query") |
| 82 |
if err != nil { |
| 83 |
return response, err |
| 84 |
} |
| 85 |
|
| 86 |
requestBody := GraphQLRequestBodyGeneric[TVars]{ |
| 87 |
Query: query, |
| 88 |
Variables: vars, |
| 89 |
} |
| 90 |
|
| 91 |
reqBody, err := json.Marshal(requestBody) |
| 92 |
if err != nil { |
| 93 |
return response, err |
| 94 |
} |
| 95 |
|
| 96 |
req, err := http.NewRequestWithContext(ctx, http.MethodPost, queryUrl, bytes.NewReader(reqBody)) |
| 97 |
if err != nil { |
| 98 |
return response, err |
| 99 |
} |
| 100 |
|
| 101 |
req = applyRequestOptions(req, requestOptions...) |
| 102 |
req.Header.Set("Content-Type", "application/json") |
| 103 |
hc := &http.Client{} |
| 104 |
res, err := hc.Do(req) |
| 105 |
if err != nil { |
| 106 |
return response, err |
| 107 |
} |
| 108 |
|
| 109 |
defer res.Body.Close() |
| 110 |
if res.StatusCode != http.StatusOK { |
| 111 |
errMsg, err := io.ReadAll(res.Body) |
| 112 |
if err != nil { |
| 113 |
return response, err |
| 114 |
} |
| 115 |
|
| 116 |
return response, errors.New(string(errMsg)) |
| 117 |
} |
| 118 |
|
| 119 |
dec := json.NewDecoder(res.Body) |
| 120 |
gqlRes := GraphQLResponseGeneric[TRes]{} |
| 121 |
if err := dec.Decode(&gqlRes); err != nil { |
| 122 |
return response, err |
| 123 |
} |
| 124 |
|
| 125 |
if len(gqlRes.Errors) > 0 { |
| 126 |
return response, fmt.Errorf("graph ql query failed: %v", gqlRes.Errors) |
| 127 |
} |
| 128 |
|
| 129 |
return gqlRes.Data, nil |
| 130 |
} |
| 131 |
|
| 132 |
// Deprecated: Use GraphQlQuery with the WithHeaders RequestOption |
| 133 |
func GraphQlQueryWithHeaders[TRes any, TVars any](ctx context.Context, baseUrl, query string, vars TVars, headers map[string]string, requestOptions ...RequestOption) (TRes, error) { |
| 134 |
h := http.Header{} |
| 135 |
for k, v := range headers { |
| 136 |
h.Set(k, v) |
| 137 |
} |
| 138 |
|
| 139 |
requestOptions = append(requestOptions, WithHeaders(h)) |
| 140 |
return GraphQlQuery[TRes](ctx, baseUrl, query, vars, requestOptions...) |
| 141 |
} |