Ignore:
Timestamp:
03/05/14 22:44:48 (11 years ago)
Author:
hendrikvanantwerpen
Message:

Completed migration to API, without CouchDB proxy.

Move to API is now completed. The full API is password protected, a very
limited API is exposed for respondents, which works with secrets that
are passed in URLs.

Serverside the HTTPResult class was introduced, which is similar to
Promises, but specifically for HTTP. It carries a status code and
response and makes it easier to extract parts of async handling in
separate functions.

Fixed a bug in our schema (it seems optional attributes don't exist but
a required list does). Verification of our schema by grunt-tv4 didn't
work yet. Our schema is organized the wrong way (this is fixable),
but the json-schema schema has problems with simple types and $refs.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • Dev/trunk/src/server/util/couch.coffee

    r479 r487  
    66 
    77class CouchDB
    8     constructor: (url) ->
    9         @url = normalizeURL url
    10     get: (id) ->
     8    constructor: (url,db) ->
     9        @serverURL = normalizeURL url
     10        @db = db
     11        if @db
     12            @url = "#{@serverURL}/#{@db}"
     13        else
     14            @url = "#{@serverURL}"
     15    get: (id, opts) ->
    1116        url = "#{@url}/#{id}"
    12         couchRequest 'GET', url
    13     post: (doc) ->
    14         url = "#{@url}"
    15         couchRequest 'POST', url, doc
    16     put: (id, doc) ->
     17        couchRequest 'GET', url, null, opts
     18    post: (doc, opts) ->
     19        url = "#{@url}/"
     20        couchRequest 'POST', url, doc, opts
     21    put: (id, doc, opts) ->
    1722        url = "#{@url}/#{id}"
    18         couchRequest 'PUT', url, doc
    19     delete: (id, rev) ->
    20         url = "#{@url}/#{id}?rev=#{rev}"
    21         couchRequest 'DELETE', url
     23        couchRequest 'PUT', url, doc, opts
     24    delete: (id, opts) ->
     25        url = "#{@url}/#{id}"
     26        couchRequest 'DELETE', url, null, opts
     27    uuids: (opts) ->
     28        url = "#{@serverURL}/_uuids"
     29        couchRequest 'GET', url, null, opts
    2230
    2331normalizeURL = (url) ->
    2432    url.replace /\/$/, ''
    2533
    26 couchRequest = (method, url, data) ->
     34couchRequest = (method, url, body, opts) ->
    2735    options =
    2836        method: method,
    2937        headers:
    30             'content-type': 'application/json; charset=utf-8'
    31             'accept': 'application/json'
    32         body: JSON.stringify (stringifyFunctions (data || {}))
     38            'Content-Type': 'application/json; charset=utf-8'
     39            'Accept': 'application/json'
     40        body: JSON.stringify (stringifyFunctions (body || {}))
     41    if opts
     42        options.qs = createQueryObj opts.query if opts.query
     43        _.extend options.headers, opts.headers if opts.headers
    3344    req = request(url, options)
    34     req.response
    35     .then (res) =>
     45    res = req.response.then (res) =>
    3646        req.then (res) =>
    3747            JSON.parse res
     
    4050    , (err) =>
    4151        Q.reject err
     52    res.response = req.response.then (res) =>
     53        statusCode: res.statusCode
     54        headers: res.headers
     55        body: JSON.parse res.body
     56    , (err) =>
     57        Q.reject err
     58    res
    4259
     60createQueryObj = (obj) ->
     61    newObj = {}
     62    _.each obj, (val,key) ->
     63        newObj[key] = JSON.stringify val
     64    newObj
     65       
    4366stringifyFunctions = (value) ->
    4467    if value is null
Note: See TracChangeset for help on using the changeset viewer.