diff --git a/api.go b/api.go index 16c44a9..32d6363 100644 --- a/api.go +++ b/api.go @@ -75,6 +75,9 @@ type Timestamp struct { time.Time } +// ContainerID is container ID +type ContainerID string + // EmptyParameters is empty parameters type EmptyParameters struct { // nothing @@ -250,7 +253,7 @@ type Version struct { // Extensions contains info about content extensions type Extensions struct { - Position string `json:"position"` // Page + Position int `json:"position"` // Page MediaType string `json:"mediaType"` // Attachment FileSize int `json:"fileSize"` // Attachment Comment string `json:"comment"` // Attachment @@ -301,11 +304,11 @@ type Publishers struct { // Container contains basic container info type Container struct { - ID string `json:"id"` - Key string `json:"key"` // Space - Name string `json:"name"` // Space - Title string `json:"title"` // Page or blogpost - Links *Links `json:"_links"` + CID ContainerID `json:"id"` + Key string `json:"key"` // Space + Name string `json:"name"` // Space + Title string `json:"title"` // Page or blogpost + Links *Links `json:"_links"` } // LABELS ////////////////////////////////////////////////////////////////////////////// @@ -588,6 +591,11 @@ MAINLOOP: return result } +// ID return container ID +func (c *Container) ID() string { + return string(c.CID) +} + // ////////////////////////////////////////////////////////////////////////////////// // // UnmarshalJSON is custom Date format unmarshaler @@ -599,6 +607,20 @@ func (d *Date) UnmarshalJSON(b []byte) error { return err } +// UnmarshalJSON is custom container ID unmarshaler +func (c *ContainerID) UnmarshalJSON(b []byte) error { + switch { + case len(b) == 0: + // nop + case b[0] == '"': + *c = ContainerID(strings.Replace(string(b), "\"", "", -1)) + default: + *c = ContainerID(string(b)) + } + + return nil +} + // UnmarshalJSON is custom Timestamp format unmarshaler func (d *Timestamp) UnmarshalJSON(b []byte) error { ts, err := strconv.ParseInt(string(b), 10, 64) diff --git a/confluence.go b/confluence.go index 975d9b6..5df23f4 100644 --- a/confluence.go +++ b/confluence.go @@ -830,7 +830,7 @@ func (api *API) acquireRequest(method, uri string, params Parameters) *fasthttp. } // Set auth header - req.Header.Set("Authorization", "Basic "+api.basicAuth) + req.Header.Add("Authorization", "Basic "+api.basicAuth) return req } diff --git a/confluence_test.go b/confluence_test.go index 0269650..16f6416 100644 --- a/confluence_test.go +++ b/confluence_test.go @@ -37,7 +37,7 @@ func (s *ConfluenceSuite) TestParamsEncoding(c *C) { Expand: []string{"test1,test2"}, } - c.Assert(p.ToQuery(), Equals, "expand=test1,test2") + c.Assert(p.ToQuery(), Equals, `expand=test1%2Ctest2`) p = SpaceParameters{ SpaceKey: []string{"TS1", "TS2", "TS3"}, diff --git a/utils.go b/utils.go index cf38fb7..a448696 100644 --- a/utils.go +++ b/utils.go @@ -9,6 +9,7 @@ package confluence import ( "fmt" + "net/url" "reflect" "strings" "time" @@ -30,7 +31,7 @@ func paramsToQuery(params interface{}) string { switch value.Type().String() { case "string": if value.String() != "" { - result += field.Tag.Get("query") + "=" + value.String() + "&" + result += field.Tag.Get("query") + "=" + esc(value.String()) + "&" } case "int": @@ -77,9 +78,9 @@ func formatSlice(tag string, s reflect.Value) string { v := s.Index(i) if unwrap { - result += name + "=" + v.String() + "&" + result += name + "=" + esc(v.String()) + "&" } else { - result += v.String() + "," + result += esc(v.String()) + "," } } @@ -94,3 +95,8 @@ func parseSliceTag(tag string) (string, bool) { return tag[:strings.Index(tag, ",")], true } + +// esc escapes the string so it can be safely placed inside a URL query +func esc(s string) string { + return url.QueryEscape(s) +} diff --git a/version.go b/version.go index 100da7e..9020ae4 100644 --- a/version.go +++ b/version.go @@ -12,7 +12,7 @@ const ( NAME = "Go-Confluence" // VERSION is package version - VERSION = "1.3.0" + VERSION = "1.4.0" ) // ////////////////////////////////////////////////////////////////////////////////// //