-
Notifications
You must be signed in to change notification settings - Fork 86
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #505 from Tuisku-L/master
Add .Net Core API.
- Loading branch information
Showing
6 changed files
with
214 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,10 +4,11 @@ builds/ | |
target/ | ||
release/ | ||
bin/ | ||
.vs/ | ||
deps/craftbukkit.jar | ||
*~ | ||
*.lock | ||
*.DS_Store | ||
*.swp | ||
*.out | ||
!*.dll | ||
!*.elt |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
|
||
Microsoft Visual Studio Solution File, Format Version 12.00 | ||
# Visual Studio 15 | ||
VisualStudioVersion = 15.0.26430.4 | ||
MinimumVisualStudioVersion = 10.0.40219.1 | ||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "JsonApi", "JsonApi\JsonApi.csproj", "{7FCD017D-A07B-499B-936B-C37F50BD99C1}" | ||
EndProject | ||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "JsonApiTest", "JsonApiTest\JsonApiTest.csproj", "{52A0D2EC-33DA-49A1-9AC0-96D477F30327}" | ||
EndProject | ||
Global | ||
GlobalSection(SolutionConfigurationPlatforms) = preSolution | ||
Debug|Any CPU = Debug|Any CPU | ||
Release|Any CPU = Release|Any CPU | ||
EndGlobalSection | ||
GlobalSection(ProjectConfigurationPlatforms) = postSolution | ||
{7FCD017D-A07B-499B-936B-C37F50BD99C1}.Debug|Any CPU.ActiveCfg = Debug|Any CPU | ||
{7FCD017D-A07B-499B-936B-C37F50BD99C1}.Debug|Any CPU.Build.0 = Debug|Any CPU | ||
{7FCD017D-A07B-499B-936B-C37F50BD99C1}.Release|Any CPU.ActiveCfg = Release|Any CPU | ||
{7FCD017D-A07B-499B-936B-C37F50BD99C1}.Release|Any CPU.Build.0 = Release|Any CPU | ||
{52A0D2EC-33DA-49A1-9AC0-96D477F30327}.Debug|Any CPU.ActiveCfg = Debug|Any CPU | ||
{52A0D2EC-33DA-49A1-9AC0-96D477F30327}.Debug|Any CPU.Build.0 = Debug|Any CPU | ||
{52A0D2EC-33DA-49A1-9AC0-96D477F30327}.Release|Any CPU.ActiveCfg = Release|Any CPU | ||
{52A0D2EC-33DA-49A1-9AC0-96D477F30327}.Release|Any CPU.Build.0 = Release|Any CPU | ||
EndGlobalSection | ||
GlobalSection(SolutionProperties) = preSolution | ||
HideSolutionNode = FALSE | ||
EndGlobalSection | ||
EndGlobal |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,143 @@ | ||
using Newtonsoft.Json; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.Net; | ||
using System.Security.Cryptography; | ||
using System.Text; | ||
|
||
namespace JsonApi | ||
{ | ||
public class ApiHelper | ||
{ | ||
private const string UrlFormat = "http://{0}:{1}/api/2/call?json={2}"; | ||
|
||
public ApiHelper(string host, int port, string userName, string password, string salt = "") | ||
{ | ||
_host = host; | ||
Port = port; | ||
_userName = userName; | ||
_password = password; | ||
_salt = salt; | ||
} | ||
|
||
private readonly string _host; | ||
private readonly string _userName; | ||
private readonly string _password; | ||
private readonly string _salt; | ||
public readonly int Port; | ||
|
||
private string GetKey(string method) | ||
{ | ||
return BitConverter.ToString( | ||
SHA256.Create().ComputeHash(Encoding.UTF8.GetBytes(_userName + method + _password + _salt))) | ||
.Replace("-", "").ToLower(); | ||
} | ||
|
||
private string MakeApiUrl(string method, object[] args) | ||
{ | ||
return string.Format(UrlFormat, _host, Port, UrlEncode(ConstructCall(method, args))); | ||
} | ||
|
||
private string MakeApiUrl(string[] method, string[][] args) | ||
{ | ||
return string.Format(UrlFormat, _host, Port, UrlEncode(ConstructCall(method, args))); | ||
} | ||
|
||
private string ConstructCall(string method, object[] args) | ||
{ | ||
var json = new | ||
{ | ||
name = method, | ||
arguments = args, | ||
key = GetKey(method), | ||
username = _userName | ||
}; | ||
|
||
return JsonConvert.SerializeObject(json); | ||
} | ||
|
||
private string ConstructCall(string[] method, string[][] args) | ||
{ | ||
var allCalls = new List<object>(); | ||
for (var i = 0; i < method.Length; i++) | ||
{ | ||
allCalls.Add( | ||
new | ||
{ | ||
name = method[i], | ||
arguments = args[i], | ||
key = GetKey(method[i]), | ||
username = _userName | ||
} | ||
); | ||
} | ||
|
||
return JsonConvert.SerializeObject(allCalls); | ||
} | ||
|
||
/// <summary> | ||
/// Call a method on the server. | ||
/// </summary> | ||
/// <param name="method">The method to call.</param> | ||
/// <param name="args">Arguments to pass to the server.</param> | ||
/// <returns></returns> | ||
public string Call(string method, object[] args) | ||
{ | ||
var url = MakeApiUrl(method, args); | ||
|
||
return Get(url); | ||
} | ||
|
||
/// <summary> | ||
/// Call multiple methods on the server. | ||
/// </summary> | ||
/// <param name="method">A string[] of methods to call on the server.</param> | ||
/// <param name="args">Arguments to pass to the server.</param> | ||
/// <returns></returns> | ||
public string Call(string[] method, string[][] args) | ||
{ | ||
if (method.Length != args.Length) | ||
{ | ||
throw new ArgumentOutOfRangeException(); | ||
} | ||
|
||
var url = MakeApiUrl(method, args); | ||
|
||
return Get(url); | ||
} | ||
|
||
private static string Get(string url) | ||
{ | ||
var request = (HttpWebRequest)WebRequest.Create(url); | ||
request.Method = "GET"; | ||
request.ContentType = "text/html;charset=UTF-8"; | ||
|
||
var response = (HttpWebResponse)request.GetResponseAsync().Result; | ||
var myResponseStream = response.GetResponseStream(); | ||
var myStreamReader = new StreamReader(myResponseStream, Encoding.UTF8); | ||
var retString = myStreamReader.ReadToEnd(); | ||
myStreamReader.Dispose(); | ||
myResponseStream.Dispose(); | ||
|
||
return retString; | ||
} | ||
|
||
/// <summary> | ||
/// UrlEncode | ||
/// </summary> | ||
/// <param name="str"></param> | ||
/// <returns></returns> | ||
private static string UrlEncode(string str) | ||
{ | ||
var sb = new StringBuilder(); | ||
var byStr = Encoding.UTF8.GetBytes(str); | ||
for (var i = 0; i < byStr.Length; i++) | ||
{ | ||
sb.Append(@"%" + Convert.ToString(i, 16)); | ||
} | ||
|
||
return (sb.ToString()); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>netcoreapp1.1</TargetFramework> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<Compile Remove="JsonAPI.cs" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Newtonsoft.Json" Version="10.0.3" /> | ||
</ItemGroup> | ||
|
||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<OutputType>Exe</OutputType> | ||
<TargetFramework>netcoreapp1.1</TargetFramework> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\JsonApi\JsonApi.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
using JsonApi; | ||
using System; | ||
|
||
namespace JsonApiTest | ||
{ | ||
class Program | ||
{ | ||
static void Main(string[] args) | ||
{ | ||
var j = new ApiHelper("192.168.0.197", 25565, "admin", "changeme"); | ||
Console.WriteLine(j.Call("players.name", new object[] { "Tuisku" })); | ||
} | ||
} | ||
} |