Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
…into ReallyLiri-master
  • Loading branch information
lvermeulen committed Jan 3, 2020
2 parents b4978e7 + b8d60c2 commit e062514
Show file tree
Hide file tree
Showing 24 changed files with 169 additions and 117 deletions.
4 changes: 2 additions & 2 deletions Bitbucket.Net.sln
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 15
VisualStudioVersion = 15.0.27130.2036
# Visual Studio Version 16
VisualStudioVersion = 16.0.28803.156
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Bitbucket.Net", "src\Bitbucket.Net\Bitbucket.Net.csproj", "{51EBF9F3-7DFA-4C72-B38D-D07B1ED7FCEE}"
EndProject
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
[![license](https://img.shields.io/github/license/lvermeulen/Bitbucket.Net.svg?maxAge=2592000)](https://github.com/lvermeulen/Bitbucket.Net/blob/master/LICENSE) [![NuGet](https://img.shields.io/nuget/v/Bitbucket.Net.svg?maxAge=2592000)](https://www.nuget.org/packages/Bitbucket.Net/)
![](https://img.shields.io/badge/.net-4.5.2-yellowgreen.svg) ![](https://img.shields.io/badge/netstandard-1.4-yellowgreen.svg)

Client for Bitbucket
Client for Bitbucket Server

## Features
* [X] Audit
Expand Down
7 changes: 4 additions & 3 deletions global.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
{
"projects": [ "src", "test" ],
"sdk": {
"version": "2.1.4"
"version": "1.1.13"
}
}
}
7 changes: 4 additions & 3 deletions src/Bitbucket.Net/Bitbucket.Net.csproj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>net452;netstandard1.4</TargetFrameworks>
Expand All @@ -13,11 +13,12 @@
<RepositoryType>git</RepositoryType>
<PackageTags>bitbucket</PackageTags>
<PackageIconUrl>https://i.imgur.com/OsDAzyV.png</PackageIconUrl>
<LangVersion>7</LangVersion>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Flurl.Http" Version="2.1.1" />
<PackageReference Include="Microsoft.CSharp" Version="4.4.1" />
<PackageReference Include="Flurl.Http" Version="2.4.1" />
<PackageReference Include="Microsoft.CSharp" Version="4.5.0" />
</ItemGroup>

</Project>
32 changes: 29 additions & 3 deletions src/Bitbucket.Net/BitbucketClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Linq;
using System.Net.Http;
using System.Threading.Tasks;
using Bitbucket.Net.Common;
using Bitbucket.Net.Common.Models;
using Flurl;
using Flurl.Http;
Expand All @@ -14,23 +15,48 @@ namespace Bitbucket.Net
{
public partial class BitbucketClient
{
private static readonly ISerializer s_serializer = new NewtonsoftJsonSerializer(new JsonSerializerSettings { ContractResolver = new CamelCasePropertyNamesContractResolver() });
private static readonly ISerializer s_serializer = new NewtonsoftJsonSerializer(new JsonSerializerSettings
{
ContractResolver = new CamelCasePropertyNamesContractResolver(),
NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore
});

static BitbucketClient()
{
JsonConvert.DefaultSettings = () => new JsonSerializerSettings
{
ContractResolver = new CamelCasePropertyNamesContractResolver(),
NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore
};
}

private readonly Url _url;
private readonly Func<string> _getToken;
private readonly string _userName;
private readonly string _password;

public BitbucketClient(string url, string userName, string password)
private BitbucketClient(string url)
{
_url = url;
}

public BitbucketClient(string url, string userName, string password)
: this(url)
{
_userName = userName;
_password = password;
}

public BitbucketClient(string url, Func<string> getToken)
: this(url)
{
_getToken = getToken;
}

private IFlurlRequest GetBaseUrl(string root = "/api", string version = "1.0") => new Url(_url)
.AppendPathSegment($"/rest{root}/{version}")
.ConfigureRequest(settings => settings.JsonSerializer = s_serializer)
.WithBasicAuth(_userName, _password);
.WithAuthentication(_getToken, _userName, _password);

private async Task<TResult> ReadResponseContentAsync<TResult>(HttpResponseMessage responseMessage, Func<string, TResult> contentHandler = null)
{
Expand Down
1 change: 1 addition & 0 deletions src/Bitbucket.Net/Branches/BitbucketClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ public async Task<bool> DeleteRepoBranchAsync(string projectKey, string reposito
};

var response = await GetBranchUrl($"/projects/{projectKey}/repos/{repositorySlug}/branches")
.WithHeader("Content-Type", "application/json")
.SendAsync(HttpMethod.Delete, new StringContent(JsonConvert.SerializeObject(data)))
.ConfigureAwait(false);

Expand Down
4 changes: 2 additions & 2 deletions src/Bitbucket.Net/CommentLikes/BitbucketClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ await GetCommentLikesUrl($"/projects/{projectKey}/repos/{repositorySlug}/commits
public async Task<bool> LikeCommitCommentAsync(string projectKey, string repositorySlug, string commitId, string commentId)
{
var response = await GetCommentLikesUrl($"/projects/{projectKey}/repos/{repositorySlug}/commits/{commitId}/comments/{commentId}/likes")
.PostAsync(new StringContent(""))
.PostJsonAsync(new StringContent(""))
.ConfigureAwait(false);

return await HandleResponseAsync(response).ConfigureAwait(false);
Expand Down Expand Up @@ -73,7 +73,7 @@ await GetCommentLikesUrl($"/projects/{projectKey}/repos/{repositorySlug}/pull-re
public async Task<bool> LikePullRequestCommentAsync(string projectKey, string repositorySlug, string pullRequestId, string commentId)
{
var response = await GetCommentLikesUrl($"/projects/{projectKey}/repos/{repositorySlug}/pull-requests/{pullRequestId}/comments/{commentId}/likes")
.PostAsync(new StringContent(""))
.PostJsonAsync(new StringContent(""))
.ConfigureAwait(false);

return await HandleResponseAsync(response).ConfigureAwait(false);
Expand Down
21 changes: 21 additions & 0 deletions src/Bitbucket.Net/Common/BitbucketHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,13 @@ public static string LineTypeToString(LineTypes lineType)
return result;
}

public static string LineTypeToString(LineTypes? lineType)
{
return lineType.HasValue
? LineTypeToString(lineType.Value)
: null;
}

public static LineTypes StringToLineType(string s)
{
var pair = s_stringByLineTypes.FirstOrDefault(kvp => kvp.Value.Equals(s, StringComparison.OrdinalIgnoreCase));
Expand Down Expand Up @@ -306,6 +313,13 @@ public static string FileTypeToString(FileTypes fileType)
return result;
}

public static string FileTypeToString(FileTypes? fileType)
{
return fileType.HasValue
? FileTypeToString(fileType.Value)
: null;
}

public static FileTypes StringToFileType(string s)
{
var pair = s_stringByFileTypes.FirstOrDefault(kvp => kvp.Value.Equals(s, StringComparison.OrdinalIgnoreCase));
Expand Down Expand Up @@ -573,6 +587,13 @@ public static string DiffTypeToString(DiffTypes diffType)
return result;
}

public static string DiffTypeToString(DiffTypes? diffType)
{
return diffType.HasValue
? DiffTypeToString(diffType.Value)
: null;
}

#endregion

#region TagTypes
Expand Down
30 changes: 0 additions & 30 deletions src/Bitbucket.Net/Common/DynamicDictionary.cs

This file was deleted.

19 changes: 19 additions & 0 deletions src/Bitbucket.Net/Common/FlurlRequestExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using System;
using Flurl.Http;

namespace Bitbucket.Net.Common
{
public static class FlurlRequestExtensions
{
public static IFlurlRequest WithAuthentication(this IFlurlRequest request, Func<string> getToken, string userName, string password)
{
if (getToken != null)
{
string token = getToken();
return request.WithOAuthBearerToken(token);
}

return request.WithBasicAuth(userName, password);
}
}
}
14 changes: 7 additions & 7 deletions src/Bitbucket.Net/Core/Admin/BitbucketClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public async Task<DeletableGroupOrUser> CreateAdminGroupAsync(string name)
{
var response = await GetAdminUrl("/groups")
.SetQueryParam("name", name)
.PostAsync(new StringContent(""))
.PostJsonAsync(new StringContent(""))
.ConfigureAwait(false);

return await HandleResponseAsync<DeletableGroupOrUser>(response).ConfigureAwait(false);
Expand Down Expand Up @@ -144,23 +144,23 @@ public async Task<bool> CreateAdminUserAsync(string name, string password, strin

var response = await GetAdminUrl("/users")
.SetQueryParams(queryParamValues)
.PostAsync(new StringContent(""))
.PostJsonAsync(new StringContent(""))
.ConfigureAwait(false);

return await HandleResponseAsync(response).ConfigureAwait(false);
}

public async Task<UserInfo> UpdateAdminUserAsync(string name = null, string displayName = null, string emailAddress = null)
{
var data = new DynamicDictionary
var data = new
{
{ name, "name" },
{ displayName, "displayName" },
{ emailAddress, "email" }
name,
displayName,
email = emailAddress
};

var response = await GetAdminUrl("/users")
.PutJsonAsync(data.ToDictionary())
.PutJsonAsync(data)
.ConfigureAwait(false);

return await HandleResponseAsync<UserInfo>(response).ConfigureAwait(false);
Expand Down
2 changes: 1 addition & 1 deletion src/Bitbucket.Net/Core/Markup/BitbucketClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public async Task<string> PreviewMarkupAsync(string text,
var response = await GetMarkupUrl("/preview")
.WithHeader("X-Atlassian-Token", "no-check")
.SetQueryParams(queryParamValues)
.PostAsync(new StringContent(text))
.PostJsonAsync(new StringContent(text))
.ConfigureAwait(false);

return await HandleResponseAsync<string>(response, s =>
Expand Down
Loading

0 comments on commit e062514

Please sign in to comment.