danielwertheim

danielwertheim


notes from a passionate developer

Developer that lives by the mantra "code is meant to be shared".

Share


Tags


Disclaimer

This is a personal blog. The opinions expressed here represent my own and not those of my employer, nor current or previous. All content is published "as is", without warranty of any kind and I don't take any responsibility and can't be liable for any claims, damages or other liabilities that might be caused by the content.

MyCouch says hello to Cloudant

Daniel WertheimDaniel Wertheim

I just took MyCouch the .Net client for CouchDb for a spin with Cloudant. My findings? It was super easy to get started with a multi tenant account at Cloudant. You could probably do it under 5 min. Let me take you through some easy steps and show you how-to do it.

Setting things up

First, I Just went over to Cloudant’s site and registered for an account and then created a database in the dashboard.


mycouch-hello-cloudant-createdb


Second, in the dashboard, after creating the database, I generated an API-key for use with my test app.
mycouch-hello-cloudant-apikey


Finally, I configured the API-key's permissions for the database.
mycouch-hello-cloudant-permissions


Bam! That’s it. If you followed this, your all set and ready to go and now have a hosted NoSQL data storage solution available to you over HTTP.

Code away

Lets look at a quick sample of how to currently connect (simplified API is coming) to Cloudant and make use of the hosted database. The API-key created earlier is used using basic authentication over https.

The first thing you need to do, is to install the NuGet package:

PM> install-package mycouch  

The next step is to get connected (read more in the docs):

//If you have chars not allowed in the URL
//wrap in Uri.EscapeUriString, e.g.
//Uri.EscapeDataString("p@ssword")
var credentials = string.Format("{0}:{1}",  
  "theApiKey",
  "thePassword");

var url = string.Format(  
  "https://{0}@useraccount.cloudant.com/dbname/",
  credentials);```

There are some alternative ways, like a `MyCouchUriBuilder` that you can [read about in the documentation](https://github.com/danielwertheim/MyCouch/wiki/documentation#get-connected).

Now, lets continue and connect and to a simple `POST` and `GET` using plain JSON.

csharp using (var client = new Client(url))
{ var posted = client .Documents .PostAsync("{"msg": "MyCouch says hello to Cloudant!"}") .Result;

var requested = client .Documents .GetAsync(posted.Id);

Console.WriteLine(requested.Result.Content); }

Outputs this to the console:

json { "id":"6b041a2c782a8254fb66646ecd264371", "rev":"1-9091dd10ff42851a1a8ead3d1d3904ab", "msg":"MyCouch says hello to Cloudant!" }

The same using entities, would look like this:

csharp using (var client = new Client(url))
{ var msg = new Message { Text = "MyCouch says hello to Cloudant!" };

var posted = client .Entities .PostAsync(msg) .Result;

//If successful, msg.MessageId and msg.MessageRev //now has correct values, and we can use those. var requested = client .Entities .GetAsync(msg.MessageId);

Console.WriteLine(requested.Result.Entity.Text); }

public class Message
{ public string MessageId { get; set; } public string MessageRev { get; set; } public string Text { get; set; } }

Outputs this to the console:

none "MyCouch says hello to Cloudant!" ```

By default, the Entities API comes with some conventions, like mapping the MessageId to _id. You can read more about it in the documentation.

Depending on what kind of request you perform the response will look a bit different, but there are some basic members providing you with generated _id & _rev etc. as well as basic HTTP response status etc, which looks something like this:

mycouch-hello-cloudant-response

The future

I just released v0.11.0 of MyCouch, and are now planning on starting on some Cloudant specific features, distributed as a separate NuGet package. More on this will follow. But in short, I hope MyCouch can be made best buddy with Cloudant.

Cheers,

//Daniel

Developer that lives by the mantra "code is meant to be shared".

Comments