Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Flickr REST API wrapper.

  • Loading branch information...
commit 7837a87fd5560631de9a6fb8e0dff4d834412b30 0 parents
@Inferis authored
8 .gitignore
@@ -0,0 +1,8 @@
+
+thumbs.db
+*.user
+*.suo
+*.cache
+_ReSharper.*
+[Bb]in
+[Oo]bj
85 Source/Inferis.Flickr/FlickrApi.cs
@@ -0,0 +1,85 @@
+using System;
+using System.Collections.Generic;
+using System.Dynamic;
+using System.IO;
+using System.Linq;
+using System.Net;
+using System.Xml;
+using System.Xml.Linq;
+
+namespace Inferis.Flickr {
+ public class FlickrApi : DynamicObject {
+ private readonly string apikey;
+
+ protected FlickrApi(string apikey)
+ {
+ this.apikey = apikey;
+ }
+
+ public static dynamic WithKey(string key)
+ {
+ return new FlickrApi(key);
+ }
+
+ public override bool TryGetMember(GetMemberBinder binder, out object result)
+ {
+ result = new Executor(string.Concat(binder.Name.Substring(0, 1).ToLower(), binder.Name.Substring(1)), InvokeCall);
+ return true;
+ }
+
+ private dynamic InvokeCall(string path, IDictionary<string, object> args)
+ {
+ args["apiKey"] = apikey;
+
+ var urlArgs = string.Join("&", args.Select(a => string.Concat(Underscorize(a.Key), "=", (a.Value ?? "").ToString())));
+ var url = string.Format("http://api.flickr.com/services/rest/?method=flickr.{0}&{1}", path, urlArgs);
+
+ var response = WebRequest.Create(url).GetResponse();
+ using (var reader = new StreamReader(response.GetResponseStream())) {
+ return XDocument.Parse(reader.ReadToEnd());
+ }
+ }
+
+ private string Underscorize(string key)
+ {
+ return new string(key.SelectMany(c => char.IsUpper(c) ? new[] { '_', char.ToLower(c) } : new[] { c }).ToArray());
+ }
+
+ private class Executor : DynamicObject {
+ private string path;
+ private readonly Func<string, IDictionary<string, object>, dynamic> invoke;
+
+ public Executor(string part, Func<string, IDictionary<string, object>, dynamic> invoke)
+ {
+ path = part;
+ this.invoke = invoke;
+ }
+
+ public override bool TryGetMember(GetMemberBinder binder, out object result)
+ {
+ result = new Executor(string.Concat(path, ".", binder.Name.Substring(0, 1).ToLower(), binder.Name.Substring(1)), invoke);
+ return true;
+ }
+
+ public override bool TryInvokeMember(InvokeMemberBinder binder, object[] args, out object result)
+ {
+ if (binder.CallInfo.ArgumentCount == 0 || binder.CallInfo.ArgumentCount != args.Length)
+ throw new InvalidOperationException("You must used named parameters for the flickr api.");
+
+ var invokeArgs = Enumerable.Range(0, binder.CallInfo.ArgumentCount)
+ .ToDictionary(i => binder.CallInfo.ArgumentNames[i], i => args[i]);
+
+ result = invoke(string.Concat(path, ".", binder.Name.Substring(0, 1).ToLower(), binder.Name.Substring(1)), invokeArgs);
+ return true;
+ }
+ }
+ }
+
+ public class XmlProxy : DynamicObject
+ {
+ public XmlProxy(string xml)
+ {
+
+ }
+ }
+}
54 Source/Inferis.Flickr/Inferis.Flickr.csproj
@@ -0,0 +1,54 @@
+<?xml version="1.0" encoding="utf-8"?>
+<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
+ <PropertyGroup>
+ <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
+ <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
+ <ProductVersion>8.0.30703</ProductVersion>
+ <SchemaVersion>2.0</SchemaVersion>
+ <ProjectGuid>{E83A8535-4A47-4D7A-A798-A1C1910011AE}</ProjectGuid>
+ <OutputType>Library</OutputType>
+ <AppDesignerFolder>Properties</AppDesignerFolder>
+ <RootNamespace>Inferis.Flickr</RootNamespace>
+ <AssemblyName>Inferis.Flickr</AssemblyName>
+ <TargetFrameworkVersion>v4.0</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" />
+ <Reference Include="System.Xml.Linq" />
+ <Reference Include="System.Data.DataSetExtensions" />
+ <Reference Include="Microsoft.CSharp" />
+ <Reference Include="System.Data" />
+ <Reference Include="System.Xml" />
+ </ItemGroup>
+ <ItemGroup>
+ <Compile Include="FlickrApi.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>
36 Source/Inferis.Flickr/Properties/AssemblyInfo.cs
@@ -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("Inferis.Flickr")]
+[assembly: AssemblyDescription("")]
+[assembly: AssemblyConfiguration("")]
+[assembly: AssemblyCompany("Microsoft")]
+[assembly: AssemblyProduct("Inferis.Flickr")]
+[assembly: AssemblyCopyright("Copyright © Microsoft 2010")]
+[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("b108e235-642a-48bd-a556-c8bdbec67bac")]
+
+// 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")]
Please sign in to comment.
Something went wrong with that request. Please try again.