Permalink
Browse files

Added first (not very well tested) version of the code

  • Loading branch information...
1 parent fc3f151 commit 6a1b2d1575928d07f63590627b67359a26300af9 @ntoll ntoll committed Aug 21, 2009
Showing with 356 additions and 1 deletion.
  1. +1 −0 AUTHORS
  2. +217 −0 FluidDB/FluidConnector.cs
  3. +60 −0 FluidDB/FluidDB.csproj
  4. +20 −0 FluidDB/FluidDB.sln
  5. +36 −0 FluidDB/Properties/AssemblyInfo.cs
  6. +19 −0 LICENSE
  7. +3 −1 README
View
@@ -0,0 +1 @@
+Nicholas H.Tollervey (http://ntoll.org/contact)
View
@@ -0,0 +1,217 @@
+using System;
+using System.Collections.Generic;
+using System.Net;
+using System.Text;
+using System.Web;
+using System.IO;
+using System.Diagnostics;
+
+namespace FluidDB
+{
+ /// <summary>
+ /// Allowed HTTP methods
+ /// </summary>
+ public enum METHOD
+ {
+ POST,
+ GET,
+ PUT,
+ DELETE
+ }
+
+ /// <summary>
+ /// A base class defining the call methods for all the other FluidDB classes
+ /// </summary>
+ public class FluidConnector
+ {
+ /// <summary>
+ /// The URL for FluidDB
+ /// </summary>
+ public const string URL = "http://fluiddb.fluidinfo.com/";
+
+ /// <summary>
+ /// Use format=json for responses from GET and PUT requests
+ ///
+ /// (FluidDB defaults to raw payload)
+ /// </summary>
+ private bool alwaysUseJson = false;
+
+ /// <summary>
+ /// Use format=json for responses from GET and PUT requests
+ ///
+ /// (FluidDB defaults to raw payload)
+ /// </summary>
+ public bool AlwaysUseJson
+ {
+ get { return this.alwaysUseJson; }
+ set { this.alwaysUseJson = value; }
+ }
+
+ /* Used for authenticatation */
+ private string username = string.Empty;
+
+ private string password = string.Empty;
+
+ public string Username
+ {
+ get { return this.username; }
+ set { this.username = value; }
+ }
+
+ public string Password
+ {
+ get { return this.password; }
+ set { this.password = value; }
+ }
+
+ /// <summary>
+ /// Makes a call to FluidDB
+ /// </summary>
+ /// <param name="method">The type of HTTP method</param>
+ /// <param name="path">The path to call</param>
+ /// <returns>The raw result</returns>
+ public HttpWebResponse Call(METHOD m, string path)
+ {
+ return this.Call(m, path, string.Empty);
+ }
+
+ /// <summary>
+ /// Makes a call to FluidDB
+ /// </summary>
+ /// <param name="method">The type of HTTP method</param>
+ /// <param name="path">The path to call</param>
+ /// <param name="body">The body of the request</param>
+ /// <returns>The raw result</returns>
+ public HttpWebResponse Call(METHOD m, string path, string body)
+ {
+ return this.Call(m, path, body, new Dictionary<string, string>());
+ }
+
+ /// <summary>
+ /// Makes a call to FluidDB
+ /// </summary>
+ /// <param name="method">The type of HTTP method</param>
+ /// <param name="path">The path to call</param>
+ /// <param name="body">The body of the request</param>
+ /// <param name="args">Any further arguments to append to the URI</param>
+ /// <returns></returns>
+ public HttpWebResponse Call(METHOD m, string path, string body, Dictionary<string, string> args)
+ {
+ // Process the URI
+ StringBuilder URI = new StringBuilder();
+ URI.Append(URL);
+ URI.Append(path);
+ if (this.alwaysUseJson)
+ {
+ if (!args.ContainsKey("format"))
+ {
+ args.Add("format", "json");
+ }
+ }
+ if (args.Count > 0)
+ {
+ URI.Append("?");
+ List<string> argList = new List<string>();
+ foreach (string k in args.Keys)
+ {
+ argList.Add(k + "=" + HttpUtility.UrlEncode(args[k]));
+ }
+ URI.Append(string.Join("&", argList.ToArray()));
+ }
+ // Build the request
+ Uri requestUri = new Uri(URI.ToString(), true);
+ WebRequest request = WebRequest.Create(requestUri);
+ switch (m)
+ {
+ case METHOD.POST:
+ request.Method = "POST";
+ break;
+ case METHOD.GET:
+ request.Method = "GET";
+ break;
+ case METHOD.PUT:
+ request.Method = "PUT";
+ break;
+ case METHOD.DELETE:
+ request.Method = "DELETE";
+ break;
+ }
+
+ // Make sure the headers are correct
+ ((HttpWebRequest)request).UserAgent = ".NET FluidDB Client";
+ ((HttpWebRequest)request).Accept = "application/json";
+ if (!(this.password == string.Empty & this.username == string.Empty))
+ {
+ request.Credentials = new NetworkCredential(this.username, this.password);
+ }
+ if (body == string.Empty)
+ {
+ request.ContentType = "text/plain";
+ }
+ else
+ {
+ ASCIIEncoding e = new ASCIIEncoding();
+ Byte[] byteArray = e.GetBytes(body);
+ request.ContentType = "application/json";
+ request.ContentLength = byteArray.Length;
+ Stream bodyStream = request.GetRequestStream();
+ bodyStream.Write(byteArray, 0, byteArray.Length);
+ bodyStream.Close();
+ }
+ // Call FluidDB
+ HttpWebResponse response = (HttpWebResponse)request.GetResponse();
+ return response;
+ }
+
+ /// <summary>
+ /// Utility method: given a WebResponse object this method will return the
+ /// "result" from FluidDB contained therein.
+ /// </summary>
+ /// <param name="response">The response from FluidDB</param>
+ /// <returns>The "result" from FluidDB</returns>
+ public string GetResult(WebResponse response)
+ {
+ Stream answer = response.GetResponseStream();
+ StreamReader sr = new StreamReader(answer);
+ return sr.ReadToEnd();
+ }
+
+ /// <summary>
+ /// In the absence of bolting on nUnit and in order to keep the dependencies to a
+ /// minimum I'm testing the class with the following method. Pass in your username
+ /// and password in order to be able to post
+ /// </summary>
+ /// <param name="username">Your username</param>
+ /// <param name="password">Your password</param>
+ public void SelfTest(string username, string password)
+ {
+ // Return a list of objects that have the tag “username” from the “fluiddb/users” namespace
+ Dictionary<string, string> args = new Dictionary<string, string>();
+ args.Add("query", "has fluiddb/users/username");
+ HttpWebResponse result = this.Call(METHOD.GET, "objects", "", args);
+ Debug.Assert((HttpStatusCode.OK == result.StatusCode), "Didn't return 200");
+ Debug.Assert((this.GetResult(result).Length > 0), "Didn't return anything!");
+
+ // Return an object where the tag “username” from the “fluiddb/users” namespace has the value “ntoll”
+ args["query"] = "fluiddb/users/username = \"ntoll\"";
+ result = this.Call(METHOD.GET, "objects", "", args);
+ Debug.Assert((HttpStatusCode.OK == result.StatusCode), "Didn't return 200");
+ Debug.Assert((this.GetResult(result).Contains("'ids':")), "Didn't return anything valid!");
+
+ // Find out about a specific object
+ result = this.Call(METHOD.GET, "/objects/5873e7cc-2a4a-44f7-a00e-7cebf92a7332", "{'showAbout': True}");
+ Debug.Assert((HttpStatusCode.OK == result.StatusCode), "Didn't return 200");
+
+ // Get the value of the tag “fluiddb/users/username” from the object with the uuid “5873e7cc-2a4a-44f7-a00e-7cebf92a7332”
+ result = this.Call(METHOD.GET, "/objects/5873e7cc-2a4a-44f7-a00e-7cebf92a7332/fluiddb/users/name");
+ Debug.Assert((HttpStatusCode.OK == result.StatusCode), "Didn't return 200");
+ Debug.Assert((this.GetResult(result) == "ntoll"), "Didn't return expected result!");
+
+ // Get the same result as json
+ this.AlwaysUseJson = true;
+ result = this.Call(METHOD.GET, "/objects/5873e7cc-2a4a-44f7-a00e-7cebf92a7332/fluiddb/users/name");
+ Debug.Assert((HttpStatusCode.OK == result.StatusCode), "Didn't return 200");
+ Debug.Assert((this.GetResult(result) == "{'value': 'ntoll'}"), "Didn't return expected result!");
+ }
+ }
+}
View
@@ -0,0 +1,60 @@
+<?xml version="1.0" encoding="utf-8"?>
+<Project ToolsVersion="3.5" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
+ <PropertyGroup>
+ <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
+ <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
+ <ProductVersion>9.0.30729</ProductVersion>
+ <SchemaVersion>2.0</SchemaVersion>
+ <ProjectGuid>{B9F01CB5-6E4C-440F-B0A8-EC7E151C8735}</ProjectGuid>
+ <OutputType>Library</OutputType>
+ <AppDesignerFolder>Properties</AppDesignerFolder>
+ <RootNamespace>FluidDB</RootNamespace>
+ <AssemblyName>FluidDB</AssemblyName>
+ <TargetFrameworkVersion>v3.5</TargetFrameworkVersion>
+ <FileAlignment>512</FileAlignment>
+ </PropertyGroup>
+ <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
+ <DebugSymbols>true</DebugSymbols>
+ <DebugType>full</DebugType>
+ <Optimize>false</Optimize>
+ <OutputPath>bin\Debug\</OutputPath>
+ <DefineConstants>DEBUG;TRACE</DefineConstants>
+ <ErrorReport>prompt</ErrorReport>
+ <WarningLevel>4</WarningLevel>
+ </PropertyGroup>
+ <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
+ <DebugType>pdbonly</DebugType>
+ <Optimize>true</Optimize>
+ <OutputPath>bin\Release\</OutputPath>
+ <DefineConstants>TRACE</DefineConstants>
+ <ErrorReport>prompt</ErrorReport>
+ <WarningLevel>4</WarningLevel>
+ </PropertyGroup>
+ <ItemGroup>
+ <Reference Include="System" />
+ <Reference Include="System.Core">
+ <RequiredTargetFramework>3.5</RequiredTargetFramework>
+ </Reference>
+ <Reference Include="System.Web" />
+ <Reference Include="System.Xml.Linq">
+ <RequiredTargetFramework>3.5</RequiredTargetFramework>
+ </Reference>
+ <Reference Include="System.Data.DataSetExtensions">
+ <RequiredTargetFramework>3.5</RequiredTargetFramework>
+ </Reference>
+ <Reference Include="System.Data" />
+ <Reference Include="System.Xml" />
+ </ItemGroup>
+ <ItemGroup>
+ <Compile Include="FluidConnector.cs" />
+ <Compile Include="Properties\AssemblyInfo.cs" />
+ </ItemGroup>
+ <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
+ <!-- To modify your build process, add your task inside one of the targets below and uncomment it.
+ Other similar extension points exist, see Microsoft.Common.targets.
+ <Target Name="BeforeBuild">
+ </Target>
+ <Target Name="AfterBuild">
+ </Target>
+ -->
+</Project>
View
@@ -0,0 +1,20 @@
+
+Microsoft Visual Studio Solution File, Format Version 10.00
+# Visual C# Express 2008
+Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FluidDB", "FluidDB.csproj", "{B9F01CB5-6E4C-440F-B0A8-EC7E151C8735}"
+EndProject
+Global
+ GlobalSection(SolutionConfigurationPlatforms) = preSolution
+ Debug|Any CPU = Debug|Any CPU
+ Release|Any CPU = Release|Any CPU
+ EndGlobalSection
+ GlobalSection(ProjectConfigurationPlatforms) = postSolution
+ {B9F01CB5-6E4C-440F-B0A8-EC7E151C8735}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
+ {B9F01CB5-6E4C-440F-B0A8-EC7E151C8735}.Debug|Any CPU.Build.0 = Debug|Any CPU
+ {B9F01CB5-6E4C-440F-B0A8-EC7E151C8735}.Release|Any CPU.ActiveCfg = Release|Any CPU
+ {B9F01CB5-6E4C-440F-B0A8-EC7E151C8735}.Release|Any CPU.Build.0 = Release|Any CPU
+ EndGlobalSection
+ GlobalSection(SolutionProperties) = preSolution
+ HideSolutionNode = FALSE
+ EndGlobalSection
+EndGlobal
@@ -0,0 +1,36 @@
+using System.Reflection;
+using System.Runtime.CompilerServices;
+using System.Runtime.InteropServices;
+
+// General Information about an assembly is controlled through the following
+// set of attributes. Change these attribute values to modify the information
+// associated with an assembly.
+[assembly: AssemblyTitle("FluidDB")]
+[assembly: AssemblyDescription("A library for interacting with FluidDB")]
+[assembly: AssemblyConfiguration("")]
+[assembly: AssemblyCompany("")]
+[assembly: AssemblyProduct("FluidDB")]
+[assembly: AssemblyCopyright("Copyright © 2009 Nicholas H.Tollervey")]
+[assembly: AssemblyTrademark("")]
+[assembly: AssemblyCulture("")]
+
+// Setting ComVisible to false makes the types in this assembly not visible
+// to COM components. If you need to access a type in this assembly from
+// COM, set the ComVisible attribute to true on that type.
+[assembly: ComVisible(false)]
+
+// The following GUID is for the ID of the typelib if this project is exposed to COM
+[assembly: Guid("eefe1e20-162f-4884-9bd0-eef126dc808a")]
+
+// Version information for an assembly consists of the following four values:
+//
+// Major Version
+// Minor Version
+// Build Number
+// Revision
+//
+// You can specify all the values or you can default the Build and Revision Numbers
+// by using the '*' as shown below:
+// [assembly: AssemblyVersion("1.0.*")]
+[assembly: AssemblyVersion("1.0.0.0")]
+[assembly: AssemblyFileVersion("1.0.0.0")]
View
19 LICENSE
@@ -0,0 +1,19 @@
+Copyright (c) 2009 Nicholas H.Tollervey
+
+Permission is hereby granted, free of charge, to any person obtaining a
+copy of this software and associated documentation files (the "Software"),
+to deal in the Software without restriction, including without limitation
+the rights to use, copy, modify, merge, publish, distribute, sublicense,
+and/or sell copies of the Software, and to permit persons to whom the
+Software is furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in
+all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
+THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+DEALINGS IN THE SOFTWARE.
View
4 README
@@ -1 +1,3 @@
-The beginnings of an API for interaction with FluidDB for .NET (written in C#)
+The beginnings of an API for interaction with FluidDB for .NET (written in C#).
+
+Not fully tested and definitely a work in progress.

0 comments on commit 6a1b2d1

Please sign in to comment.