Skip to content

Commit

Permalink
Add .Net Core API.
Browse files Browse the repository at this point in the history
  • Loading branch information
tuiskuwood committed Jun 23, 2017
1 parent e8b2a0e commit fafda59
Show file tree
Hide file tree
Showing 20 changed files with 15,245 additions and 1 deletion.
3 changes: 2 additions & 1 deletion .gitignore
Expand Up @@ -4,10 +4,11 @@ builds/
target/
release/
bin/
.vs/
deps/craftbukkit.jar
*~
*.lock
*.DS_Store
*.swp
*.out
!*.dll
!*.elt
28 changes: 28 additions & 0 deletions sdk/DotNet Core/JsonApi.sln
@@ -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
143 changes: 143 additions & 0 deletions sdk/DotNet Core/JsonApi/ApiHelper.cs
@@ -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());
}
}
}
15 changes: 15 additions & 0 deletions sdk/DotNet Core/JsonApi/JsonApi.csproj
@@ -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>
@@ -0,0 +1 @@
e73fe7e18aea0d48ea5dab60bc98e71a8f7abff4
18 changes: 18 additions & 0 deletions sdk/DotNet Core/JsonApi/obj/JsonApi.csproj.nuget.g.props
@@ -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>
6 changes: 6 additions & 0 deletions sdk/DotNet Core/JsonApi/obj/JsonApi.csproj.nuget.g.targets
@@ -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>

0 comments on commit fafda59

Please sign in to comment.