Skip to content

Commit 80df8b5

Browse files
committed
增加获取默认资料库的功能
1 parent cb5a208 commit 80df8b5

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

library.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,43 @@ func (cli *Client) GetLibrary(name string) (*Library, error) {
105105
return nil, fmt.Errorf("未找到资料库")
106106
}
107107

108+
//获取默认资料库
109+
func (cli *Client) GetDefaultLibrary() (*Library, error) {
110+
resp,err := cli.doRequest("GET", "/default-repo/", nil, nil)
111+
if err!=nil {
112+
return nil, fmt.Errorf("获取默认资料库失败: %s", err)
113+
}
114+
defer resp.Body.Close()
115+
116+
var respInfo struct{
117+
Exists bool
118+
RepoId string `json:"repo_id"`
119+
}
120+
121+
err = json.NewDecoder(resp.Body).Decode(&respInfo)
122+
if err!=nil {
123+
return nil, fmt.Errorf("获取默认资料库失败: %s", err)
124+
}
125+
126+
if !respInfo.Exists {
127+
return nil, fmt.Errorf("默认资料库不存在")
128+
}
129+
130+
libraries, err := cli.ListAllLibraries()
131+
if err != nil {
132+
return nil, fmt.Errorf("获取资料库列表失败: %s", err)
133+
}
134+
135+
for _, library := range libraries {
136+
if library.Id == respInfo.RepoId {
137+
return library, nil
138+
}
139+
}
140+
141+
return nil, fmt.Errorf("未找到资料库")
142+
143+
}
144+
108145
func (lib *Library) doRequest(method, uri string, header http.Header, body io.Reader) (*http.Response, error) {
109146
if !strings.HasPrefix(uri, "http://") && !strings.HasPrefix(uri, "https://") {
110147
uri = "/repos/" + lib.Id + uri

0 commit comments

Comments
 (0)