Skip to content

Instantly share code, notes, and snippets.

@janl

janl/Program.cs Secret

Created February 2, 2016 14:41
Show Gist options
  • Save janl/638d8aa39841df829cd8 to your computer and use it in GitHub Desktop.
Save janl/638d8aa39841df829cd8 to your computer and use it in GitHub Desktop.
using System;
using System.Net;
using System.Threading.Tasks;
namespace ConsoleApplication2
{
class Program
{
const string Id = "MyDummyEntry122";
const string Doc = "{\"_id\":\"" + Id + "\", \"title\":\"test\"}";
const string DatabaseUri = "http://localhost:5984/viz_data_hub_1_1_0/";
const string DocUri = DatabaseUri + Id;
static void Main()
{
while (true)
{
var rev = Get();
Delete(rev, true);
AsyncPut();
}
}
static string Send(string uri, string method = "GET", string body = "", bool async = false)
{
var task = Task.Factory.StartNew(() =>
{
var client = new WebClient();
try
{
Console.WriteLine(DateTime.Now.ToString("O") + ": " + method + " " + uri);
string response;
if (method == "GET")
{
response = client.DownloadString(uri + "?deleted_conflicts=true");
if(response.Contains("_deleted_conflicts")) {
Console.WriteLine(DateTime.Now.ToString("O") + ": " + response);
Console.WriteLine(DateTime.Now.ToString("O") + ": " + method + " " + uri + ": got _deleted_conflicts, exiting");
System.Environment.Exit(0);
}
} else
{
client.Headers["Content-Type"] = "application/json";
response = client.UploadString(uri, method, body);
}
Console.WriteLine(DateTime.Now.ToString("O") + ": " + response);
return client.ResponseHeaders["ETag"].Replace("\"", "");
}
catch (Exception exception)
{
Console.WriteLine(DateTime.Now.ToString("O") + ": " + exception.Message);
return null;
}
});
return async ? null : task.Result;
}
static string Get(bool async = false)
{
return Send(DocUri, "GET", "", async);
}
static string AsyncGet()
{
return Send(DocUri, "GET", "", true);
}
static string Post(bool async = false)
{
return Send(DatabaseUri, "POST", Doc, async);
}
static string AsyncPost()
{
return Send(DatabaseUri, "POST", Doc, true);
}
static string Put(string rev = "", bool async = false)
{
var uri = DocUri;
if (!string.IsNullOrEmpty(rev))
uri += "?rev=" + rev;
return Send(uri, "PUT", Doc, async);
}
static string AsyncPut()
{
return Send(DocUri, "PUT", Doc, true);
}
static string Delete(string rev = "", bool async = false)
{
var uri = DocUri;
if (!string.IsNullOrEmpty(rev))
uri += "?rev=" + rev;
return Send(uri, "DELETE", "", async);
}
static string AsyncDelete()
{
return Send(DocUri, "DELETE", "", true);
}
void PutAndDelete()
{
var client = new WebClient();
try
{
string response;
try
{
Console.WriteLine(DateTime.Now.ToString("O") + ": creating...");
response = client.UploadString("http://localhost:5984/viz_data_hub_1_1_0/" + Id, "PUT", "{\"title\":\"test\"}");
Console.WriteLine(DateTime.Now.ToString("O") + ": " + response);
}
catch (Exception exception)
{
Console.WriteLine(DateTime.Now.ToString("O") + ": " + exception.Message);
Console.WriteLine(DateTime.Now.ToString("O") + ": retrieving...");
client.DownloadString("http://localhost:5984/viz_data_hub_1_1_0/" + Id);
Console.WriteLine(DateTime.Now.ToString("O") + ": received");
}
var rev = client.ResponseHeaders["ETag"].Replace("\"", "");
var uri = "http://localhost:5984/viz_data_hub_1_1_0/" + Id + "?rev=" + rev;
Console.WriteLine(DateTime.Now.ToString("O") + ": deleting...");
response = client.UploadString(uri, "DELETE", "");
Console.WriteLine(DateTime.Now.ToString("O") + ": " + response);
}
catch (Exception exception)
{
Console.WriteLine(DateTime.Now.ToString("O") + ": " + exception.Message);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment