-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRepositories.go
69 lines (57 loc) · 1.38 KB
/
Repositories.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
package aspace
import (
"encoding/json"
"fmt"
"io"
"strconv"
)
// Return a slice of Repository IDs for the instance
func (a *ASClient) GetRepositories() ([]int, error) {
repIds := []int{}
endpoint := "/repositories"
response, err := a.get(endpoint, false)
if err != nil {
return repIds, err
}
body, err := io.ReadAll(response.Body)
if err != nil {
return repIds, err
}
reps := make([]map[string]interface{}, 1, 1)
err = json.Unmarshal(body, &reps)
if err != nil {
return repIds, err
}
for i := range reps {
rep := fmt.Sprintf("%v", reps[i]["uri"])
repId, err := strconv.Atoi(rep[len(rep)-1:])
if err != nil {
return repIds, err
}
repIds = append(repIds, repId)
}
return repIds, nil
}
// Return a Repository struct for given Repository ID
func (a *ASClient) GetRepository(repositoryID int) (Repository, error) {
repository := Repository{}
endpoint := fmt.Sprintf("/repositories/%d", repositoryID)
response, err := a.get(endpoint, true)
if err != nil {
return repository, err
}
body, err := io.ReadAll(response.Body)
if err != nil {
return repository, err
}
err = json.Unmarshal(body, &repository)
if err != nil {
return repository, err
}
return repository, nil
}
// Get a random Repository ID
func (a *ASClient) GetRandomRepository() (int, error) {
repositoryIDs := []int{2, 3, 6}
return repositoryIDs[rGen.Intn(len(repositoryIDs))], nil
}