Skip to content

Commit

Permalink
[Closes #17, #18] Update to net452, async/await, fix search bug
Browse files Browse the repository at this point in the history
  • Loading branch information
kamranayub committed Aug 7, 2015
1 parent a4e6de2 commit 16717da
Show file tree
Hide file tree
Showing 22 changed files with 1,467 additions and 81 deletions.
Binary file modified .nuget/NuGet.exe
Binary file not shown.
1,030 changes: 1,030 additions & 0 deletions .vs/config/applicationhost.config

Large diffs are not rendered by default.

14 changes: 13 additions & 1 deletion GiantBomb.Api.Tests/Core.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using GiantBomb.Api.Tests.Support;
using NUnit.Framework;
using RestSharp;
Expand All @@ -14,13 +15,24 @@ public class Core : TestBase
[Test]
public void giantbomb_should_respond_to_request()
{
var request = new RestRequest();
var request = new RestRequest();
request.Resource = "games";

var result = _client.Execute(request);

Assert.IsNotNull(result.Content);
}

[Test]
public async Task giantbomb_should_respond_to_request_async()
{
var request = new RestRequest();
request.Resource = "games";

var result = await _client.ExecuteAsync(request);

Assert.IsNotNull(result.Content);
}

}
}
19 changes: 10 additions & 9 deletions GiantBomb.Api.Tests/GiantBomb.Api.Tests.csproj
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Project ToolsVersion="12.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
Expand All @@ -10,10 +10,11 @@
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>GiantBomb.Api.Tests</RootNamespace>
<AssemblyName>GiantBomb.Api.Tests</AssemblyName>
<TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
<TargetFrameworkVersion>v4.5.2</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<SolutionDir Condition="$(SolutionDir) == '' Or $(SolutionDir) == '*Undefined*'">..\..\GiantBomb-CSharp\</SolutionDir>
<RestorePackages>true</RestorePackages>
<TargetFrameworkProfile />
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
Expand All @@ -23,6 +24,7 @@
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<Prefer32Bit>false</Prefer32Bit>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
Expand All @@ -31,15 +33,16 @@
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<Prefer32Bit>false</Prefer32Bit>
</PropertyGroup>
<ItemGroup>
<Reference Include="nunit.framework, Version=2.6.4.14350, Culture=neutral, PublicKeyToken=96d09a1eb7f44a77, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\packages\NUnit.2.6.4\lib\nunit.framework.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="RestSharp, Version=105.0.1.0, Culture=neutral, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\packages\RestSharp.105.0.1\lib\net4\RestSharp.dll</HintPath>
<Reference Include="RestSharp, Version=105.1.0.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\RestSharp.105.1.0\lib\net452\RestSharp.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
Expand All @@ -62,12 +65,10 @@
</ItemGroup>
<ItemGroup>
<None Include="app.config" />
<None Include="packages.config">
<SubType>Designer</SubType>
</None>
<Content Include="Support\api_token.private">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
<None Include="packages.config" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\GiantBomb.Api\GiantBomb.Api.csproj">
Expand Down
2 changes: 1 addition & 1 deletion GiantBomb.Api.Tests/app.config
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@
</dependentAssembly>
</assemblyBinding>
</runtime>
</configuration>
<startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2"/></startup></configuration>
4 changes: 2 additions & 2 deletions GiantBomb.Api.Tests/packages.config
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="NUnit" version="2.6.4" targetFramework="net40" />
<package id="RestSharp" version="105.0.1" targetFramework="net40" />
<package id="NUnit" version="2.6.4" targetFramework="net452" />
<package id="RestSharp" version="105.1.0" targetFramework="net452" />
</packages>
94 changes: 71 additions & 23 deletions GiantBomb.Api/Core.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Net;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using GiantBomb.Api.Model;
using RestSharp;

Expand All @@ -19,7 +21,7 @@ public partial class GiantBombRestClient : IGiantBombRestClient {
/// <summary>
/// Your GiantBomb API token
/// </summary>
private string ApiKey { get; set; }
private string ApiKey { get; set; }

/// <summary>
/// Create a new Rest client with your API token and custom base URL
Expand All @@ -32,7 +34,7 @@ public partial class GiantBombRestClient : IGiantBombRestClient {

var assembly = Assembly.GetExecutingAssembly();
var version = System.Diagnostics.FileVersionInfo.GetVersionInfo(assembly.Location).ProductVersion;

_client = new RestClient
{
UserAgent = "giantbomb-csharp/" + version,
Expand All @@ -58,17 +60,48 @@ public GiantBombRestClient(string apiToken)
/// </summary>
/// <typeparam name="T">The type of object to create and populate with the returned data.</typeparam>
/// <param name="request">The RestRequest to execute (will use client credentials)</param>
public virtual T Execute<T>(RestRequest request) where T : new() {
var response = _client.Execute<T>(request);
public virtual T Execute<T>(RestRequest request) where T : new()
{
return ExecuteAsync<T>(request).Result;
}

/// <summary>
/// Execute a manual REST request (async)
/// </summary>
/// <typeparam name="T">The type of object to create and populate with the returned data.</typeparam>
/// <param name="request">The RestRequest to execute (will use client credentials)</param>
public virtual async Task<T> ExecuteAsync<T>(RestRequest request) where T : new()
{
var response = await _client.ExecuteTaskAsync<T>(request);

if (response.Data == null)
{
throw new GiantBombHttpException(response.Content);
}
else if (response.StatusCode != HttpStatusCode.OK)
{
throw new GiantBombHttpException(response.ErrorMessage + ": " + response.Content);
}

return response.Data;
}

/// <summary>
/// Execute a manual REST request
/// </summary>
/// <param name="request">The RestRequest to execute (will use client credentials)</param>
public virtual IRestResponse Execute(RestRequest request) {
return _client.Execute(request);
public virtual IRestResponse Execute(RestRequest request)
{
return ExecuteAsync(request).Result;
}

/// <summary>
/// Execute a manual REST request (async)
/// </summary>
/// <param name="request">The RestRequest to execute (will use client credentials)</param>
public virtual async Task<IRestResponse> ExecuteAsync(RestRequest request)
{
return await _client.ExecuteTaskAsync(request);
}
#endif

Expand Down Expand Up @@ -124,25 +157,12 @@ private string BuildKeyValueListForUrl(IEnumerable<KeyValuePair<string, SortDire
return BuildKeyValueListForUrl(sortDictionary);
}

public virtual IEnumerable<TResult> GetListResource<TResult>(string resource, int page = 1, int pageSize = GiantBombBase.DefaultLimit, string[] fieldList = null, IDictionary<string, SortDirection> sortOptions = null, IDictionary<string, object> filterOptions = null) where TResult : new() {
var request = GetListResource(resource, page, pageSize, fieldList, sortOptions, filterOptions);
var results = Execute<GiantBombResults<TResult>>(request);

if (results != null && results.StatusCode == GiantBombBase.StatusOk)
return results.Results;

if (results != null && results.StatusCode != GiantBombBase.StatusOk)
throw new GiantBombApiException(results.StatusCode, results.Error);

return null;
}

public virtual RestRequest GetSingleResource(string resource, int resourceId, int id, string[] fieldList = null) {
var request = new RestRequest {
var request = new RestRequest {
Resource = resource + "/{ResourceId}-{Id}/",
DateFormat = "yyyy-MM-dd HH:mm:ss"
};

request.AddUrlSegment("ResourceId", resourceId.ToString(CultureInfo.InvariantCulture));
request.AddUrlSegment("Id", id.ToString(CultureInfo.InvariantCulture));

Expand All @@ -152,9 +172,37 @@ private string BuildKeyValueListForUrl(IEnumerable<KeyValuePair<string, SortDire
return request;
}

public virtual TResult GetSingleResource<TResult>(string resource, int resourceId, int id, string[] fieldList = null) where TResult : class, new() {
public virtual IEnumerable<TResult> GetListResource<TResult>(string resource, int page = 1,
int pageSize = GiantBombBase.DefaultLimit, string[] fieldList = null,
IDictionary<string, SortDirection> sortOptions = null, IDictionary<string, object> filterOptions = null)
where TResult : new()
{
return GetListResourceAsync<TResult>(resource, page, pageSize, fieldList, sortOptions, filterOptions).Result;
}

public virtual async Task<IEnumerable<TResult>> GetListResourceAsync<TResult>(string resource, int page = 1, int pageSize = GiantBombBase.DefaultLimit, string[] fieldList = null, IDictionary<string, SortDirection> sortOptions = null, IDictionary<string, object> filterOptions = null) where TResult : new()
{
var request = GetListResource(resource, page, pageSize, fieldList, sortOptions, filterOptions);
var results = await ExecuteAsync<GiantBombResults<TResult>>(request);

if (results != null && results.StatusCode == GiantBombBase.StatusOk)
return results.Results;

if (results != null && results.StatusCode != GiantBombBase.StatusOk)
throw new GiantBombApiException(results.StatusCode, results.Error);

return null;
}

public virtual TResult GetSingleResource<TResult>(string resource, int resourceId, int id,
string[] fieldList = null) where TResult : class, new()
{
return GetSingleResourceAsync<TResult>(resource, resourceId, id, fieldList).Result;
}

public virtual async Task<TResult> GetSingleResourceAsync<TResult>(string resource, int resourceId, int id, string[] fieldList = null) where TResult : class, new() {
var request = GetSingleResource(resource, resourceId, id, fieldList);
var result = Execute<GiantBombResult<TResult>>(request);
var result = await ExecuteAsync<GiantBombResult<TResult>>(request);

if (result != null && result.StatusCode == GiantBombBase.StatusOk)
return result.Results;
Expand Down
14 changes: 9 additions & 5 deletions GiantBomb.Api/GiantBomb.Api.csproj
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Project ToolsVersion="12.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
Expand All @@ -10,10 +10,11 @@
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>GiantBomb.Api</RootNamespace>
<AssemblyName>GiantBomb.Api</AssemblyName>
<TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
<TargetFrameworkVersion>v4.5.2</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<SolutionDir Condition="$(SolutionDir) == '' Or $(SolutionDir) == '*Undefined*'">..\..\GiantBomb-CSharp\</SolutionDir>
<RestorePackages>true</RestorePackages>
<TargetFrameworkProfile />
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
Expand All @@ -23,6 +24,7 @@
<DefineConstants>TRACE;DEBUG;FRAMEWORK</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<Prefer32Bit>false</Prefer32Bit>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
Expand All @@ -31,14 +33,15 @@
<DefineConstants>TRACE;FRAMEWORK</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<Prefer32Bit>false</Prefer32Bit>
</PropertyGroup>
<ItemGroup>
<Reference Include="fastJSON">
<HintPath>..\Dependencies\fastJSON.dll</HintPath>
</Reference>
<Reference Include="RestSharp, Version=105.0.1.0, Culture=neutral, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\packages\RestSharp.105.0.1\lib\net4\RestSharp.dll</HintPath>
<Reference Include="RestSharp, Version=105.1.0.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\RestSharp.105.1.0\lib\net452\RestSharp.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
Expand All @@ -51,6 +54,7 @@
<ItemGroup>
<Compile Include="Core.cs" />
<Compile Include="FastJsonDeserializer.cs" />
<Compile Include="GiantBombHttpException.cs" />
<Compile Include="GiantBombApiException.cs" />
<Compile Include="Model\Franchise.cs" />
<Compile Include="ResourceTypes.cs" />
Expand Down
6 changes: 4 additions & 2 deletions GiantBomb.Api/GiantBomb.Api.nuspec
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@
<owners>kayub</owners>
<id>GiantBomb.Api</id>
<title>GiantBomb API (C#)</title>
<projectUrl>http://github.com/kamranayub/GiantBomb-CSharp</projectUrl>
<projectUrl>https://github.com/kamranayub/GiantBomb-CSharp</projectUrl>
<licenseUrl>https://github.com/kamranayub/GiantBomb-CSharp/blob/master/LICENSE.txt</licenseUrl>
<requireLicenseAcceptance>false</requireLicenseAcceptance>
<description>RestSharp-based API wrapper for the GiantBomb games database public API</description>
<copyright>Copyright © kayub 2015</copyright>
<releaseNotes>
Updated with fixes, see README.
For changes, see README.
</releaseNotes>
<tags>giantbomb</tags>
</metadata>
<files>
<file src="readme.txt" target="readme.txt" />
Expand Down
19 changes: 19 additions & 0 deletions GiantBomb.Api/GiantBombHttpException.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using System;

namespace GiantBomb.Api
{
public class GiantBombHttpException : Exception
{
public GiantBombHttpException(string content)
: base("GiantBomb HTTP Exception, bad request from API: " + content)
{

}

public GiantBombHttpException(string content, Exception innerEx)
: base("GiantBomb HTTP Exception, bad request from API: " + content, innerEx)
{

}
}
}
Loading

0 comments on commit 16717da

Please sign in to comment.