Skip to content

Commit 0023150

Browse files
committed
Merge branch 'dev_doc' into 'master' (merge request !82)
dev_doc
2 parents a1560e5 + 5c51c3a commit 0023150

File tree

3 files changed

+131
-0
lines changed

3 files changed

+131
-0
lines changed

ci_doc.go

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
package cos
22

33
import (
4+
"bytes"
45
"context"
56
"encoding/json"
67
"encoding/xml"
8+
"io"
79
"net/http"
810
"net/url"
911
)
@@ -293,6 +295,53 @@ func (s *CIService) DocPreview(ctx context.Context, name string, opt *DocPreview
293295
return resp, err
294296
}
295297

298+
type CIDocCompareOptions struct {
299+
Object string `url:"object,omitempty"`
300+
ComparePath string `url:"comparePath,omitempty"`
301+
CompareUrl string `url:"compareUrl,omitempty"`
302+
SrcType string `url:"srcType,omitempty"`
303+
TgtUri string `url:"tgtUri,omitempty"`
304+
}
305+
306+
type CIDocCompareResult struct {
307+
XMLName xml.Name `xml:"Response"`
308+
Code string `xml:"Code,omitempty" json:"code,omitempty"`
309+
ETag string `xml:"ETag,omitempty" json:"eTag,omitempty"`
310+
Msg string `xml:"Msg,omitempty" json:"msg,omitempty"`
311+
ResultPath string `xml:"ResultPath,omitempty" json:"resultPath,omitempty"`
312+
}
313+
314+
// 优先 json
315+
func (w *CIDocCompareResult) Write(p []byte) (n int, err error) {
316+
err = json.Unmarshal(p, w)
317+
if err != nil {
318+
err = xml.NewDecoder(bytes.NewReader(p)).Decode(w)
319+
if err == nil {
320+
return len(p), nil
321+
}
322+
if err == io.EOF {
323+
err = nil // ignore EOF errors caused by empty response body
324+
}
325+
return 0, err
326+
}
327+
return len(p), nil
328+
}
329+
330+
// DocCompare TODO
331+
func (s *CIService) CIDocCompare(ctx context.Context, opt *CIDocCompareOptions) (*Response, *CIDocCompareResult, error) {
332+
var res CIDocCompareResult
333+
sendOpt := sendOptions{
334+
baseURL: s.client.BaseURL.BucketURL,
335+
uri: "/doccompare",
336+
optQuery: opt,
337+
method: http.MethodGet,
338+
disableCloseBody: true,
339+
result: &res,
340+
}
341+
resp, err := s.client.send(ctx, &sendOpt)
342+
return resp, &res, err
343+
}
344+
296345
type DocPreviewHTMLOptions struct {
297346
DstType string `url:"dstType,omitempty"`
298347
SrcType string `url:"srcType,omitempty"`

ci_doc_test.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,30 @@ func TestCIService_DocPreview(t *testing.T) {
202202
}
203203
}
204204

205+
func TestCIService_CIDocCompare(t *testing.T) {
206+
setup()
207+
defer teardown()
208+
209+
mux.HandleFunc("/doccompare", func(w http.ResponseWriter, r *http.Request) {
210+
testMethod(t, r, http.MethodGet)
211+
v := values{
212+
"object": "doc/1.docx",
213+
"comparePath": "doc/2.docx",
214+
}
215+
testFormValues(t, r, v)
216+
})
217+
218+
opt := &CIDocCompareOptions{
219+
Object: "doc/1.docx",
220+
ComparePath: "doc/2.docx",
221+
}
222+
223+
_, _, err := client.CI.CIDocCompare(context.Background(), opt)
224+
if err != nil {
225+
t.Fatalf("CI.DocPreview returned error: %v", err)
226+
}
227+
}
228+
205229
func TestCIService_DocPreviewHTML(t *testing.T) {
206230
setup()
207231
defer teardown()
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package main
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"net/http"
7+
"net/url"
8+
"os"
9+
10+
"github.com/tencentyun/cos-go-sdk-v5"
11+
"github.com/tencentyun/cos-go-sdk-v5/debug"
12+
)
13+
14+
func log_status(err error) {
15+
if err == nil {
16+
return
17+
}
18+
if cos.IsNotFoundError(err) {
19+
// WARN
20+
fmt.Println("WARN: Resource is not existed")
21+
} else if e, ok := cos.IsCOSError(err); ok {
22+
fmt.Printf("ERROR: Code: %v\n", e.Code)
23+
fmt.Printf("ERROR: Message: %v\n", e.Message)
24+
fmt.Printf("ERROR: Resource: %v\n", e.Resource)
25+
fmt.Printf("ERROR: RequestId: %v\n", e.RequestID)
26+
// ERROR
27+
} else {
28+
fmt.Printf("ERROR: %v\n", err)
29+
// ERROR
30+
}
31+
}
32+
33+
func main() {
34+
u, _ := url.Parse("https://test-1234567890.cos.ap-chongqing.myqcloud.com")
35+
cu, _ := url.Parse("https://test-1234567890.ci.ap-chongqing.myqcloud.com")
36+
b := &cos.BaseURL{BucketURL: u, CIURL: cu}
37+
c := cos.NewClient(b, &http.Client{
38+
Transport: &cos.AuthorizationTransport{
39+
SecretID: os.Getenv("COS_SECRETID"),
40+
SecretKey: os.Getenv("COS_SECRETKEY"),
41+
Transport: &debug.DebugRequestTransport{
42+
RequestHeader: true,
43+
// Notice when put a large file and set need the request body, might happend out of memory error.
44+
RequestBody: true,
45+
ResponseHeader: true,
46+
ResponseBody: false,
47+
},
48+
},
49+
})
50+
51+
opt := &cos.CIDocCompareOptions{
52+
Object: "doc/1.docx",
53+
ComparePath: "doc/2.docx",
54+
}
55+
_, res, err := c.CI.CIDocCompare(context.Background(), opt)
56+
fmt.Printf("%+v\n", res)
57+
log_status(err)
58+
}

0 commit comments

Comments
 (0)