// Set body. // // @param body interface{} // @return void // @panics // @implemented func (this *Request) SetBody(body interface{}) { if body != nil && // these methods not allowed for body this.Method != METHOD_HEAD && this.Method != METHOD_GET { switch body := body.(type) { case string: // @overwrite if this.GetHeader("Content-Type") == "application/json" { // embrace with quotes for valid JSON body body = util.Quote(body) } this.Body = body default: bodyType := _fmt.Sprintf("%T", body) if util.StringSearch(bodyType, "^u?int(\\d+)?|float(32|64)$") { // @overwrite this.Body = util.String(body) } else { if this.GetHeader("Content-Type") == "application/json" { // @overwrite body, err := util.UnparseBody(body) if err != nil { panic(err) } this.Body = body } } } // auto-set content length headers this.SetHeader("Content-Length", len(this.Body.(string))) } }
// Get document. // // @param key string // @return map[string]interface{}, error func (this *Database) GetDocument(key string) (map[string]interface{}, error) { // prepare query query := util.ParamList( "include_docs", true, "key", util.Quote(key), ) data, err := this.Client.Get(this.Name+"/_all_docs", query, nil). GetBodyData(&DatabaseDocumentList{}) if err != nil { return nil, err } ret := util.Map() for _, doc := range data.(*DatabaseDocumentList).Rows { ret["id"] = doc.Id ret["key"] = doc.Key ret["value"] = map[string]string{"rev": doc.Value["rev"].(string)} ret["doc"] = map[string]interface{}{} // fill doc field for key, value := range doc.Doc { ret["doc"].(map[string]interface{})[key] = value } } return ret, nil }
// Ping. // // @param statusCodes... uint16 // @return bool // @panics func (this *DocumentAttachment) Ping(statusCodes ...uint16) bool { if this.Document == nil { panic("Attachment document is not defined!") } docId := this.Document.GetId() docRev := this.Document.GetRev() if docId == "" { panic("Attachment document _id is required!") } if this.FileName == "" { panic("Attachment file name is required!") } query, headers := util.Map(), util.Map() if docRev != "" { query["rev"] = docRev } if this.Digest != "" { headers["If-None-Match"] = util.Quote(this.Digest) } database := this.Document.GetDatabase() response := database.Client.Head(util.StringFormat("%s/%s/%s", database.Name, docId, util.UrlEncode(this.FileName)), query, headers) // try to match given status codes for _, statusCode := range statusCodes { if response.GetStatusCode() == statusCode { return true } } return false }
// Check is not modified. // // @return bool // @panics func (this *Document) IsNotModified() bool { id, rev := this.GetId(), this.GetRev() if id == "" || rev == "" { panic("_id & _rev fields are could not be empty!") } headers := util.Map() headers["If-None-Match"] = util.Quote(rev) return (304 == this.Database.Client. Head(this.Database.Name+"/"+util.UrlEncode(id), nil, headers).GetStatusCode()) }
// Ping. // // @param statusCode uint16 // @return bool // @panics func (this *Document) Ping(statusCode uint16) bool { id := this.GetId() if id == "" { panic("_id field is could not be empty!") } headers := util.Map() if this.Rev != "" { headers["If-None-Match"] = util.Quote(this.Rev) } return (statusCode == this.Database.Client. Head(this.Database.Name+"/"+util.UrlEncode(id), nil, headers).GetStatusCode()) }
// Find. // // @return map[string]interface{} // @panics func (this *DocumentAttachment) Find() map[string]interface{} { if this.Document == nil { panic("Attachment document is not defined!") } docId := this.Document.GetId() docRev := this.Document.GetRev() if docId == "" { panic("Attachment document _id is required!") } if this.FileName == "" { panic("Attachment file name is required!") } query, headers := util.Map(), util.Map() if docRev != "" { query["rev"] = docRev } if this.Digest != "" { headers["If-None-Match"] = util.Quote(this.Digest) } headers["Accept"] = "*/*" headers["Content-Type"] = nil // nil=remove database := this.Document.GetDatabase() response := database.Client.Get(util.StringFormat("%s/%s/%s", database.Name, docId, util.UrlEncode(this.FileName)), query, headers) statusCode := response.GetStatusCode() ret := util.Map() // try to match excepted status code if statusCode == 200 || statusCode == 304 { ret["content"] = response.GetBody() ret["content_type"] = response.GetHeader("Content-Type") ret["content_length"] = util.UInt(response.GetHeader("Content-Length")) // set digest md5 := response.GetHeader("Content-MD5") if md5 == nil { md5 = response.GetHeader("ETag") } ret["digest"] = "md5-" + util.Trim(md5.(string), "\"") } return ret }