Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
7f87869
Added README.md and comments for the following:
Apr 25, 2018
f10f365
Added Serilog nuget and upgraded the NET Framework from 4.0 => 4.5
Apr 25, 2018
bbe2e20
SharpBucket/Authentication/RequestExecutor.cs => Added ExecuteRequest…
Apr 25, 2018
06b508c
Added ILogger parameter to Sharpbucket.cs methods (Send/Get/Post/Put/…
Apr 25, 2018
c6bf276
Added Serilog Logger to EndPoint
Apr 25, 2018
f9f1065
Added logging to repositoriesEndPoint
Apr 25, 2018
7bc5fc9
Logging added to repository Resource.
Apr 25, 2018
acf27a8
* Added PutRepository
Apr 25, 2018
20c058b
TeamEndPoin : Added PostProject/ ListProjects / GetProject
Apr 25, 2018
dfcb474
endpoint and sharpbucket v2 interfaces
Apr 25, 2018
313491e
Added ISharpBucket and ISharpBucketV2 interfaces to enable mocking fo…
Apr 26, 2018
c30ccef
Merge branch 'interfacing' into development
Apr 26, 2018
64d8c43
Revert "endpoint and sharpbucket v2 interfaces"
Apr 26, 2018
4f83d6e
SharpBucket interfacing
Apr 26, 2018
7b65330
Merge branch 'interfacing' into development
Apr 26, 2018
70032cb
Merge branch 'serilogging' into development
Apr 26, 2018
c6b0222
repo slug to ToLowerInvariant, uppercase characters will cause issues…
Apr 26, 2018
580e73e
removed wrong files
Apr 26, 2018
983ba34
added ToLowerInvariant
Apr 26, 2018
fe6f8f3
removed unnecessary files
Apr 26, 2018
23f3a8d
merge repository-slug-tolowerinvariant repos into dev
Apr 26, 2018
a20f642
merge interfacing into dev
Apr 26, 2018
096001a
Merge branch 'serilogging' into development
Apr 26, 2018
d9b54a6
added public Project project { get; set; }
Apr 26, 2018
4671898
Merge branch 'serilogging' into development
Apr 26, 2018
7dedca2
endpoint using interface
Apr 26, 2018
b9079a0
CRUD methods public
Apr 26, 2018
59edc07
all changes together
Apr 26, 2018
957d8a1
slug parser for repository endpoint and added missing links
Apr 26, 2018
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
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,6 @@ obj/
_ReSharper*/
*sln.dotsettings
/packages/
!packages/repositories.config
!packages/repositories.config
/.vs/SharpBucket/v15/Server/sqlite3
*.nupkg
9 changes: 8 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 @@ -45,6 +48,10 @@
<Name>SharpBucket</Name>
</ProjectReference>
</ItemGroup>
<ItemGroup>
<None Include="app.config" />
<None Include="README.md" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.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
});
}
}
}
65 changes: 65 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,68 @@ 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}");
logger.Verbose($"{result.Content}");
return result.Data;
}
}
}
76 changes: 76 additions & 0 deletions SharpBucket/ISharpBucket.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
using Serilog;
using SharpBucket.Authentication;

namespace SharpBucket
{
public interface ISharpBucket
{
/// <summary>
/// Use basic authentication with the BitBucket API. OAuth authentication is preferred over
/// basic authentication, due to security reasons.
/// </summary>
/// <param name="username">Your BitBucket user name.</param>
/// <param name="password">Your BitBucket password.</param>
void BasicAuthentication(string username, string password);

/// <summary>
/// Use 2 legged OAuth 1.0a authentication. This is similar to basic authentication, since
/// it requires the same number of steps. It is still safer to use than basic authentication,
/// since you can revoke the API keys.
/// More info:
/// https://confluence.atlassian.com/display/BITBUCKET/OAuth+on+Bitbucket
/// </summary>
/// <param name="consumerKey">Your consumer API key obtained from the BitBucket web page.</param>
/// <param name="consumerSecretKey">Your consumer secret API key also obtained from the BitBucket web page.</param>
void OAuth2LeggedAuthentication(string consumerKey, string consumerSecretKey);

/// <summary>
/// Use 3 legged OAuth 1.0a authentication. This is the most secure one, but for simple uses it might
/// be a bit too complex.
/// More info:
/// https://confluence.atlassian.com/display/BITBUCKET/OAuth+on+Bitbucket
/// </summary>
/// <param name="consumerKey">Your consumer API key obtained from the BitBucket web page.</param>
/// <param name="consumerSecretKey">Your consumer secret API key also obtained from the BitBucket web page.</param>
/// <param name="callback">Callback URL to which BitBucket will send the pin.</param>
/// <returns></returns>
OAuthentication3Legged OAuth3LeggedAuthentication(
string consumerKey,
string consumerSecretKey,
string callback = "oob");

/// <summary>
/// Use 3 legged OAuth 1.0a authentication. Use this method if you have already obtained the OAuthToken
/// and OAuthSecretToken. This method can be used so you do not have to go trough the whole 3 legged
/// process every time. You can save the tokens you receive the first time and reuse them in another session.
/// </summary>
/// <param name="consumerKey">Your consumer API key obtained from the BitBucket web page.</param>
/// <param name="consumerSecretKey">Your consumer secret API key also obtained from the BitBucket web page.</param>
/// <param name="oauthToken">Your OAuth token that was obtained on a previous session.</param>
/// <param name="oauthTokenSecret">Your OAuth secret token thata was obtained on a previous session.</param>
/// <returns></returns>
OAuthentication3Legged OAuth3LeggedAuthentication(
string consumerKey,
string consumerSecretKey,
string oauthToken,
string oauthTokenSecret);

/// <summary>
/// Use Oauth2 authentication. This is the neweset version and is prefered.
/// </summary>
/// <param name="consumerKey"></param>
/// <param name="consumerSecretKey"></param>
/// <returns></returns>
OAuthentication2 OAuthentication2(string consumerKey, string consumerSecretKey);

T Get<T>(T body, string overrideUrl, object requestParameters = null);
T Post<T>(T body, string overrideUrl);
T Put<T>(T body, string overrideUrl);
T Delete<T>(T body, string overrideUrl);

T Get<T>(ILogger logger,T body, string overrideUrl, object requestParameters = null);
T Post<T>(ILogger logger, T body, string overrideUrl);
T Put<T>(ILogger logger, T body, string overrideUrl);
T Delete<T>(ILogger logger, T body, string overrideUrl);
}
}
Loading