First crack the API

This commit is contained in:
Mark McDowall 2012-11-03 18:39:29 -07:00
parent 64468cd9ed
commit b366f8fadc
13 changed files with 139 additions and 24 deletions

View File

@ -27,8 +27,10 @@ namespace NzbDrone.Api
SetConfig(new EndpointHostConfig { ServiceStackHandlerFactoryPath = "api" }); SetConfig(new EndpointHostConfig { ServiceStackHandlerFactoryPath = "api" });
Routes Routes
.Add<QualityProfileRequest>("{ApiKey}/qualityprofiles") .Add<QualityProfileModel>("/qualityprofiles")
.Add<QualityProfileRequest>("{ApiKey}/qualityprofiles/{Id}"); .Add<QualityProfileModel>("/qualityprofiles/{Id}");
Bootstrapper.Initialize();
} }
} }
} }

View File

@ -0,0 +1,22 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using AutoMapper;
using NzbDrone.Api.QualityProfiles;
using NzbDrone.Core.Repository.Quality;
namespace NzbDrone.Api
{
public static class Bootstrapper
{
public static void Initialize()
{
Mapper.CreateMap<QualityProfile, QualityProfileModel>()
.ForMember(dest => dest.Id, opt => opt.MapFrom(src => src.QualityProfileId));
Mapper.CreateMap<QualityProfileModel, QualityProfile>()
.ForMember(dest => dest.QualityProfileId, opt => opt.MapFrom(src => src.Id));
}
}
}

View File

@ -4,6 +4,7 @@ using System.Linq;
using System.Text; using System.Text;
using Ninject; using Ninject;
using NzbDrone.Api.Exceptions; using NzbDrone.Api.Exceptions;
using NzbDrone.Api.Helpers;
using NzbDrone.Core.Providers.Core; using NzbDrone.Core.Providers.Core;
using ServiceStack.ServiceHost; using ServiceStack.ServiceHost;
using ServiceStack.ServiceInterface; using ServiceStack.ServiceInterface;
@ -21,10 +22,10 @@ namespace NzbDrone.Api.Filters
public void RequestFilter(IHttpRequest req, IHttpResponse res, object requestDto) public void RequestFilter(IHttpRequest req, IHttpResponse res, object requestDto)
{ {
//Verify the API Key here //Verify the API Key here
var apikey = ((IApiRequest)requestDto).ApiKey; var apikey = req.GetApiKey();
if (String.IsNullOrWhiteSpace(apikey)) //if (String.IsNullOrWhiteSpace(apikey))
throw new InvalidApiKeyException(); //throw new InvalidApiKeyException();
} }
public IHasRequestFilter Copy() public IHasRequestFilter Copy()

View File

@ -0,0 +1,28 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ServiceStack.Common.Web;
using ServiceStack.ServiceHost;
namespace NzbDrone.Api.Helpers
{
public static class HttpRequestExtensions
{
public static string GetApiKey(this IHttpRequest httpReq)
{
var auth = httpReq.Headers[HttpHeaders.Authorization];
if (auth == null) return null;
var split = auth.Split(' ');
if (split.Count() != 2)
return null;
if (!split[0].Equals("APIKEY", StringComparison.InvariantCultureIgnoreCase))
return null;
return split[1];
}
}
}

View File

@ -50,6 +50,9 @@
<CodeAnalysisRuleSet>MinimumRecommendedRules.ruleset</CodeAnalysisRuleSet> <CodeAnalysisRuleSet>MinimumRecommendedRules.ruleset</CodeAnalysisRuleSet>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<Reference Include="AutoMapper">
<HintPath>..\packages\AutoMapper.2.2.0\lib\net40\AutoMapper.dll</HintPath>
</Reference>
<Reference Include="Ninject, Version=3.0.0.0, Culture=neutral, PublicKeyToken=c7192dc5380945e7, processorArchitecture=MSIL"> <Reference Include="Ninject, Version=3.0.0.0, Culture=neutral, PublicKeyToken=c7192dc5380945e7, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion> <SpecificVersion>False</SpecificVersion>
<HintPath>..\packages\Ninject.3.0.1.10\lib\net40\Ninject.dll</HintPath> <HintPath>..\packages\Ninject.3.0.1.10\lib\net40\Ninject.dll</HintPath>
@ -91,11 +94,14 @@
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Compile Include="AppHost.cs" /> <Compile Include="AppHost.cs" />
<Compile Include="Bootstrapper.cs" />
<Compile Include="Exceptions\InvalidApiKeyException.cs" /> <Compile Include="Exceptions\InvalidApiKeyException.cs" />
<Compile Include="Filters\ValidApiRequestAttribute.cs" /> <Compile Include="Filters\ValidApiRequestAttribute.cs" />
<Compile Include="Helpers\HttpRequestExtensions.cs" />
<Compile Include="IApiRequest.cs" /> <Compile Include="IApiRequest.cs" />
<Compile Include="QualityProfiles\QualityProfileRequest.cs" /> <Compile Include="QualityProfiles\QualityProfileRequest.cs" />
<Compile Include="Properties\AssemblyInfo.cs" /> <Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="QualityProfiles\QualityProfileModel.cs" />
<Compile Include="QualityProfiles\QualityProfileService.cs" /> <Compile Include="QualityProfiles\QualityProfileService.cs" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>

View File

@ -0,0 +1,16 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NzbDrone.Core.Repository.Quality;
namespace NzbDrone.Api.QualityProfiles
{
public class QualityProfileModel
{
public Int32 Id { get; set; }
public String Name { get; set; }
public QualityTypes Cutoff { get; set; }
public List<QualityTypes> Allowed { get; set; }
}
}

View File

@ -1,13 +1,16 @@
using System.Linq; using System.Collections.Generic;
using System.Linq;
using AutoMapper;
using Ninject; using Ninject;
using NzbDrone.Api.Filters; using NzbDrone.Api.Filters;
using NzbDrone.Core.Providers; using NzbDrone.Core.Providers;
using NzbDrone.Core.Repository.Quality;
using ServiceStack.ServiceInterface; using ServiceStack.ServiceInterface;
namespace NzbDrone.Api.QualityProfiles namespace NzbDrone.Api.QualityProfiles
{ {
[ValidApiRequest] [ValidApiRequest]
public class QualityProfileService : RestServiceBase<QualityProfileRequest> public class QualityProfileService : RestServiceBase<QualityProfileModel>
{ {
private readonly QualityProvider _qualityProvider; private readonly QualityProvider _qualityProvider;
@ -21,32 +24,39 @@ namespace NzbDrone.Api.QualityProfiles
{ {
} }
public override object OnGet(QualityProfileRequest request) public override object OnGet(QualityProfileModel request)
{ {
if (request.Id == 0) if (request.Id == 0)
{ {
var profiles = _qualityProvider.All(); var profiles = _qualityProvider.All();
return new { Profiles = profiles }; return Mapper.Map<IEnumerable<QualityProfile>, IEnumerable<QualityProfileModel>>(profiles);
} }
var profile = _qualityProvider.Get(request.Id); var profile = _qualityProvider.Get(request.Id);
return profile; return Mapper.Map<QualityProfile, QualityProfileModel>(profile);
} }
//public override object OnPost(Todo todo) public override object OnPost(QualityProfileModel data)
//{ {
// return Repository.Store(todo); var profile = Mapper.Map<QualityProfileModel, QualityProfile>(data);
//} _qualityProvider.Update(profile);
//public override object OnPut(Todo todo) return data;
//{ }
// return Repository.Store(todo);
//}
//public override object OnDelete(Todo request) public override object OnPut(QualityProfileModel data)
//{ {
// Repository.DeleteById(request.Id); var profile = Mapper.Map<QualityProfileModel, QualityProfile>(data);
// return null; data.Id = _qualityProvider.Add(profile);
//}
return data;
}
public override object OnDelete(QualityProfileModel data)
{
_qualityProvider.Delete(data.Id);
return "ok";
}
} }
} }

View File

@ -1,5 +1,6 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<packages> <packages>
<package id="AutoMapper" version="2.2.0" targetFramework="net40" />
<package id="Ninject" version="3.0.1.10" targetFramework="net40" /> <package id="Ninject" version="3.0.1.10" targetFramework="net40" />
<package id="ServiceStack" version="3.9.25" targetFramework="net40" /> <package id="ServiceStack" version="3.9.25" targetFramework="net40" />
<package id="ServiceStack.Common" version="3.9.25" targetFramework="net40" /> <package id="ServiceStack.Common" version="3.9.25" targetFramework="net40" />

View File

@ -203,7 +203,6 @@
<HintPath>..\packages\EntityFramework.SqlServerCompact.4.1.8482.2\lib\System.Data.SqlServerCe.Entity.dll</HintPath> <HintPath>..\packages\EntityFramework.SqlServerCompact.4.1.8482.2\lib\System.Data.SqlServerCe.Entity.dll</HintPath>
</Reference> </Reference>
<Reference Include="System.Drawing" /> <Reference Include="System.Drawing" />
<Reference Include="System.Runtime.Serialization" />
<Reference Include="System.ServiceModel" /> <Reference Include="System.ServiceModel" />
<Reference Include="System.Transactions" /> <Reference Include="System.Transactions" />
<Reference Include="System.Web" /> <Reference Include="System.Web" />

Binary file not shown.

View File

@ -0,0 +1,15 @@
<?xml version="1.0"?>
<package xmlns="http://schemas.microsoft.com/packaging/2010/07/nuspec.xsd">
<metadata>
<id>AutoMapper</id>
<version>2.2.0</version>
<authors>Jimmy Bogard</authors>
<owners>Jimmy Bogard</owners>
<licenseUrl>https://github.com/AutoMapper/AutoMapper/blob/master/LICENSE.txt</licenseUrl>
<projectUrl>http://automapper.org/</projectUrl>
<iconUrl>https://s3.amazonaws.com/automapper/icon.png</iconUrl>
<requireLicenseAcceptance>false</requireLicenseAcceptance>
<description>A convention-based object-object mapper. AutoMapper uses a fluent configuration API to define an object-object mapping strategy. AutoMapper uses a convention-based matching algorithm to match up source to destination values. Currently, AutoMapper is geared towards model projection scenarios to flatten complex object models to DTOs and other simple objects, whose design is better suited for serialization, communication, messaging, or simply an anti-corruption layer between the domain and application layer.</description>
<summary>A convention-based object-object mapper</summary>
</metadata>
</package>

Binary file not shown.

View File

@ -0,0 +1,15 @@
<?xml version="1.0"?>
<doc>
<assembly>
<name>AutoMapper</name>
</assembly>
<members>
<member name="T:AutoMapper.MappingEngine.ConversionVisitor">
<summary>
This expression visitor will replace an input parameter by another one
see http://stackoverflow.com/questions/4601844/expression-tree-copy-or-convert
</summary>
</member>
</members>
</doc>