Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,4 @@ _ReSharper*/
/packages/
!packages/repositories.config
/.vs/SharpBucket/v15/Server/sqlite3
*.nupkg
6 changes: 5 additions & 1 deletion ConsoleTests/ConsoleTests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>ConsoleTests</RootNamespace>
<AssemblyName>ConsoleTests</AssemblyName>
<TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<TargetFrameworkProfile />
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
Expand All @@ -21,6 +22,7 @@
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<Prefer32Bit>false</Prefer32Bit>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
Expand All @@ -30,6 +32,7 @@
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<Prefer32Bit>false</Prefer32Bit>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
Expand All @@ -46,6 +49,7 @@
</ProjectReference>
</ItemGroup>
<ItemGroup>
<None Include="app.config" />
<None Include="README.md" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
Expand Down
3 changes: 3 additions & 0 deletions ConsoleTests/app.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5"/></startup></configuration>
19 changes: 19 additions & 0 deletions SharpBucket/Authentication/IAuthenticate.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Collections.Generic;
using RestSharp;
using Serilog;

namespace SharpBucket.Authentication
{
Expand All @@ -13,5 +14,23 @@ public virtual T GetResponse<T>(string url, Method method, T body, IDictionary<s
var generic = executeMethod.MakeGenericMethod(typeof(T));
return (T)generic.Invoke(this, new object[] { url, method, body, client, requestParameters });
}

/// <summary>
/// With SeriLog logging
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="logger"></param>
/// <param name="url"></param>
/// <param name="method"></param>
/// <param name="body"></param>
/// <param name="requestParameters"></param>
/// <returns></returns>
public virtual T GetResponse<T>(ILogger logger, string url, Method method, T body, IDictionary<string, object> requestParameters)
{
var executeMethod = typeof(RequestExecutor).GetMethod("ExecuteRequestWithLogging");
var generic = executeMethod?.MakeGenericMethod(typeof(T));
return (T)generic?.Invoke(this, new object[] { logger,url, method, body, client, requestParameters
});
}
}
}
64 changes: 64 additions & 0 deletions SharpBucket/Authentication/RequestExecutor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
using System.Net;
using RestSharp;
using RestSharp.Deserializers;
using Serilog;
using SharpBucket.V2.Pocos;

namespace SharpBucket.Authentication
{
Expand Down Expand Up @@ -87,5 +89,67 @@ private static bool RequestingSimpleType<T>()
{
return typeof(T) == typeof(object);
}


/// <summary>
/// With Logging
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="logger"></param>
/// <param name="url"></param>
/// <param name="method"></param>
/// <param name="body"></param>
/// <param name="client"></param>
/// <param name="requestParameters"></param>
/// <returns></returns>
public static T ExecuteRequestWithLogging<T>(ILogger logger, string url, Method method, T body, RestClient client, IDictionary<string, object> requestParameters)
where T : new()
{
var request = new RestRequest(url, method);
if (requestParameters != null)
{
foreach (var requestParameter in requestParameters)
{
request.AddParameter(requestParameter.Key, requestParameter.Value);
}
}

if (ShouldAddBody(method))
{
if (body.GetType() != typeof(Branch))
{
request.RequestFormat = DataFormat.Json;
request.AddBody(body);
}
else
{
request.AddObject(body);
request.AddHeader("Content-Type", "multipart/form-data");
}
}

//Fixed bug that prevents RestClient for adding custom headers to the request
//https://stackoverflow.com/questions/22229393/why-is-restsharp-addheaderaccept-application-json-to-a-list-of-item

client.ClearHandlers();

client.AddHandler("application/json", new JsonDeserializer());

var result = ExectueRequest<T>(method, client, request);

if (result.ErrorException != null)
{
throw new WebException("REST client encountered an error: " + result.ErrorMessage, result.ErrorException);
}
// This is a hack in order to allow this method to work for simple types as well
// one example of this is the GetRevisionRaw method
if (RequestingSimpleType<T>())
{
return result.Content as dynamic;
}

logger.Debug($"{result.StatusCode}: {result.StatusDescription}");
return result.Data;
}
}
}
82 changes: 82 additions & 0 deletions SharpBucket/SharpBucket.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.Net;
using RestSharp;
using Serilog;
using SharpBucket.Authentication;
using SharpBucket.Utility;

Expand Down Expand Up @@ -119,26 +120,107 @@ private T Send<T>(T body, Method method, string overrideUrl = null, IDictionary<
return response;
}

/// <summary>
/// With Logging
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="logger"></param>
/// <param name="body"></param>
/// <param name="method"></param>
/// <param name="overrideUrl"></param>
/// <param name="requestParameters"></param>
/// <returns></returns>
private T Send<T>(ILogger logger, T body, Method method, string overrideUrl = null, IDictionary<string, object> requestParameters = null)
{
var relativeUrl = overrideUrl;
T response;
try
{
response = authenticator.GetResponse(logger, relativeUrl, method, body, requestParameters);
}
catch (WebException ex)
{
Console.WriteLine(ex.Message);
response = default(T);
}
return response;
}

internal T Get<T>(T body, string overrideUrl, object requestParameters = null)
{
//Convert to dictionary to avoid refactoring the Send method.
var parameterDictionary = requestParameters.ToDictionary();
return Send(body, Method.GET, overrideUrl, parameterDictionary);
}

/// <summary>
/// With Logging
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="logger"></param>
/// <param name="body"></param>
/// <param name="overrideUrl"></param>
/// <param name="requestParameters"></param>
/// <returns></returns>
internal T Get<T>(ILogger logger, T body, string overrideUrl, object requestParameters = null)
{
//Convert to dictionary to avoid refactoring the Send method.
var parameterDictionary = requestParameters.ToDictionary();
return Send(logger, body, Method.GET, overrideUrl, parameterDictionary);
}

internal T Post<T>(T body, string overrideUrl)
{
return Send(body, Method.POST, overrideUrl);
}

/// <summary>
/// With Logging
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="logger"></param>
/// <param name="body"></param>
/// <param name="overrideUrl"></param>
/// <returns></returns>
internal T Post<T>(ILogger logger, T body, string overrideUrl)
{
return Send(logger, body, Method.POST, overrideUrl);
}

internal T Put<T>(T body, string overrideUrl)
{
return Send(body, Method.PUT, overrideUrl);
}

/// <summary>
/// With Logging
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="logger"></param>
/// <param name="body"></param>
/// <param name="overrideUrl"></param>
/// <returns></returns>
internal T Put<T>(ILogger logger, T body, string overrideUrl)
{
return Send(logger, body, Method.PUT, overrideUrl);
}

internal T Delete<T>(T body, string overrideUrl)
{
return Send(body, Method.DELETE, overrideUrl);
}

/// <summary>
/// With Logging
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="logger"></param>
/// <param name="body"></param>
/// <param name="overrideUrl"></param>
/// <returns></returns>
internal T Delete<T>(ILogger logger, T body, string overrideUrl)
{
return Send(logger, body, Method.DELETE, overrideUrl);
}
}
}
24 changes: 18 additions & 6 deletions SharpBucket/SharpBucket.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>SharpBucket</RootNamespace>
<AssemblyName>SharpBucket</AssemblyName>
<TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<TargetFrameworkProfile />
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
Expand All @@ -20,6 +21,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 @@ -29,16 +31,18 @@
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<DocumentationFile>bin\Release\SharpBucket.XML</DocumentationFile>
<Prefer32Bit>false</Prefer32Bit>
</PropertyGroup>
<ItemGroup>
<Reference Include="Microsoft.CSharp" />
<Reference Include="Newtonsoft.Json, Version=7.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
<HintPath>..\packages\Newtonsoft.Json.7.0.1\lib\net40\Newtonsoft.Json.dll</HintPath>
<Private>True</Private>
<HintPath>..\packages\Newtonsoft.Json.7.0.1\lib\net45\Newtonsoft.Json.dll</HintPath>
</Reference>
<Reference Include="RestSharp, Version=105.2.3.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\RestSharp.105.2.3\lib\net4\RestSharp.dll</HintPath>
<Private>True</Private>
<HintPath>..\packages\RestSharp.105.2.3\lib\net45\RestSharp.dll</HintPath>
</Reference>
<Reference Include="Serilog, Version=2.0.0.0, Culture=neutral, PublicKeyToken=24c2f752a8e58a10, processorArchitecture=MSIL">
<HintPath>..\packages\Serilog.2.6.0\lib\net45\Serilog.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
Expand Down Expand Up @@ -81,13 +85,19 @@
<Compile Include="V2\Pocos\BranchRestrictionInfo.cs" />
<Compile Include="V2\Pocos\BuildInfo.cs" />
<Compile Include="V2\Pocos\CommitInfo.cs" />
<Compile Include="V2\Pocos\Email.cs" />
<Compile Include="V2\Pocos\Fork.cs" />
<Compile Include="V2\Pocos\ForkInfo.cs" />
<Compile Include="V2\Pocos\Group.cs" />
<Compile Include="V2\Pocos\IteratorBasedPage.cs" />
<Compile Include="V2\Pocos\LinkConfigurations.cs" />
<Compile Include="V2\Pocos\ListBasedPage.cs" />
<Compile Include="V2\Pocos\Member.cs" />
<Compile Include="V2\Pocos\Parent.cs" />
<Compile Include="V2\Pocos\Activity.cs" />
<Compile Include="V2\Pocos\ActivityInfo.cs" />
<Compile Include="V2\Pocos\Project.cs" />
<Compile Include="V2\Pocos\ProjectPostParams.cs" />
<Compile Include="V2\Pocos\Tag.cs" />
<Compile Include="V2\Pocos\Update.cs" />
<Compile Include="V2\Pocos\Comment.cs" />
Expand Down Expand Up @@ -154,7 +164,9 @@
<Compile Include="V1\SharpBucketV1.cs" />
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
<None Include="packages.config">
<SubType>Designer</SubType>
</None>
<None Include="SharpBucket.nuspec">
<SubType>Designer</SubType>
</None>
Expand Down
30 changes: 30 additions & 0 deletions SharpBucket/V2/EndPoints/BranchResource.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using SharpBucket.V2.Pocos;
using System.Collections.Generic;
using Serilog;

namespace SharpBucket.V2.EndPoints
{
Expand Down Expand Up @@ -30,5 +31,34 @@ public List<Branch> ListBranches()
{
return _repositoriesEndPoint.ListBranches(_accountName, _repository);
}

/// <summary>
/// With Logging
/// Returns a list of branches for the specific repository
/// </summary>
/// <returns></returns>
public List<Branch> ListBranches(ILogger logger)
{
return _repositoriesEndPoint.ListBranches(logger, _accountName, _repository);
}

/// <summary>
/// Posts a new branch for the specific repository
/// </summary>
/// <param name="branch"></param>
public void PostBranch(Branch branch)
{
_repositoriesEndPoint.PostBranch(_accountName, _repository, branch);
}

/// <summary>
/// With Logging
/// </summary>
/// <param name="logger"></param>
/// <param name="branch"></param>
public void PostBranch(ILogger logger, Branch branch)
{
_repositoriesEndPoint.PostBranch(logger, _accountName, _repository, branch);
}
}
}
Loading