1

I want to be able to get a list of views available for a bucket using the python API (or any API). How do I do this?

Mathieu Longtin
  • 15,922
  • 6
  • 30
  • 40

1 Answers1

1
import couchbase
from couchbase.rest_client import RestConnection
import json

server_info = { "ip" : "127.0.0.1", "port" : 8092, 
                "username" : "", 
                "password" : "", 
                "couchApiBase" :  "http://127.0.0.1:8092/" }
rest = RestConnection(server_info)

params = "startkey=\"_design\"&endkey=\"_design0\""
design_docs_uri = "http://%s:8091/couchBase/default/_all_docs?%s" % (server_info["ip"], params)

#not supported as public API
response, content = rest._http_request(design_docs_uri, 'GET', headers=rest._create_headers())
json_parsed = json.loads(content)

for row in json_parsed["rows"]:
    design_doc = row["key"].split("/")[1]

    print "Views for design doc: %s" % design_doc
    doc = rest.get_design_doc("default", design_doc)
    #get views element from dictionary
    for view in doc["views"]:
        print "\t%s" % view
John Zablocki
  • 1,592
  • 9
  • 6
  • I get this: Exception: unable to get design doc. – Mathieu Longtin Apr 14 '12 at 14:01
  • Which version of the client are you using? I just tried this code again and had no problems. Stupid question, but did you update the bucket and design doc names in the call to get_design_doc? – John Zablocki Apr 25 '12 at 15:12
  • I guess I need to get the list of design docs first, like this: http://stackoverflow.com/questions/2814352/get-all-design-documents-in-couchdb – Mathieu Longtin Apr 26 '12 at 00:27
  • Right, if you want to first find all the design docs, you would need to access http://localhost:8091/couchBase/default/_all_docs?startkey="_design"&endkey="_design0" – John Zablocki Apr 26 '12 at 14:28
  • I updated the sample in the answer to include looking up the design docs first and then finding all available views. – John Zablocki Apr 26 '12 at 15:01