forked from ravendb/ravendb-go-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhead_document_command.go
80 lines (63 loc) · 1.9 KB
/
head_document_command.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package ravendb
import (
"net/http"
)
var (
_ RavenCommand = &HeadDocumentCommand{}
)
// HeadDocumentCommand describes "head document" command
type HeadDocumentCommand struct {
RavenCommandBase
id string
changeVector *string
Result *string // change vector
}
// NewHeadDocumentCommand returns new HeadDocumentCommand
func NewHeadDocumentCommand(id string, changeVector *string) *HeadDocumentCommand {
panicIf(id == "", "id cannot be empty")
cmd := &HeadDocumentCommand{
RavenCommandBase: NewRavenCommandBase(),
id: id,
changeVector: changeVector,
}
return cmd
}
func (c *HeadDocumentCommand) CreateRequest(node *ServerNode) (*http.Request, error) {
url := node.URL + "/databases/" + node.Database + "/docs?id=" + urlUtilsEscapeDataString(c.id)
request, err := newHttpHead(url)
if err != nil {
return nil, err
}
if c.changeVector != nil {
request.Header.Set(headersIfNoneMatch, *c.changeVector)
}
return request, nil
}
// ProcessResponse processes HTTP response
func (c *HeadDocumentCommand) ProcessResponse(cache *httpCache, response *http.Response, url string) (responseDisposeHandling, error) {
statusCode := response.StatusCode
if statusCode == http.StatusNotModified {
c.Result = c.changeVector
return responseDisposeHandlingAutomatic, nil
}
if statusCode == http.StatusNotFound {
c.Result = nil
return responseDisposeHandlingAutomatic, nil
}
var err error
c.Result, err = gttpExtensionsGetRequiredEtagHeader(response)
return responseDisposeHandlingAutomatic, err
}
func (c *HeadDocumentCommand) SetResponse(response []byte, fromCache bool) error {
if len(response) != 0 {
return throwInvalidResponse()
}
// This is called from handleUnsuccessfulResponse() to mark the command
// as having empty result
c.Result = nil
return nil
}
// Exists returns true if the command has a result
func (c *HeadDocumentCommand) Exists() bool {
return c.Result != nil
}