You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@couchdb.apache.org by Sébastien Durand <se...@gmail.com> on 2013/11/12 02:40:59 UTC
A Simple REST API
Hi !
I want to use CouchDB for a Ruby project. I downloaded CouchRest, but this
driver seems to be overkill for what I want : a simple REST API.
So, is it wrong to design my own driver (using tcp sockets) ? Like this :
class CouchDB
def initialize(database, host = 'localhost', port = 5984)
@database, @host, @port = database, host, port
end
def get(uri)
if block_given?
yield get(uri)
else
socket = TCPSocket.open(@host, @port)
request = ""
request << "GET /#{@database}/#{uri} HTTP/1.0\r\n"
request << "\r\n"
socket.write(request)
response = socket.read
socket.close
# TODO : parsing json
end
end
end
It's just a dead simple draft, it works, but what are the drawbacks ? Do I
put myself in trouble ? (I don't have a deep tcp/http background and I fear
it's not that simple.)
Thank U for you help !
Sébastien
Re: A Simple REST API
Posted by Filippo Fadda <fi...@programmazione.it>.
ElephantOnCouch is a CouchDB client written in PHP, using both cURL or raw sockets (default method, handle chunk responses, persistent connection, etc.) as transport layers. In ElephantOnCouch you don't save JSON documents, but objects. You can store every instance of any Doc subclass you have defined, or you can implement the IDoc interface or use the trait TDoc in your classes so they become persistent. When you retrieve a document from CouchDB using ElephantOnCouch you obtain an object of the class you have previously stored. Your objects are translated in persistent documents so you can write easily object oriented software.
ElephantOnCouch comes with a PHP Query Server (EOCSvr), giving you the ability to write map/reduce and other handlers in PHP. ElephantOnCouch is not just a REST client to use the CouchDB APIs, but it's a complete CouchDB client with methods for all the CouchDB APIs and options. You don't need to know when use POST or PUT or GET or deal with CouchDB documentation, you just use the ElephantOnCouch methods. The exhaustive documentation can be generated using Doxygen.
There is nothing like ElephantOnCouch and you can't find any description because it's still a work in progress. I still have to implement some handlers, adding caching, etc. But it works. Feel free to contribute: writing tests, a tutorial, making a logo, etc.
Here an example of a query:
// Establishes the connection.
$couch = new Couch("127.0.0.1:5984", "foo", "bar");
// Selects database.
$couch->selectDb("test");
// Sets the options.
$opts = new ViewQueryOpts();
$opts->doNotReduce()->setLimit(30)->reverseOrderOfResults()->setStartKey(['blog', new \stdClass()])->setEndKey(['blog']);
// Queries the view.
$rows = $couch->queryView("posts", "newestPerSection", NULL, $opts)['rows'];
I have also created a command line console (like mysql), that let you interact directly with CouchDB. Not all the commands are implemented, but I will release it as part of the ElephantOnCouch suite soon as I can.
fff@macpro:~/Dropbox/Progetti/pit-press$ pit
PitPress Console version 0.1.0
Usage:
[options] command [arguments]
Options:
--help -h Display this help message.
--quiet -q Do not output any message.
--verbose -v|vv|vvv Increase the verbosity of messages: 1 for normal output, 2 for more verbose output and 3 for debug
--version -V Display this application version.
--ansi Force ANSI output.
--no-ansi Disable ANSI output.
--no-interaction -n Do not ask any interactive question.
Available commands:
about Displays information about PitPress, like version, database, etc.
cleanup Removes all outdated view indexes.
commit Makes sure all uncommited database changes are written and synchronized to the disk.
compact Starts a compaction for the current selected database.
create Creates a new database.
delete Deletes the PitPress database.
generate Generates fake accessory documents per post.
help Displays help for a command
import Imports into CouchDB the data from Programmazione.it v6.4 MySQL database.
init Initializes the PitPress database, adding the required design documents.
install Executes the following commands: create, prepare, import all, init all.
list Lists commands
prepare Prepares Programmazione.it v6.4 MySQL database to be imported.
query Query a view and outputs the result.
restore Restores Programmazione.it v6.4 MySQL database.
status Gets PitPress list of active tasks.
fff@macpro:~/Dropbox/Progetti/pit-press$ pit help query
Usage:
query [--key="..."] [--startkey="..."] [--endkey="..."] [--startkey-docid="..."] [--endkey-docid="..."] [--limit="..."] [--group-results] [--group-level="..."] [--do-not-reduce] [--include-docs] [--exclude-results] [--exclude-endkey] [--reverse-order] [--skip="..."] [--include-conflicts] [--include-missing-keys] [--map="..."] [--reduce="..."] [--language="..."] design-doc/view-name [keys1] ... [keysN]
Arguments:
design-doc/view-name The design document name followed by the view you want query. In case of a temporary view,
use: _temp_view --map=map.txt --reduce=reduce.txt. The files map.txt and reduce.txt must contains the map and
reduce functions implementation.
keys Used to retrieve just the view rows matching that set of keys. Rows are returned in the order of the specified
keys. Combining this feature with --include-docs results in the so-called multi-document-fetch feature.
In case yours keys are string, they must be quoted with double quotes and escaped, like \"firstkey\", \"secondkey\".
Options:
--key Returns only documents that match the specified key.
In case the key is a string, it must be quoted with double quotes and escaped, like --key=\"mykey\".
--startkey Defines the first key to be included in the range.
In case the key is a string, it must be quoted with double quotes and escaped, like --startkey=\"mykey\".
To provide a complex key, instead, you must use --startkey=[\"book\",{}]. The {} symbol is a wildcard used in
JavaScript to create an empty object.
Don't put a space between the values of your complex key, because the console will consider them like new arguments.
Unfortunately, there is known bug that don't let you write something like --startkey=[\"my book\",{}].
--endkey Defines the last key to be included in the range.
In case the key is a string, it must be quoted with double quotes and escaped, like --endkey=\"mykey\".
--startkey-docid Sets the ID of the document with which to start the range.
--endkey-docid Sets the ID of the document with which to end the range.
--limit Limit the number of results.
--group-results Results should be grouped.
--group-level Level at which documents should be grouped.
--do-not-reduce Even is a reduce function is defined for the view, doesn't call it.
--include-docs Includes documents in the output.
--exclude-results Don't get any data, but all meta-data for this View. The number of documents in this View for example.
--exclude-endkey Tells CouchDB to not include end key in the result.
--reverse-order Reverses order of results.
--skip Skips the defined number of documents.
--include-conflicts Includes conflict documents.
--include-missing-keys Includes all the rows, even if a match for a key is not found.
--map Load map function from this file. To be used with _temp_view only, ignored otherwise.
--reduce Load reduce function from this file. To be used with _temp_view only, ignored otherwise.
--language The language used to implement the map and reduce functions. If no specified, PHP assumed.
--help (-h) Display this help message.
--quiet (-q) Do not output any message.
--verbose (-v|vv|vvv) Increase the verbosity of messages: 1 for normal output, 2 for more verbose output and 3 for debug
--version (-V) Display this application version.
--ansi Force ANSI output.
--no-ansi Disable ANSI output.
--no-interaction (-n) Do not ask any interactive question.
fff@macpro:~$ pit query index/latest --do-not-reduce --limit=30 --reverse-order
{"total_rows":21534,"offset":0,"rows":[
{"id":"76b646a4-7797-4852-f6f7-425628abdc3b","key":"2013-03-15 13:00:26","value":"76b646a4-7797-4852-f6f7-425628abdc3b"},
{"id":"c88803a2-84a8-4ef7-ad14-11fc16068992","key":"2013-03-05 15:55:01","value":"c88803a2-84a8-4ef7-ad14-11fc16068992"},
{"id":"ba427ec2-6a97-47c5-e94e-ffffcc918a1a","key":"2013-02-25 02:08:23","value":"ba427ec2-6a97-47c5-e94e-ffffcc918a1a"},
{"id":"2e3b9ad3-d0fa-434a-e8f9-28f7d0c2fe0f","key":"2013-02-25 01:41:21","value":"2e3b9ad3-d0fa-434a-e8f9-28f7d0c2fe0f"},
{"id":"f6b85de6-ffeb-49cc-c421-9fa2a2db898f","key":"2013-02-19 14:45:22","value":"f6b85de6-ffeb-49cc-c421-9fa2a2db898f"},
{"id":"5de4713f-405f-4bd9-dd1b-35836416affe","key":"2013-02-11 12:06:06","value":"5de4713f-405f-4bd9-dd1b-35836416affe"},
{"id":"fa8546c7-73be-4ff0-c04a-a78f877b5351","key":"2013-02-05 16:57:56","value":"fa8546c7-73be-4ff0-c04a-a78f877b5351"},
{"id":"552aae3d-60c6-4942-9f78-2ae349ebf9aa","key":"2013-01-30 13:20:25","value":"552aae3d-60c6-4942-9f78-2ae349ebf9aa"},
{"id":"bce6118c-8401-4043-eade-8267c406f909","key":"2013-01-24 05:43:21","value":"bce6118c-8401-4043-eade-8267c406f909"},
{"id":"555c87df-3a0f-4886-bb71-49437b3d76c0","key":"2012-12-23 20:03:14","value":"555c87df-3a0f-4886-bb71-49437b3d76c0"},
{"id":"0c1295d1-b701-41e6-b040-0e9a31671f91","key":"2012-12-19 19:36:16","value":"0c1295d1-b701-41e6-b040-0e9a31671f91"},
{"id":"8d2ceb67-449f-4cb7-dd38-6c752a422b77","key":"2012-12-17 14:37:57","value":"8d2ceb67-449f-4cb7-dd38-6c752a422b77"},
{"id":"659eece4-5f01-489a-98c9-3fb7409f8944","key":"2012-12-07 14:46:27","value":"659eece4-5f01-489a-98c9-3fb7409f8944"},
{"id":"fb0ff625-b6d6-4c69-8a24-5c27a3cda24e","key":"2012-12-03 12:07:10","value":"fb0ff625-b6d6-4c69-8a24-5c27a3cda24e"},
{"id":"58666125-ec14-4838-d00b-9dbd083154bf","key":"2012-12-03 11:58:57","value":"58666125-ec14-4838-d00b-9dbd083154bf"},
{"id":"e4a8fb99-0a14-48b0-d17b-6baf69ab8639","key":"2012-11-27 16:40:42","value":"e4a8fb99-0a14-48b0-d17b-6baf69ab8639"},
{"id":"2fe1d8c0-1503-4cdb-9766-43a48729a340","key":"2012-11-26 10:15:46","value":"2fe1d8c0-1503-4cdb-9766-43a48729a340"},
{"id":"ce81793f-a109-4596-b06d-6c6de8394197","key":"2012-11-26 10:11:53","value":"ce81793f-a109-4596-b06d-6c6de8394197"},
{"id":"97b25567-b67b-4a94-a58e-7d9c8c2f299d","key":"2012-11-26 09:54:12","value":"97b25567-b67b-4a94-a58e-7d9c8c2f299d"},
{"id":"e428abe4-be46-4d7f-fc6e-3103c15412f6","key":"2012-11-15 01:02:24","value":"e428abe4-be46-4d7f-fc6e-3103c15412f6"},
{"id":"cfd21d1e-03f8-49b9-9e22-26b897b4d977","key":"2012-11-15 00:59:26","value":"cfd21d1e-03f8-49b9-9e22-26b897b4d977"},
{"id":"80552941-2fbd-4a22-e8cf-176c394f90db","key":"2012-11-05 16:16:08","value":"80552941-2fbd-4a22-e8cf-176c394f90db"},
{"id":"c7664789-db86-4950-80eb-f7d040faa7ae","key":"2012-11-01 18:05:05","value":"c7664789-db86-4950-80eb-f7d040faa7ae"},
{"id":"fa81665c-dc2f-40bb-bf3d-de888704d3e8","key":"2012-10-30 01:06:40","value":"fa81665c-dc2f-40bb-bf3d-de888704d3e8"},
{"id":"4924c23f-6bc5-4eca-aba8-5dc4605e2beb","key":"2012-10-25 03:30:29","value":"4924c23f-6bc5-4eca-aba8-5dc4605e2beb"},
{"id":"a7942413-91ef-4fa9-8da8-ca376fc60162","key":"2012-10-25 03:28:22","value":"a7942413-91ef-4fa9-8da8-ca376fc60162"},
{"id":"fdfea671-eb01-4827-c84d-0b3a00b67212","key":"2012-10-22 15:31:47","value":"fdfea671-eb01-4827-c84d-0b3a00b67212"},
{"id":"b86e458e-2fa0-4648-9c60-c5be0120fd1f","key":"2012-10-17 16:33:27","value":"b86e458e-2fa0-4648-9c60-c5be0120fd1f"},
{"id":"3aacf6a1-30f2-4b79-d391-89db75bfa0cd","key":"2012-10-17 16:25:32","value":"3aacf6a1-30f2-4b79-d391-89db75bfa0cd"},
{"id":"82e1412e-2c46-4d06-d6d9-442a2fe0669c","key":"2012-10-11 17:07:41","value":"82e1412e-2c46-4d06-d6d9-442a2fe0669c"}
]}
On Nov 12, 2013, at 4:41 AM, Mark Deibert wrote:
> What is ElephantOnCouch?!?! There is zero info on the github. I'm very
> intrigued...
Re: A Simple REST API
Posted by Andy Wenk <an...@nms.de>.
everything what the others said ;-)
To get an idea for one way to use Ruby against CouchDB you are welcome to
check this out
https://github.com/andywenk/couchdbtools
This my playground - nothing production ready! But I think there are the
Ruby libs the other readers suggested ...
Cheers
Andy
On 12 November 2013 08:19, Sébastien Durand <se...@gmail.com> wrote:
> Thank you very much for your comments guys (and for not beating me for my
> stupid idea) ! ;-) I don't know why I started with this sockets thing
> (low-level performance seeking maybe ?)
>
> Yes, I will move to "net/http" and "json" from Ruby stdlib (rather than
> implementing the http client myself). I will come back to share the code
> when it's ready. :-)
>
> Two good links I found on the topic :
>
> http://wiki.apache.org/couchdb/Getting_started_with_Ruby (what I was
> trying
> to achieve)
> http://www.youtube.com/watch?v=zEMfvCqVL4E (CouchDB & Ruby: you're doing
> it
> wrong by Tim Anglade)
>
> And by the way, I love the name ElephantOnCouch lol.
>
>
>
> On Tue, Nov 12, 2013 at 4:41 AM, Mark Deibert <mark.deibert@gmail.com
> >wrote:
>
> > What is ElephantOnCouch?!?! There is zero info on the github. I'm very
> > intrigued...
> >
> >
> > On Mon, Nov 11, 2013 at 9:54 PM, Filippo Fadda <
> > filippo.fadda@programmazione.it> wrote:
> >
> > > Just use PHP and ElephantOnCouch! :-)
> > >
> > > -Filippo
> > >
> > > On Nov 12, 2013, at 3:40 AM, Alexander Shorin wrote:
> > >
> > > > I wonder why you prefer to work with raw sockets instead of using one
> > > > of many available http clients for Ruby...
> > > > Just pick one that you like:
> > > >
> http://www.slideshare.net/HiroshiNakamura/rubyhttp-clients-comparison
> > > > To really work with simple rest api you don't need to reimplement
> > > > baseline things that have been already done and tested.
> > > > --
> > > > ,,,^..^,,,
> > > >
> > > >
> > > > On Tue, Nov 12, 2013 at 5:40 AM, Sébastien Durand <se8.and@gmail.com
> >
> > > wrote:
> > > >> Hi !
> > > >>
> > > >> I want to use CouchDB for a Ruby project. I downloaded CouchRest,
> but
> > > this
> > > >> driver seems to be overkill for what I want : a simple REST API.
> > > >>
> > > >> So, is it wrong to design my own driver (using tcp sockets) ? Like
> > this
> > > :
> > > >>
> > > >>
> > > >> class CouchDB
> > > >>
> > > >> def initialize(database, host = 'localhost', port = 5984)
> > > >> @database, @host, @port = database, host, port
> > > >> end
> > > >>
> > > >> def get(uri)
> > > >> if block_given?
> > > >> yield get(uri)
> > > >> else
> > > >> socket = TCPSocket.open(@host, @port)
> > > >> request = ""
> > > >> request << "GET /#{@database}/#{uri} HTTP/1.0\r\n"
> > > >> request << "\r\n"
> > > >> socket.write(request)
> > > >> response = socket.read
> > > >> socket.close
> > > >> # TODO : parsing json
> > > >> end
> > > >> end
> > > >>
> > > >> end
> > > >>
> > > >>
> > > >> It's just a dead simple draft, it works, but what are the drawbacks
> ?
> > > Do I
> > > >> put myself in trouble ? (I don't have a deep tcp/http background
> and I
> > > fear
> > > >> it's not that simple.)
> > > >>
> > > >> Thank U for you help !
> > > >>
> > > >> Sébastien
> > >
> > >
> >
>
--
Andy Wenk
Hamburg - Germany
RockIt!
http://www.couchdb-buch.de
http://www.pg-praxisbuch.de
GPG fingerprint: C044 8322 9E12 1483 4FEC 9452 B65D 6BE3 9ED3 9588
Re: A Simple REST API
Posted by Sébastien Durand <se...@gmail.com>.
Thank you very much for your comments guys (and for not beating me for my
stupid idea) ! ;-) I don't know why I started with this sockets thing
(low-level performance seeking maybe ?)
Yes, I will move to "net/http" and "json" from Ruby stdlib (rather than
implementing the http client myself). I will come back to share the code
when it's ready. :-)
Two good links I found on the topic :
http://wiki.apache.org/couchdb/Getting_started_with_Ruby (what I was trying
to achieve)
http://www.youtube.com/watch?v=zEMfvCqVL4E (CouchDB & Ruby: you're doing it
wrong by Tim Anglade)
And by the way, I love the name ElephantOnCouch lol.
On Tue, Nov 12, 2013 at 4:41 AM, Mark Deibert <ma...@gmail.com>wrote:
> What is ElephantOnCouch?!?! There is zero info on the github. I'm very
> intrigued...
>
>
> On Mon, Nov 11, 2013 at 9:54 PM, Filippo Fadda <
> filippo.fadda@programmazione.it> wrote:
>
> > Just use PHP and ElephantOnCouch! :-)
> >
> > -Filippo
> >
> > On Nov 12, 2013, at 3:40 AM, Alexander Shorin wrote:
> >
> > > I wonder why you prefer to work with raw sockets instead of using one
> > > of many available http clients for Ruby...
> > > Just pick one that you like:
> > > http://www.slideshare.net/HiroshiNakamura/rubyhttp-clients-comparison
> > > To really work with simple rest api you don't need to reimplement
> > > baseline things that have been already done and tested.
> > > --
> > > ,,,^..^,,,
> > >
> > >
> > > On Tue, Nov 12, 2013 at 5:40 AM, Sébastien Durand <se...@gmail.com>
> > wrote:
> > >> Hi !
> > >>
> > >> I want to use CouchDB for a Ruby project. I downloaded CouchRest, but
> > this
> > >> driver seems to be overkill for what I want : a simple REST API.
> > >>
> > >> So, is it wrong to design my own driver (using tcp sockets) ? Like
> this
> > :
> > >>
> > >>
> > >> class CouchDB
> > >>
> > >> def initialize(database, host = 'localhost', port = 5984)
> > >> @database, @host, @port = database, host, port
> > >> end
> > >>
> > >> def get(uri)
> > >> if block_given?
> > >> yield get(uri)
> > >> else
> > >> socket = TCPSocket.open(@host, @port)
> > >> request = ""
> > >> request << "GET /#{@database}/#{uri} HTTP/1.0\r\n"
> > >> request << "\r\n"
> > >> socket.write(request)
> > >> response = socket.read
> > >> socket.close
> > >> # TODO : parsing json
> > >> end
> > >> end
> > >>
> > >> end
> > >>
> > >>
> > >> It's just a dead simple draft, it works, but what are the drawbacks ?
> > Do I
> > >> put myself in trouble ? (I don't have a deep tcp/http background and I
> > fear
> > >> it's not that simple.)
> > >>
> > >> Thank U for you help !
> > >>
> > >> Sébastien
> >
> >
>
Re: A Simple REST API
Posted by Mark Deibert <ma...@gmail.com>.
What is ElephantOnCouch?!?! There is zero info on the github. I'm very
intrigued...
On Mon, Nov 11, 2013 at 9:54 PM, Filippo Fadda <
filippo.fadda@programmazione.it> wrote:
> Just use PHP and ElephantOnCouch! :-)
>
> -Filippo
>
> On Nov 12, 2013, at 3:40 AM, Alexander Shorin wrote:
>
> > I wonder why you prefer to work with raw sockets instead of using one
> > of many available http clients for Ruby...
> > Just pick one that you like:
> > http://www.slideshare.net/HiroshiNakamura/rubyhttp-clients-comparison
> > To really work with simple rest api you don't need to reimplement
> > baseline things that have been already done and tested.
> > --
> > ,,,^..^,,,
> >
> >
> > On Tue, Nov 12, 2013 at 5:40 AM, Sébastien Durand <se...@gmail.com>
> wrote:
> >> Hi !
> >>
> >> I want to use CouchDB for a Ruby project. I downloaded CouchRest, but
> this
> >> driver seems to be overkill for what I want : a simple REST API.
> >>
> >> So, is it wrong to design my own driver (using tcp sockets) ? Like this
> :
> >>
> >>
> >> class CouchDB
> >>
> >> def initialize(database, host = 'localhost', port = 5984)
> >> @database, @host, @port = database, host, port
> >> end
> >>
> >> def get(uri)
> >> if block_given?
> >> yield get(uri)
> >> else
> >> socket = TCPSocket.open(@host, @port)
> >> request = ""
> >> request << "GET /#{@database}/#{uri} HTTP/1.0\r\n"
> >> request << "\r\n"
> >> socket.write(request)
> >> response = socket.read
> >> socket.close
> >> # TODO : parsing json
> >> end
> >> end
> >>
> >> end
> >>
> >>
> >> It's just a dead simple draft, it works, but what are the drawbacks ?
> Do I
> >> put myself in trouble ? (I don't have a deep tcp/http background and I
> fear
> >> it's not that simple.)
> >>
> >> Thank U for you help !
> >>
> >> Sébastien
>
>
Re: A Simple REST API
Posted by Stanley Iriele <si...@gmail.com>.
Err...just use a http client of any kind... Your driver is essentially
that...I've use drivers in the past but I always find them a tad lacking or
it makes it harder to do what I want...
On Nov 11, 2013 6:54 PM, "Filippo Fadda" <fi...@programmazione.it>
wrote:
> Just use PHP and ElephantOnCouch! :-)
>
> -Filippo
>
> On Nov 12, 2013, at 3:40 AM, Alexander Shorin wrote:
>
> > I wonder why you prefer to work with raw sockets instead of using one
> > of many available http clients for Ruby...
> > Just pick one that you like:
> > http://www.slideshare.net/HiroshiNakamura/rubyhttp-clients-comparison
> > To really work with simple rest api you don't need to reimplement
> > baseline things that have been already done and tested.
> > --
> > ,,,^..^,,,
> >
> >
> > On Tue, Nov 12, 2013 at 5:40 AM, Sébastien Durand <se...@gmail.com>
> wrote:
> >> Hi !
> >>
> >> I want to use CouchDB for a Ruby project. I downloaded CouchRest, but
> this
> >> driver seems to be overkill for what I want : a simple REST API.
> >>
> >> So, is it wrong to design my own driver (using tcp sockets) ? Like this
> :
> >>
> >>
> >> class CouchDB
> >>
> >> def initialize(database, host = 'localhost', port = 5984)
> >> @database, @host, @port = database, host, port
> >> end
> >>
> >> def get(uri)
> >> if block_given?
> >> yield get(uri)
> >> else
> >> socket = TCPSocket.open(@host, @port)
> >> request = ""
> >> request << "GET /#{@database}/#{uri} HTTP/1.0\r\n"
> >> request << "\r\n"
> >> socket.write(request)
> >> response = socket.read
> >> socket.close
> >> # TODO : parsing json
> >> end
> >> end
> >>
> >> end
> >>
> >>
> >> It's just a dead simple draft, it works, but what are the drawbacks ?
> Do I
> >> put myself in trouble ? (I don't have a deep tcp/http background and I
> fear
> >> it's not that simple.)
> >>
> >> Thank U for you help !
> >>
> >> Sébastien
>
>
Re: A Simple REST API
Posted by Filippo Fadda <fi...@programmazione.it>.
Just use PHP and ElephantOnCouch! :-)
-Filippo
On Nov 12, 2013, at 3:40 AM, Alexander Shorin wrote:
> I wonder why you prefer to work with raw sockets instead of using one
> of many available http clients for Ruby...
> Just pick one that you like:
> http://www.slideshare.net/HiroshiNakamura/rubyhttp-clients-comparison
> To really work with simple rest api you don't need to reimplement
> baseline things that have been already done and tested.
> --
> ,,,^..^,,,
>
>
> On Tue, Nov 12, 2013 at 5:40 AM, Sébastien Durand <se...@gmail.com> wrote:
>> Hi !
>>
>> I want to use CouchDB for a Ruby project. I downloaded CouchRest, but this
>> driver seems to be overkill for what I want : a simple REST API.
>>
>> So, is it wrong to design my own driver (using tcp sockets) ? Like this :
>>
>>
>> class CouchDB
>>
>> def initialize(database, host = 'localhost', port = 5984)
>> @database, @host, @port = database, host, port
>> end
>>
>> def get(uri)
>> if block_given?
>> yield get(uri)
>> else
>> socket = TCPSocket.open(@host, @port)
>> request = ""
>> request << "GET /#{@database}/#{uri} HTTP/1.0\r\n"
>> request << "\r\n"
>> socket.write(request)
>> response = socket.read
>> socket.close
>> # TODO : parsing json
>> end
>> end
>>
>> end
>>
>>
>> It's just a dead simple draft, it works, but what are the drawbacks ? Do I
>> put myself in trouble ? (I don't have a deep tcp/http background and I fear
>> it's not that simple.)
>>
>> Thank U for you help !
>>
>> Sébastien
Re: A Simple REST API
Posted by Alexander Shorin <kx...@gmail.com>.
I wonder why you prefer to work with raw sockets instead of using one
of many available http clients for Ruby...
Just pick one that you like:
http://www.slideshare.net/HiroshiNakamura/rubyhttp-clients-comparison
To really work with simple rest api you don't need to reimplement
baseline things that have been already done and tested.
--
,,,^..^,,,
On Tue, Nov 12, 2013 at 5:40 AM, Sébastien Durand <se...@gmail.com> wrote:
> Hi !
>
> I want to use CouchDB for a Ruby project. I downloaded CouchRest, but this
> driver seems to be overkill for what I want : a simple REST API.
>
> So, is it wrong to design my own driver (using tcp sockets) ? Like this :
>
>
> class CouchDB
>
> def initialize(database, host = 'localhost', port = 5984)
> @database, @host, @port = database, host, port
> end
>
> def get(uri)
> if block_given?
> yield get(uri)
> else
> socket = TCPSocket.open(@host, @port)
> request = ""
> request << "GET /#{@database}/#{uri} HTTP/1.0\r\n"
> request << "\r\n"
> socket.write(request)
> response = socket.read
> socket.close
> # TODO : parsing json
> end
> end
>
> end
>
>
> It's just a dead simple draft, it works, but what are the drawbacks ? Do I
> put myself in trouble ? (I don't have a deep tcp/http background and I fear
> it's not that simple.)
>
> Thank U for you help !
>
> Sébastien
Re: A Simple REST API
Posted by Miles Fidelman <mf...@meetinghouse.net>.
Sébastien Durand wrote:
> Hi !
>
> I want to use CouchDB for a Ruby project. I downloaded CouchRest, but this
> driver seems to be overkill for what I want : a simple REST API.
Umm... you're making things way to complicated. Couchdb's API is REST
all the way down to start with.
Miles Fidelman
--
In theory, there is no difference between theory and practice.
In practice, there is. .... Yogi Berra
Re: A Simple REST API
Posted by Jens Alfke <je...@couchbase.com>.
Oh, I should add: If you want to handle HTTP yourself, just use the net/http module in the Ruby standard library.
But I think you’ll find yourself writing repetitive blocks of code in calls that you make to it, because there are common REST and CouchDB patterns to a lot of the API calls. Stuff like “append database name and document ID to server URL, add common query params, send request, check result code, unpack JSON …" It’s easier to use a CouchDB client library that encapsulates that stuff for you.
—Jens
Re: A Simple REST API
Posted by Jens Alfke <je...@couchbase.com>.
On Nov 11, 2013, at 5:40 PM, Sébastien Durand <se...@gmail.com> wrote:
> It's just a dead simple draft, it works, but what are the drawbacks ? Do I
> put myself in trouble ? (I don't have a deep tcp/http background and I fear
> it's not that simple.)
It’s not that simple. HTTP isn’t rocket science but it has a lot of details and some subtleties. You do NOT want to go about re-implementing an HTTP client on your own. It’s going to be a big pain in the ass and get in the way of what you really want to do. And Ruby already has perfectly good HTTP support.
Just off the top of my head, your code will break with redirects, chunked-mode responses or gzip encoding, and it doesn’t support keep-alive. If you added code to parse the response, I could probably find six or seven other things that didn’t do. I’m not trying to be harsh, but I've spent a decade working closely with HTTP and I know the pitfalls.
—Jens
Re: A Simple REST API
Posted by Filippo Fadda <fi...@programmazione.it>.
You need to implement a full HTTP/1.1 client with chunk response support, etc. Not so easy, especially if you don't have a deep background. At least use a gem that implements the http client or use cURL.
-Filippo
On Nov 12, 2013, at 2:40 AM, Sébastien Durand wrote:
> Hi !
>
> I want to use CouchDB for a Ruby project. I downloaded CouchRest, but this
> driver seems to be overkill for what I want : a simple REST API.
>
> So, is it wrong to design my own driver (using tcp sockets) ? Like this :
>
>
> class CouchDB
>
> def initialize(database, host = 'localhost', port = 5984)
> @database, @host, @port = database, host, port
> end
>
> def get(uri)
> if block_given?
> yield get(uri)
> else
> socket = TCPSocket.open(@host, @port)
> request = ""
> request << "GET /#{@database}/#{uri} HTTP/1.0\r\n"
> request << "\r\n"
> socket.write(request)
> response = socket.read
> socket.close
> # TODO : parsing json
> end
> end
>
> end
>
>
> It's just a dead simple draft, it works, but what are the drawbacks ? Do I
> put myself in trouble ? (I don't have a deep tcp/http background and I fear
> it's not that simple.)
>
> Thank U for you help !
>
> Sébastien