Skip to content

Commit

Permalink
intial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
jgiacomini committed Apr 13, 2018
0 parents commit 732f0be
Show file tree
Hide file tree
Showing 20 changed files with 802 additions and 0 deletions.
10 changes: 10 additions & 0 deletions .gitignore
@@ -0,0 +1,10 @@
################################################################################
# This .gitignore file was automatically created by Microsoft(R) Visual Studio.
################################################################################

/.vs
bin/
obj/
Debug/
Release/
*.user
23 changes: 23 additions & 0 deletions TinyHttp.Tests/Test.cs
@@ -0,0 +1,23 @@
using System.Threading.Tasks;

namespace TinyHttp
{
////public class Test
////{
//// async Task MakeTest()
//// {

//// IFluent client = null;

//// await client.Header("Test", "Test").
//// SerializeWith(new TinyJsonSerializer()).
//// DeserializeWith(new TinyJsonDeserializer()).
//// PostAsync<TestPoco>(null);

//// await client.Header("Test", "Test").
//// GetAsync(null);


//// }
////}
}
8 changes: 8 additions & 0 deletions TinyHttp.Tests/TestPoco.cs
@@ -0,0 +1,8 @@
namespace TinyHttp
{
public class TestPoco
{
public string Toto { get; set; }

}
}
19 changes: 19 additions & 0 deletions TinyHttp.Tests/TinyHttp.Tests.csproj
@@ -0,0 +1,19 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netcoreapp2.0</TargetFramework>

<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.6.0" />
<PackageReference Include="MSTest.TestAdapter" Version="1.2.0" />
<PackageReference Include="MSTest.TestFramework" Version="1.2.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\TinyHttp\TinyHttp.csproj" />
</ItemGroup>

</Project>
31 changes: 31 additions & 0 deletions TinyHttp.sln
@@ -0,0 +1,31 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 15
VisualStudioVersion = 15.0.27004.2005
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TinyHttp", "TinyHttp\TinyHttp.csproj", "{AE3327FF-7E49-467A-A410-8F151CCC30AD}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TinyHttp.Tests", "TinyHttp.Tests\TinyHttp.Tests.csproj", "{C48AB557-FF2E-4739-88A9-7A8F3B484FC1}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{AE3327FF-7E49-467A-A410-8F151CCC30AD}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{AE3327FF-7E49-467A-A410-8F151CCC30AD}.Debug|Any CPU.Build.0 = Debug|Any CPU
{AE3327FF-7E49-467A-A410-8F151CCC30AD}.Release|Any CPU.ActiveCfg = Release|Any CPU
{AE3327FF-7E49-467A-A410-8F151CCC30AD}.Release|Any CPU.Build.0 = Release|Any CPU
{C48AB557-FF2E-4739-88A9-7A8F3B484FC1}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{C48AB557-FF2E-4739-88A9-7A8F3B484FC1}.Debug|Any CPU.Build.0 = Debug|Any CPU
{C48AB557-FF2E-4739-88A9-7A8F3B484FC1}.Release|Any CPU.ActiveCfg = Release|Any CPU
{C48AB557-FF2E-4739-88A9-7A8F3B484FC1}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {CA5D635D-59C9-4247-8C13-9D78B5834FC2}
EndGlobalSection
EndGlobal
26 changes: 26 additions & 0 deletions TinyHttp/Exceptions/ConnectionException.cs
@@ -0,0 +1,26 @@
using System;

namespace TinyHttp
{
public class ConnectionException : TinyHttpException
{
public ConnectionException(string message, string url, string verb, Exception innerException)
: base(message, innerException)
{
Url = url;
Verb = verb;
}

public string Url
{
get => (string)Data[nameof(Url)];
private set => Data[nameof(Url)] = value;
}

public string Verb
{
get => (string)Data[nameof(Verb)];
private set => Data[nameof(Verb)] = value;
}
}
}
22 changes: 22 additions & 0 deletions TinyHttp/Exceptions/DeserializeException.cs
@@ -0,0 +1,22 @@
using System;
using System.Collections.Generic;
using System.Text;

namespace TinyHttp
{
public class DeserializeException : TinyHttpException
{
public DeserializeException(string message, Exception innerException, string dataToDeserialize)
: base(message, innerException)

{
DataToDeserialize = dataToDeserialize;
}

public string DataToDeserialize
{
get => (string)Data[nameof(DataToDeserialize)];
private set => Data[nameof(DataToDeserialize)] = value;
}
}
}
121 changes: 121 additions & 0 deletions TinyHttp/Exceptions/HttpException.cs
@@ -0,0 +1,121 @@
using System;
using System.Linq;
using System.Net;
using System.Net.Http.Headers;
using System.Text;

namespace TinyHttp
{
/// <summary>
/// A httpException
/// </summary>
/// <seealso cref="System.Exception" />
/// <summary>
/// A httpexception
/// </summary>
/// <seealso cref="System.Exception" />
public class HttpException : TinyHttpException
{
/// <summary>
/// Initializes a new instance of the <see cref="HttpException"/> class.
/// </summary>
/// <param name="message">The message.</param>
/// <param name="reasonPhrase">The reason phrase.</param>
/// <param name="url">The URL.</param>
/// <param name="verb">The verb.</param>
/// <param name="content">The content.</param>
/// <param name="statusCode">The status code.</param>
/// <param name="ex">The ex.</param>
public HttpException(
string message,
HttpRequestHeaders headers,
string reasonPhrase,
string url,
string verb,
string content,
HttpStatusCode statusCode,
Exception ex)
: base($"URL : {url}, Headers {HeadersToString(headers)}, Verb : {verb}, StatusCode : {statusCode}, message : {message}, Response Content : {content}", ex)
{
Verb = verb;
Url = url;
Content = content;
StatusCode = statusCode;
ReasonPhrase = reasonPhrase;
}

private static string HeadersToString(HttpRequestHeaders headers)
{
var stringBuilder = new StringBuilder(headers.Count() * 50);
foreach (var header in headers)
{
stringBuilder.AppendLine($"Header key : {header.Key}");

var countValuesHeader = header.Value.Count();
if (countValuesHeader > 1)
{
stringBuilder.AppendLine($"Header values ({countValuesHeader}) :");
}
else
{
stringBuilder.AppendLine($"Header value :");
}

foreach (var value in header.Value)
{
stringBuilder.AppendLine(value);
}
}

return stringBuilder.ToString();
}

/// <summary>
/// Gets the verb.
/// </summary>
/// <value>
/// The verb.
/// </value>
public string Verb { get; private set; }

/// <summary>
/// Gets the headers of sended request
/// </summary>
/// <value>
/// The verb.
/// </value>
public HttpRequestHeaders Headers { get; private set; }

/// <summary>
/// Gets or sets the reason phrase.
/// </summary>
/// <value>
/// The reason phrase.
/// </value>
public string ReasonPhrase { get; set; }

/// <summary>
/// Gets the URL.
/// </summary>
/// <value>
/// The URL.
/// </value>
public string Url { get; private set; }

/// <summary>
/// Gets the content.
/// </summary>
/// <value>
/// The content.
/// </value>
public string Content { get; private set; }

/// <summary>
/// Gets the status code.
/// </summary>
/// <value>
/// The status code.
/// </value>
public HttpStatusCode StatusCode { get; private set; }
}
}
14 changes: 14 additions & 0 deletions TinyHttp/Exceptions/TinyHttpException.cs
@@ -0,0 +1,14 @@
using System;
using System.Collections.Generic;
using System.Text;

namespace TinyHttp
{
public abstract class TinyHttpException : Exception
{
public TinyHttpException(string message, Exception innerException) :
base (message, innerException)
{
}
}
}
14 changes: 14 additions & 0 deletions TinyHttp/IDeserializableRequest.cs
@@ -0,0 +1,14 @@
using System.Threading;
using System.Threading.Tasks;

namespace TinyHttp
{
public interface IDeserializableRequest
{
Task<TResult> PostAsync<TResult>(string route, CancellationToken cancellationToken = default);
Task<TResult> GetAsync<TResult>(string route, CancellationToken cancellationToken = default);
Task<TResult> PutAsync<TResult>(string route, CancellationToken cancellationToken = default);
Task<TResult> PatchAsync<TResult>(string route, CancellationToken cancellationToken = default);
Task<TResult> DeleteAsync<TResult>(string route, CancellationToken cancellationToken = default);
}
}
18 changes: 18 additions & 0 deletions TinyHttp/IFluent.cs
@@ -0,0 +1,18 @@
using System.Collections.Generic;

namespace TinyHttp
{
public interface IFluent : ISerializableFluent, ISimpleRequest
{
Dictionary<string, string> DefaultHeaders { get; }
IFluent AddHeader(string key, string value);
IFluent AddQueryParameter (string key, string value);
IFluent AddFormParameter(string key, string value);

IFluent FromStream();
IFluent ToStream();

IFluent WithByteArrayResult();
IFluent WithStreamResult();
}
}
15 changes: 15 additions & 0 deletions TinyHttp/ISerializableFluent.cs
@@ -0,0 +1,15 @@
using System.Threading;
using System.Threading.Tasks;

namespace TinyHttp
{
public interface ISerializableFluent
{
ISerializableFluent SerializeWith(ISerializer serializer);
ISerializableFluent DeserializeWith(IDeserializer deserializer);
Task<TResult> PostAsync<TResult>(string route, CancellationToken cancellationToken = default);
Task<TResult> PostAsync<TResult, TInput>(string route, TInput data, CancellationToken cancellationToken = default);
Task<T> PostAsync<T>(string route, byte[] data, CancellationToken cancellationToken = default);

}
}
14 changes: 14 additions & 0 deletions TinyHttp/ISimpleRequest.cs
@@ -0,0 +1,14 @@
using System.Threading;
using System.Threading.Tasks;

namespace TinyHttp
{
public interface ISimpleRequest
{
Task PostAsync(string route, CancellationToken cancellationToken = default);
Task GetAsync(string route, CancellationToken cancellationToken = default);
Task PutAsync(string route, CancellationToken cancellationToken = default);
Task PatchAsync(string route, CancellationToken cancellationToken = default);
Task DeleteAsync(string route, CancellationToken cancellationToken = default);
}
}
8 changes: 8 additions & 0 deletions TinyHttp/Serializer/IDeserializer.cs
@@ -0,0 +1,8 @@
using System.Threading.Tasks;
namespace TinyHttp
{
public interface IDeserializer
{
T Deserialize<T>(string data);
}
}
9 changes: 9 additions & 0 deletions TinyHttp/Serializer/ISerializer.cs
@@ -0,0 +1,9 @@
namespace TinyHttp
{
public interface ISerializer
{
string MediaType { get; }

string Serialize<T>(T data);
}
}
20 changes: 20 additions & 0 deletions TinyHttp/Serializer/TinyJsonDeserializer.cs
@@ -0,0 +1,20 @@
using Newtonsoft.Json;
using System;

namespace TinyHttp
{
public class TinyJsonDeserializer : IDeserializer
{
public T Deserialize<T>(string data)
{
try
{
return JsonConvert.DeserializeObject<T>(data);
}
catch (Exception ex)
{
throw new DeserializeException("Error during deserialization", ex, data);
}
}
}
}

0 comments on commit 732f0be

Please sign in to comment.