Skip to content

Commit

Permalink
add files, wip #1
Browse files Browse the repository at this point in the history
  • Loading branch information
marihachi committed May 8, 2018
1 parent 4eb4935 commit b3ca5cd
Show file tree
Hide file tree
Showing 8 changed files with 208 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
obj
bin
.vs
13 changes: 13 additions & 0 deletions FrostSDK.Example/FrostSDK.Example.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<Project Sdk="Microsoft.NET.Sdk">

<ItemGroup>
<ProjectReference Include="..\FrostSDK\FrostSDK.csproj" />
</ItemGroup>

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp2.0</TargetFramework>
<LangVersion>7.1</LangVersion>
</PropertyGroup>

</Project>
37 changes: 37 additions & 0 deletions FrostSDK.Example/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Threading.Tasks;

namespace FrostSDK.Example
{
class Program
{
static async Task Main(string[] args)
{
var apiUrl = "http://localhost:8000";
var accessToken = "set_your_access_token";

var requester = new HttpApiRequester(apiUrl);

while (true)
{
Console.Write("投稿内容(exit=終了) > ");
var text = Console.ReadLine();
if (text == "exit") break;

Console.Write("ポストを投稿しています...");
var res = await requester.RequestAsync(HttpMethod.Post, "/posts/post_status", accessToken, new Dictionary<string, string> { ["text"] = text + text });
if (res.StatusCode == 200)
{
Console.WriteLine("成功しました!");
}
else
{
Console.WriteLine("失敗しました。。");
Console.WriteLine(res.ContentJson);
}
}
}
}
}
14 changes: 14 additions & 0 deletions FrostSDK/ApiResponse.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
namespace FrostSDK
{
public class ApiResponse
{
public ApiResponse(int statusCode, string contentJson = null)
{
StatusCode = statusCode;
ContentJson = contentJson;
}

public int StatusCode { get; set; }
public string ContentJson { get; set; }
}
}
11 changes: 11 additions & 0 deletions FrostSDK/FrostSDK.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Newtonsoft.Json" Version="11.0.2" />
</ItemGroup>

</Project>
66 changes: 66 additions & 0 deletions FrostSDK/HttpApiRequester.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Threading.Tasks;
using System.Web;

namespace FrostSDK
{
public class HttpApiRequester : IApiRequester
{
public HttpApiRequester(string apiUrl, HttpClient httpClient = null)
{
ApiUrl = new Uri(apiUrl);
_HttpHandler = new HttpClientHandler { UseCookies = false };
_Http = httpClient ?? new HttpClient(_HttpHandler);
}

private HttpClient _Http { get; set; }
private HttpClientHandler _HttpHandler { get; set; }
public Uri ApiUrl { get; set; }

public async Task<ApiResponse> RequestAsync(HttpMethod method, string endpoint, string accessToken, Dictionary<string, string> parameters = null, Dictionary<string, string> headers = null)
{
var bodyMode = method != HttpMethod.Get && method != HttpMethod.Head;

parameters = parameters ?? new Dictionary<string, string>();
headers = headers ?? new Dictionary<string, string>();

var query = "";

if (!bodyMode)
{
var queryPairs = parameters.Select(p => HttpUtility.UrlEncode(p.Key) + "=" + HttpUtility.UrlEncode(p.Value));
var queryContent = string.Join("&", queryPairs);
query = "?" + queryContent;
}

if (endpoint[0] == '/')
endpoint = endpoint.Remove(0, 1);

var request = new HttpRequestMessage(method, ApiUrl + endpoint + query);
foreach(var header in headers)
{
request.Headers.Add(header.Key, header.Value);
}
request.Headers.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", accessToken);
if (bodyMode)
{
var bodyJson = Newtonsoft.Json.JsonConvert.SerializeObject(parameters);
request.Content = new StringContent(bodyJson);
request.Content.Headers.ContentType.MediaType = "application/json";
}

var response = await _Http.SendAsync(request);
var responseJson = await response.Content.ReadAsStringAsync();

if (response.Content.Headers.ContentType.MediaType.ToLower() != "application/json")
{
throw new Exception("response is not json format");
}

return new ApiResponse((int)response.StatusCode, responseJson);
}
}
}
11 changes: 11 additions & 0 deletions FrostSDK/IApiRequester.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using System.Collections.Generic;
using System.Net.Http;
using System.Threading.Tasks;

namespace FrostSDK
{
public interface IApiRequester
{
Task<ApiResponse> RequestAsync(HttpMethod method, string endpoint, string accessToken, Dictionary<string, string> parameters = null, Dictionary<string, string> headers = null);
}
}
53 changes: 53 additions & 0 deletions frost-sdk-dotnet.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 15
VisualStudioVersion = 15.0.26124.0
MinimumVisualStudioVersion = 15.0.26124.0
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "FrostSDK", "FrostSDK\FrostSDK.csproj", "{53785701-9DCE-403A-A480-4E8CA8FCB1B1}"
ProjectSection(ProjectDependencies) = postProject
{9F5C6FAE-0E72-4497-B672-705912539D91} = {9F5C6FAE-0E72-4497-B672-705912539D91}
EndProjectSection
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "FrostSDK.Example", "FrostSDK.Example\FrostSDK.Example.csproj", "{9F5C6FAE-0E72-4497-B672-705912539D91}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Debug|x64 = Debug|x64
Debug|x86 = Debug|x86
Release|Any CPU = Release|Any CPU
Release|x64 = Release|x64
Release|x86 = Release|x86
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{53785701-9DCE-403A-A480-4E8CA8FCB1B1}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{53785701-9DCE-403A-A480-4E8CA8FCB1B1}.Debug|Any CPU.Build.0 = Debug|Any CPU
{53785701-9DCE-403A-A480-4E8CA8FCB1B1}.Debug|x64.ActiveCfg = Debug|Any CPU
{53785701-9DCE-403A-A480-4E8CA8FCB1B1}.Debug|x64.Build.0 = Debug|Any CPU
{53785701-9DCE-403A-A480-4E8CA8FCB1B1}.Debug|x86.ActiveCfg = Debug|Any CPU
{53785701-9DCE-403A-A480-4E8CA8FCB1B1}.Debug|x86.Build.0 = Debug|Any CPU
{53785701-9DCE-403A-A480-4E8CA8FCB1B1}.Release|Any CPU.ActiveCfg = Release|Any CPU
{53785701-9DCE-403A-A480-4E8CA8FCB1B1}.Release|Any CPU.Build.0 = Release|Any CPU
{53785701-9DCE-403A-A480-4E8CA8FCB1B1}.Release|x64.ActiveCfg = Release|Any CPU
{53785701-9DCE-403A-A480-4E8CA8FCB1B1}.Release|x64.Build.0 = Release|Any CPU
{53785701-9DCE-403A-A480-4E8CA8FCB1B1}.Release|x86.ActiveCfg = Release|Any CPU
{53785701-9DCE-403A-A480-4E8CA8FCB1B1}.Release|x86.Build.0 = Release|Any CPU
{9F5C6FAE-0E72-4497-B672-705912539D91}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{9F5C6FAE-0E72-4497-B672-705912539D91}.Debug|Any CPU.Build.0 = Debug|Any CPU
{9F5C6FAE-0E72-4497-B672-705912539D91}.Debug|x64.ActiveCfg = Debug|Any CPU
{9F5C6FAE-0E72-4497-B672-705912539D91}.Debug|x64.Build.0 = Debug|Any CPU
{9F5C6FAE-0E72-4497-B672-705912539D91}.Debug|x86.ActiveCfg = Debug|Any CPU
{9F5C6FAE-0E72-4497-B672-705912539D91}.Debug|x86.Build.0 = Debug|Any CPU
{9F5C6FAE-0E72-4497-B672-705912539D91}.Release|Any CPU.ActiveCfg = Release|Any CPU
{9F5C6FAE-0E72-4497-B672-705912539D91}.Release|Any CPU.Build.0 = Release|Any CPU
{9F5C6FAE-0E72-4497-B672-705912539D91}.Release|x64.ActiveCfg = Release|Any CPU
{9F5C6FAE-0E72-4497-B672-705912539D91}.Release|x64.Build.0 = Release|Any CPU
{9F5C6FAE-0E72-4497-B672-705912539D91}.Release|x86.ActiveCfg = Release|Any CPU
{9F5C6FAE-0E72-4497-B672-705912539D91}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {E11D9B07-54AB-4CFD-9CC1-1B5E49EF8E25}
EndGlobalSection
EndGlobal

0 comments on commit b3ca5cd

Please sign in to comment.