Skip to content

Commit f627e29

Browse files
committed
Initial Commit
1 parent cf3b057 commit f627e29

25 files changed

+540
-0
lines changed

GeekLearning.RestKit.sln

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio 14
4+
VisualStudioVersion = 14.0.25420.1
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "GeekLearning.RestKit.Core", "src\GeekLearning.RestKit.Core\GeekLearning.RestKit.Core.xproj", "{DB5207D5-3F54-45F1-B7FC-C266F55C33A4}"
7+
EndProject
8+
Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "GeekLearning.RestKit.Json", "src\GeekLearning.RestKit.Json\GeekLearning.RestKit.Json.xproj", "{9C387D89-0D16-4279-8B94-37D60DAA7FF5}"
9+
EndProject
10+
Global
11+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
12+
Debug|Any CPU = Debug|Any CPU
13+
Release|Any CPU = Release|Any CPU
14+
EndGlobalSection
15+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
16+
{DB5207D5-3F54-45F1-B7FC-C266F55C33A4}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
17+
{DB5207D5-3F54-45F1-B7FC-C266F55C33A4}.Debug|Any CPU.Build.0 = Debug|Any CPU
18+
{DB5207D5-3F54-45F1-B7FC-C266F55C33A4}.Release|Any CPU.ActiveCfg = Release|Any CPU
19+
{DB5207D5-3F54-45F1-B7FC-C266F55C33A4}.Release|Any CPU.Build.0 = Release|Any CPU
20+
{9C387D89-0D16-4279-8B94-37D60DAA7FF5}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
21+
{9C387D89-0D16-4279-8B94-37D60DAA7FF5}.Debug|Any CPU.Build.0 = Debug|Any CPU
22+
{9C387D89-0D16-4279-8B94-37D60DAA7FF5}.Release|Any CPU.ActiveCfg = Release|Any CPU
23+
{9C387D89-0D16-4279-8B94-37D60DAA7FF5}.Release|Any CPU.Build.0 = Release|Any CPU
24+
EndGlobalSection
25+
GlobalSection(SolutionProperties) = preSolution
26+
HideSolutionNode = FALSE
27+
EndGlobalSection
28+
EndGlobal
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Net.Http;
5+
using System.Threading.Tasks;
6+
7+
namespace GeekLearning.RestKit.Core
8+
{
9+
public abstract class ApiException: Exception
10+
{
11+
public ApiException()
12+
{
13+
}
14+
}
15+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Threading.Tasks;
5+
6+
namespace GeekLearning.RestKit.Core
7+
{
8+
public abstract class ApiException<TResponse>: ApiException
9+
{
10+
public ApiException()
11+
{
12+
}
13+
14+
}
15+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Threading.Tasks;
5+
6+
namespace GeekLearning.RestKit.Core
7+
{
8+
public class BadRequestApiException: ApiException
9+
{
10+
}
11+
12+
public class BadRequestApiException<TResponse> : ApiException<TResponse>
13+
{
14+
}
15+
}
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
using Microsoft.Extensions.Options;
2+
using System.Collections.Generic;
3+
using System.Net;
4+
using System.Net.Http;
5+
using System.Text;
6+
using System.Threading.Tasks;
7+
8+
namespace GeekLearning.RestKit.Core
9+
{
10+
public abstract class ClientBase<TOptions>
11+
where TOptions : class, new()
12+
{
13+
private IMediaFormatterProvider mediaFormatterProvider;
14+
15+
public ClientBase(IOptions<TOptions> options, IMediaFormatterProvider mediaFormatterProvider)
16+
{
17+
this.Options = options.Value;
18+
this.mediaFormatterProvider = mediaFormatterProvider;
19+
}
20+
21+
protected TOptions Options { get; private set; }
22+
23+
protected Task<TTarget> TransformResponseAsync<TTarget>(HttpResponseMessage message)
24+
{
25+
IMediaFormatter mediaFormatter = this.mediaFormatterProvider.GetMediaFormatter(message.Content.Headers.ContentType);
26+
if (mediaFormatter == null)
27+
{
28+
throw new UnsupportedMediaTypeApiException(message.Content.Headers.ContentType);
29+
}
30+
return mediaFormatter.TransformAsync<TTarget>(message.Content);
31+
}
32+
33+
protected HttpContent TransformRequestBody(object data, string mediaType)
34+
{
35+
IMediaFormatter mediaFormatter = this.mediaFormatterProvider.GetMediaFormatter(mediaType);
36+
if (mediaFormatter == null)
37+
{
38+
throw new UnsupportedMediaTypeApiException(mediaType);
39+
}
40+
return mediaFormatter.Format(data);
41+
}
42+
43+
/// <summary>
44+
/// Append the given query keys and values to the uri.
45+
/// </summary>
46+
/// <param name="uri">The base uri.</param>
47+
/// <param name="queryString">A collection of name value query pairs to append.</param>
48+
/// <returns>The combined result.</returns>
49+
protected static string AddQueryString(
50+
string uri,
51+
IEnumerable<KeyValuePair<string, object>> queryString)
52+
{
53+
var anchorIndex = uri.IndexOf('#');
54+
var uriToBeAppended = uri;
55+
var anchorText = "";
56+
// If there is an anchor, then the query string must be inserted before its first occurance.
57+
if (anchorIndex != -1)
58+
{
59+
anchorText = uri.Substring(anchorIndex);
60+
uriToBeAppended = uri.Substring(0, anchorIndex);
61+
}
62+
63+
var queryIndex = uriToBeAppended.IndexOf('?');
64+
var hasQuery = queryIndex != -1;
65+
66+
var sb = new StringBuilder();
67+
sb.Append(uriToBeAppended);
68+
foreach (var parameter in queryString)
69+
{
70+
if (parameter.Value == null)
71+
continue;
72+
sb.Append(hasQuery ? '&' : '?');
73+
sb.Append(WebUtility.UrlEncode(parameter.Key));
74+
sb.Append('=');
75+
sb.Append(WebUtility.UrlEncode(parameter.Value.ToString()));
76+
hasQuery = true;
77+
}
78+
79+
sb.Append(anchorText);
80+
return sb.ToString();
81+
}
82+
}
83+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Threading.Tasks;
5+
6+
namespace GeekLearning.RestKit.Core
7+
{
8+
public class ConflictApiException: ApiException
9+
{
10+
}
11+
12+
public class ConflictApiException<TResponse> : ApiException<TResponse>
13+
{
14+
}
15+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Threading.Tasks;
5+
6+
namespace GeekLearning.RestKit.Core
7+
{
8+
public class ForbiddenApiException: ApiException
9+
{
10+
11+
}
12+
13+
public class ForbiddenApiException<TResponse>: ApiException<TResponse>
14+
{
15+
}
16+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<PropertyGroup>
4+
<VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">14.0</VisualStudioVersion>
5+
<VSToolsPath Condition="'$(VSToolsPath)' == ''">$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)</VSToolsPath>
6+
</PropertyGroup>
7+
8+
<Import Project="$(VSToolsPath)\DotNet\Microsoft.DotNet.Props" Condition="'$(VSToolsPath)' != ''" />
9+
<PropertyGroup Label="Globals">
10+
<ProjectGuid>db5207d5-3f54-45f1-b7fc-c266f55c33a4</ProjectGuid>
11+
<RootNamespace>GeekLearning.RestKit.Core</RootNamespace>
12+
<BaseIntermediateOutputPath Condition="'$(BaseIntermediateOutputPath)'=='' ">.\obj</BaseIntermediateOutputPath>
13+
<OutputPath Condition="'$(OutputPath)'=='' ">.\bin\</OutputPath>
14+
<TargetFrameworkVersion>v4.5.2</TargetFrameworkVersion>
15+
</PropertyGroup>
16+
17+
<PropertyGroup>
18+
<SchemaVersion>2.0</SchemaVersion>
19+
</PropertyGroup>
20+
<Import Project="$(VSToolsPath)\DotNet\Microsoft.DotNet.targets" Condition="'$(VSToolsPath)' != ''" />
21+
</Project>
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Net.Http;
5+
using System.Threading.Tasks;
6+
7+
namespace GeekLearning.RestKit.Core
8+
{
9+
public interface IMediaFormatter
10+
{
11+
Task<TTarget> TransformAsync<TTarget>(HttpContent content);
12+
HttpContent Format(object data);
13+
bool Supports(string contentType);
14+
}
15+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Net.Http.Headers;
5+
using System.Threading.Tasks;
6+
7+
namespace GeekLearning.RestKit.Core
8+
{
9+
public interface IMediaFormatterProvider
10+
{
11+
IMediaFormatter GetMediaFormatter(MediaTypeHeaderValue contentType);
12+
IMediaFormatter GetMediaFormatter(string mediaType);
13+
}
14+
}

0 commit comments

Comments
 (0)