Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e8b2a0e
commit fafda59
Showing
20 changed files
with
15,245 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> |
1 change: 1 addition & 0 deletions
1
sdk/DotNet Core/JsonApi/obj/Debug/netcoreapp1.1/CoreCompileInputs.cache
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 @@ | ||
e73fe7e18aea0d48ea5dab60bc98e71a8f7abff4 |
Empty file.
Empty file.
Empty file.
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,18 @@ | ||
<?xml version="1.0" encoding="utf-8" standalone="no"?> | ||
<Project ToolsVersion="14.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> | ||
<PropertyGroup Condition=" '$(ExcludeRestorePackageImports)' != 'true' "> | ||
<RestoreSuccess Condition=" '$(RestoreSuccess)' == '' ">True</RestoreSuccess> | ||
<RestoreTool Condition=" '$(RestoreTool)' == '' ">NuGet</RestoreTool> | ||
<ProjectAssetsFile Condition=" '$(ProjectAssetsFile)' == '' ">F:\jsonapi\sdk\DotNet Core\JsonApi\obj\project.assets.json</ProjectAssetsFile> | ||
<NuGetPackageRoot Condition=" '$(NuGetPackageRoot)' == '' ">$(UserProfile)\.nuget\packages\</NuGetPackageRoot> | ||
<NuGetPackageFolders Condition=" '$(NuGetPackageFolders)' == '' ">C:\Users\Administrator\.nuget\packages\</NuGetPackageFolders> | ||
<NuGetProjectStyle Condition=" '$(NuGetProjectStyle)' == '' ">PackageReference</NuGetProjectStyle> | ||
<NuGetToolVersion Condition=" '$(NuGetToolVersion)' == '' ">4.2.0</NuGetToolVersion> | ||
</PropertyGroup> | ||
<PropertyGroup> | ||
<MSBuildAllProjects>$(MSBuildAllProjects);$(MSBuildThisFileFullPath)</MSBuildAllProjects> | ||
</PropertyGroup> | ||
<ImportGroup Condition=" '$(ExcludeRestorePackageImports)' != 'true' "> | ||
<Import Project="$(NuGetPackageRoot)microsoft.diasymreader.native\1.4.1\build\Microsoft.DiaSymReader.Native.props" Condition="Exists('$(NuGetPackageRoot)microsoft.diasymreader.native\1.4.1\build\Microsoft.DiaSymReader.Native.props')" /> | ||
</ImportGroup> | ||
</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,6 @@ | ||
<?xml version="1.0" encoding="utf-8" standalone="no"?> | ||
<Project ToolsVersion="14.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> | ||
<PropertyGroup> | ||
<MSBuildAllProjects>$(MSBuildAllProjects);$(MSBuildThisFileFullPath)</MSBuildAllProjects> | ||
</PropertyGroup> | ||
</Project> |
Oops, something went wrong.