Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 208 lines (175 sloc) 4.333 kb
bba4763 Marty Schoch REST discovery of buckets ddocs URI and method to get ddocs
mschoch authored
1 package couchbase
2
d4b0c22 Dustin Sallings Move PutDDoc into ddocs.go
dustin authored
3 import (
4 "bytes"
5 "encoding/json"
6 "fmt"
7 "io/ioutil"
c6f2b95 Manik Taneja MB-13109. Add retry logic to views APIs
maniktaneja authored
8 "log"
d4b0c22 Dustin Sallings Move PutDDoc into ddocs.go
dustin authored
9 "net/http"
10 )
11
c6f2b95 Manik Taneja MB-13109. Add retry logic to views APIs
maniktaneja authored
12 const MAX_RETRY_COUNT = 3
13
88b6e00 Dustin Sallings Removed some unnecessarily exported types in ddoc list
dustin authored
14 // ViewDefinition represents a single view within a design document.
bba4763 Marty Schoch REST discovery of buckets ddocs URI and method to get ddocs
mschoch authored
15 type ViewDefinition struct {
16 Map string `json:"map"`
75c8dae Sriram Melkote Suppress empty reduce function.
melkote authored
17 Reduce string `json:"reduce,omitempty"`
bba4763 Marty Schoch REST discovery of buckets ddocs URI and method to get ddocs
mschoch authored
18 }
19
88b6e00 Dustin Sallings Removed some unnecessarily exported types in ddoc list
dustin authored
20 // DDoc is the document body of a design document specifying a view.
21 type DDoc struct {
75c8dae Sriram Melkote Suppress empty reduce function.
melkote authored
22 Language string `json:"language,omitempty"`
bba4763 Marty Schoch REST discovery of buckets ddocs URI and method to get ddocs
mschoch authored
23 Views map[string]ViewDefinition `json:"views"`
24 }
25
88b6e00 Dustin Sallings Removed some unnecessarily exported types in ddoc list
dustin authored
26 // DDocsResult represents the result from listing the design
27 // documents.
bba4763 Marty Schoch REST discovery of buckets ddocs URI and method to get ddocs
mschoch authored
28 type DDocsResult struct {
88b6e00 Dustin Sallings Removed some unnecessarily exported types in ddoc list
dustin authored
29 Rows []struct {
30 DDoc struct {
31 Meta map[string]interface{}
98eaf35 Dustin Sallings More doc/golint cleanup
dustin authored
32 JSON DDoc
88b6e00 Dustin Sallings Removed some unnecessarily exported types in ddoc list
dustin authored
33 } `json:"doc"`
34 } `json:"rows"`
bba4763 Marty Schoch REST discovery of buckets ddocs URI and method to get ddocs
mschoch authored
35 }
36
88b6e00 Dustin Sallings Removed some unnecessarily exported types in ddoc list
dustin authored
37 // GetDDocs lists all design documents
bba4763 Marty Schoch REST discovery of buckets ddocs URI and method to get ddocs
mschoch authored
38 func (b *Bucket) GetDDocs() (DDocsResult, error) {
39 var ddocsResult DDocsResult
0632c53 Manik Taneja Add functions GetDDocWithRetry and GetDDocsWithRetry
maniktaneja authored
40
e1dffac Dustin Sallings Auth refactoring
dustin authored
41 err := b.pool.client.parseURLResponse(b.DDocs.URI, &ddocsResult)
bba4763 Marty Schoch REST discovery of buckets ddocs URI and method to get ddocs
mschoch authored
42 if err != nil {
43 return DDocsResult{}, err
44 }
45 return ddocsResult, nil
46 }
d4b0c22 Dustin Sallings Move PutDDoc into ddocs.go
dustin authored
47
0632c53 Manik Taneja Add functions GetDDocWithRetry and GetDDocsWithRetry
maniktaneja authored
48 func (b *Bucket) GetDDocWithRetry(docname string, into interface{}) error {
49
50 ddocURI := fmt.Sprintf("/%s/_design/%s", b.Name, docname)
51 err := b.parseAPIResponse(ddocURI, &into)
52 if err != nil {
53 return err
54 }
55 return nil
56 }
57
58 func (b *Bucket) GetDDocsWithRetry() (DDocsResult, error) {
59 var ddocsResult DDocsResult
60
61 err := b.parseURLResponse(b.DDocs.URI, &ddocsResult)
62 if err != nil {
63 return DDocsResult{}, err
64 }
65 return ddocsResult, nil
66 }
67
9d80792 Dustin Sallings Added ddoc get.
dustin authored
68 func (b *Bucket) ddocURL(docname string) (string, error) {
69 u, err := b.randomBaseURL()
70 if err != nil {
71 return "", err
72 }
73 u.Path = fmt.Sprintf("/%s/_design/%s", b.Name, docname)
74 return u.String(), nil
75 }
76
88b6e00 Dustin Sallings Removed some unnecessarily exported types in ddoc list
dustin authored
77 // PutDDoc installs a design document.
d4b0c22 Dustin Sallings Move PutDDoc into ddocs.go
dustin authored
78 func (b *Bucket) PutDDoc(docname string, value interface{}) error {
79
c6f2b95 Manik Taneja MB-13109. Add retry logic to views APIs
maniktaneja authored
80 var Err error
81 for retryCount := 0; retryCount < MAX_RETRY_COUNT; retryCount++ {
82
83 Err = nil
84 ddocU, err := b.ddocURL(docname)
85 if err != nil {
86 return err
87 }
88
89 j, err := json.Marshal(value)
90 if err != nil {
91 return err
92 }
93
94 req, err := http.NewRequest("PUT", ddocU, bytes.NewReader(j))
95 if err != nil {
96 return err
97 }
98 req.Header.Set("Content-Type", "application/json")
99 err = maybeAddAuth(req, b.authHandler())
100 if err != nil {
101 return err
102 }
103
104 res, err := HTTPClient.Do(req)
105 if err != nil {
106 return err
107 }
108 defer res.Body.Close()
109 if res.StatusCode != 201 {
110 body, _ := ioutil.ReadAll(res.Body)
111 Err = fmt.Errorf("error installing view: %v / %s",
112 res.Status, body)
113 log.Printf(" Error in PutDDOC %v. Retrying...", Err)
114 b.Refresh()
115 continue
116 }
117 break
118 }
119
120 return Err
d4b0c22 Dustin Sallings Move PutDDoc into ddocs.go
dustin authored
121 }
9d80792 Dustin Sallings Added ddoc get.
dustin authored
122
88b6e00 Dustin Sallings Removed some unnecessarily exported types in ddoc list
dustin authored
123 // GetDDoc retrieves a specific a design doc.
9d80792 Dustin Sallings Added ddoc get.
dustin authored
124 func (b *Bucket) GetDDoc(docname string, into interface{}) error {
c6f2b95 Manik Taneja MB-13109. Add retry logic to views APIs
maniktaneja authored
125 var Err error
126 var res *http.Response
127
128 for retryCount := 0; retryCount < MAX_RETRY_COUNT; retryCount++ {
129
130 Err = nil
131 ddocU, err := b.ddocURL(docname)
132 if err != nil {
133 return err
134 }
135
136 req, err := http.NewRequest("GET", ddocU, nil)
137 if err != nil {
138 return err
139 }
140 req.Header.Set("Content-Type", "application/json")
141 err = maybeAddAuth(req, b.authHandler())
142 if err != nil {
143 return err
144 }
145
146 res, err = HTTPClient.Do(req)
147 if err != nil {
148 return err
149 }
150 defer res.Body.Close()
151 if res.StatusCode != 200 {
152 body, _ := ioutil.ReadAll(res.Body)
153 Err = fmt.Errorf("error reading view: %v / %s",
154 res.Status, body)
155 log.Printf(" Error in GetDDOC %v Retrying...", Err)
156 b.Refresh()
157 continue
158 }
159 }
160
161 if Err != nil {
162 return Err
9d80792 Dustin Sallings Added ddoc get.
dustin authored
163 }
164
165 d := json.NewDecoder(res.Body)
166 return d.Decode(into)
167 }
ff7195c Sriram Melkote Add ability delete design docs
melkote authored
168
88b6e00 Dustin Sallings Removed some unnecessarily exported types in ddoc list
dustin authored
169 // DeleteDDoc removes a design document.
ff7195c Sriram Melkote Add ability delete design docs
melkote authored
170 func (b *Bucket) DeleteDDoc(docname string) error {
171
c6f2b95 Manik Taneja MB-13109. Add retry logic to views APIs
maniktaneja authored
172 var Err error
173 for retryCount := 0; retryCount < MAX_RETRY_COUNT; retryCount++ {
174
175 Err = nil
176 ddocU, err := b.ddocURL(docname)
177 if err != nil {
178 return err
179 }
180
181 req, err := http.NewRequest("DELETE", ddocU, nil)
182 if err != nil {
183 return err
184 }
185 req.Header.Set("Content-Type", "application/json")
186 err = maybeAddAuth(req, b.authHandler())
187 if err != nil {
188 return err
189 }
190
191 res, err := HTTPClient.Do(req)
192 if err != nil {
193 return err
194 }
195 defer res.Body.Close()
196 if res.StatusCode != 200 {
197 body, _ := ioutil.ReadAll(res.Body)
198 Err = fmt.Errorf("error deleting view : %v / %s", res.Status, body)
199 log.Printf(" Error in DeleteDDOC %v. Retrying ... ", Err)
200 b.Refresh()
201 continue
202 }
203
204 break
205 }
206 return Err
ff7195c Sriram Melkote Add ability delete design docs
melkote authored
207 }
Something went wrong with that request. Please try again.