GO:Confluence API无法获取所有附件

时间:2017-06-30 08:35:56

标签: api go confluence confluence-rest-api

我正在使用golang作为我的应用程序,在这个应用程序中,我尝试从Confluence获取附件,以下是详细信息

$(".remove-button").click(function(event) {
         $(this).parents('.has-delete').remove();
           return false; 
        });
req:="https://domain.atlassian.net/wiki/rest/api/content?expand=body.view,version&type=page&start=0&limit="
res, err := w.sendRequest(req)
    if err != nil {
        return nil, err
    }
    if strings.EqualFold(contentID, "") == false {
        if len(res.Results) != 0 {
            for i, _ := range res.Results {
                Log.Info("files processed is:", i)
                extension := filepath.Ext(res.Results[i].Title)
                isExtenstionExclude := isExcludedExtenstion(sbConfig, extension)
                ispathExclude := isExcludedFolder(sbConfig, res.Results[i].Links.Webui)
                if sbgoclient.ExtesionMap[extension] == 0 || isExtenstionExclude == true || ispathExclude == true {
                    binarycount++
                    Log.Info("Excluded by extension" + extension + " for file" + res.Results[i].Title)
                } else {
                    md5HashInBytes := md5.Sum([]byte(res.Results[i].Title))
                    md5HashInString := hex.EncodeToString(md5HashInBytes[:])
                    file_path := parameter[0] + "/" + md5HashInString + strings.Replace(res.Results[i].Title, " ", "", -1)
                    file, err := os.Create(file_path)

                    if err != nil {
                        fmt.Println(err)
                        panic(err)
                    }
                    url_1 := sbConfig.ConfluenceUrl + res.Results[i].Links.Download
                    req, err := http.NewRequest("GET", url_1, nil)
                    resp, _ := w.client.Do(req) // add a filter to check redirect

                    if err != nil {
                        fmt.Println(err)
                        panic(err)
                    }
                    // Close body on function exit
                    defer resp.Body.Close()
                    fmt.Println(resp.Status)

                    size, err = io.Copy(file, resp.Body)

                    if err != nil {
                        panic(err)
                    }

                    defer file.Close()
                    fmt.Printf("%s with %v bytes downloaded", res.Results[i].Title, size)
                    meta := map[string]string{
                        "size": strconv.FormatInt(size, 10),
                    }
                }
            }
        }
    } else {

        if len(res.Results) != 0 {
            for i, _ := range res.Results {
                Log.Info("page indexing is", res.Results[i].Title, "and i value is:", i)
                fmt.Println("hmtl content is", res.Results[i].Body.View.Value)
                fmt.Println("page name is:", res.Results[i].Title)
                md5HashInBytes := md5.Sum([]byte(res.Results[i].Title))
                md5HashInString := hex.EncodeToString(md5HashInBytes[:])
                file_path := parameter[0] + "/" + md5HashInString + strings.Replace(res.Results[i].Title, " ", "", -1) + ".html"
                file, err := os.Create(file_path)

                if err != nil {
                    fmt.Println(err)
                    panic(err)
                }
                defer file.Close()
                html_content := "<html><body>" + res.Results[i].Body.View.Value + "</body></html>"
                err = ioutil.WriteFile(file.Name(), []byte(html_content), 0777)
                if err != nil {
                    fmt.Println("error writing into file", err)
                    panic(err)
                }
                file.Close()                    
        }

并且在这个汇合域实际上我有超过1000个文件,但我只能下载80到90左右,我不知道这里发生了什么,请建议做任何修改 以下是用于从响应json

获取值的结构
func (w *Wiki) sendRequest(req *http.Request) (*vijay_content, error) {
    var testjson vijay_content
    req.Header.Add("Accept", "application/json, */*")
    w.authMethod.auth(req)

    resp, err := w.client.Do(req)
    if err != nil {
        return nil, err
    }
    bodyBytes, _ := ioutil.ReadAll(resp.Body)
    body := string(bodyBytes)

    fmt.Printf("response is %s\n", body)
    err = json.Unmarshal(bodyBytes, &testjson)
    if err != nil {
        fmt.Println("error here is", err)
        return nil, err
    }

    switch resp.StatusCode {
    case http.StatusOK, http.StatusCreated, http.StatusPartialContent:

        return &testjson, nil
    case http.StatusNoContent, http.StatusResetContent:
        return nil, nil
    case http.StatusUnauthorized:
        return nil, fmt.Errorf("Authentication failed.")
    case http.StatusServiceUnavailable:
        return nil, fmt.Errorf("Service is not available (%s).", resp.Status)
    case http.StatusInternalServerError:
        return nil, fmt.Errorf("Internal server error: %s", resp.Status)
    }

    return nil, fmt.Errorf("Unknown response status %s", resp.Status)
}

1 个答案:

答案 0 :(得分:1)

API对结果进行分页。您应该通过指定startlimit来获取多个请求中的整个列表。

E.g。请求start=0&limit=30的前30个文档列表,然后使用start=30&limit=30请求下30个文档,依此类推,直到您收到空列表的回复。

您可以在the docs on pagination中阅读更多详情。