-
Notifications
You must be signed in to change notification settings - Fork 1
/
methods_tree.go
130 lines (114 loc) · 4.1 KB
/
methods_tree.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
package sourcify
import (
"encoding/json"
"fmt"
"io"
"net/http"
"strings"
"github.com/ethereum/go-ethereum/common"
)
var (
// MethodGetFileTreeFullOrPartialMatch represents the API endpoint for getting the file tree with full or partial match in the Sourcify service.
// It includes the name, the HTTP method, the URI, and the parameters necessary for the request.
// Returns repository URLs for every file in the source tree for the desired chain and address. Searches for full and partial matches.
// More information: https://docs.sourcify.dev/docs/api/server/get-file-tree-all/
MethodGetFileTreeFullOrPartialMatch = Method{
Name: "Get File Tree Full or Partial Match",
URI: "/files/tree/any/:chain/:address",
MoreInfo: "https://docs.sourcify.dev/docs/api/server/get-file-tree-all/",
Method: "GET",
ParamType: MethodParamTypeUri,
RequiredParams: []string{":chain", ":address"},
Params: []MethodParam{
{
Key: ":chain",
Value: "",
},
{
Key: ":address",
Value: "",
},
},
}
// MethodGetFileTreeFullMatch represents the API endpoint for getting the file tree with full match in the Sourcify service.
// It includes the name, the HTTP method, the URI, and the parameters necessary for the request.
// Returns repository URLs for every file in the source tree for the desired chain and address. Searches only for full matches.
// More information: https://docs.sourcify.dev/docs/api/server/get-file-tree-full/
MethodGetFileTreeFullMatch = Method{
Name: "Get File Tree Full Match",
URI: "/files/tree/:chain/:address",
MoreInfo: "https://docs.sourcify.dev/docs/api/server/get-file-tree-full/",
Method: "GET",
ParamType: MethodParamTypeUri,
RequiredParams: []string{":chain", ":address"},
Params: []MethodParam{
{
Key: ":chain",
Value: "",
},
{
Key: ":address",
Value: "",
},
},
}
)
// FileTree represents the file tree response from the Sourcify service.
type FileTree struct {
Status string `json:"status"`
Files []string `json:"files"`
}
// GetContractFiles retrieves the repository URLs for every file in the source tree for the given chain ID and contract address.
// The matchType parameter determines whether to search for full matches, partial matches, or any matches.
// It returns the FileTree object containing the status and file URLs, or an error if any.
func GetContractFiles(client *Client, chainId int, contract common.Address, matchType MethodMatchType) (*FileTree, error) {
var method Method
switch matchType {
case MethodMatchTypeFull:
method = MethodGetFileTreeFullMatch
case MethodMatchTypePartial:
method = MethodGetFileTreeFullOrPartialMatch
case MethodMatchTypeAny:
method = MethodGetFileTreeFullOrPartialMatch
default:
return nil, fmt.Errorf("invalid match type: %s", matchType)
}
method.SetParams(
MethodParam{Key: ":chain", Value: chainId},
MethodParam{Key: ":address", Value: contract.Hex()},
)
if err := method.Verify(); err != nil {
return nil, err
}
response, statusCode, err := client.CallMethod(method)
if err != nil {
return nil, err
}
// Close the io.ReadCloser interface.
// This is important as CallMethod is NOT closing the response body!
// You'll have memory leaks if you don't do this!
defer response.Close()
body, readBodyErr := io.ReadAll(response)
if readBodyErr != nil {
return nil, fmt.Errorf("failure to read body: %s", readBodyErr)
}
if statusCode != http.StatusOK {
return nil, fmt.Errorf("unexpected status code: %d", statusCode)
}
toReturn := &FileTree{}
if err := json.Unmarshal(body, &toReturn); err != nil {
// Sometimes, response will not be a JSON object, but an array.
// In this case, we'll get an error, but we can still return the code.
// This is a workaround for the Sourcify API.
// Ugly, but it works.
if strings.Contains(err.Error(), "cannot unmarshal array into Go value") {
toReturn.Status = "unknown"
if err := json.Unmarshal(body, &toReturn.Files); err != nil {
return nil, err
}
return toReturn, nil
}
return nil, err
}
return toReturn, nil
}