Skip to content
This repository was archived by the owner on Apr 21, 2022. It is now read-only.

Commit 290385a

Browse files
committed
Initial commit
0 parents  commit 290385a

File tree

9 files changed

+620
-0
lines changed

9 files changed

+620
-0
lines changed

.gitignore

Lines changed: 216 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,216 @@
1+
## Ignore Visual Studio temporary files, build results, and
2+
## files generated by popular Visual Studio add-ons.
3+
4+
# User-specific files
5+
*.suo
6+
*.user
7+
*.userosscache
8+
*.sln.docstates
9+
10+
# User-specific files (MonoDevelop/Xamarin Studio)
11+
*.userprefs
12+
13+
# Build results
14+
[Dd]ebug/
15+
[Dd]ebugPublic/
16+
[Rr]elease/
17+
[Rr]eleases/
18+
x64/
19+
x86/
20+
build/
21+
bld/
22+
[Bb]in/
23+
[Oo]bj/
24+
25+
# Visual Studio 2015 cache/options directory
26+
.vs/
27+
# Uncomment if you have tasks that create the project's static files in wwwroot
28+
#wwwroot/
29+
30+
# MSTest test Results
31+
[Tt]est[Rr]esult*/
32+
[Bb]uild[Ll]og.*
33+
34+
# NUNIT
35+
*.VisualState.xml
36+
TestResult.xml
37+
38+
# Build Results of an ATL Project
39+
[Dd]ebugPS/
40+
[Rr]eleasePS/
41+
dlldata.c
42+
43+
# DNX
44+
project.lock.json
45+
artifacts/
46+
47+
*_i.c
48+
*_p.c
49+
*_i.h
50+
*.ilk
51+
*.meta
52+
*.obj
53+
*.pch
54+
*.pdb
55+
*.pgc
56+
*.pgd
57+
*.rsp
58+
*.sbr
59+
*.tlb
60+
*.tli
61+
*.tlh
62+
*.tmp
63+
*.tmp_proj
64+
*.log
65+
*.vspscc
66+
*.vssscc
67+
.builds
68+
*.pidb
69+
*.svclog
70+
*.scc
71+
72+
# Chutzpah Test files
73+
_Chutzpah*
74+
75+
# Visual C++ cache files
76+
ipch/
77+
*.aps
78+
*.ncb
79+
*.opensdf
80+
*.sdf
81+
*.cachefile
82+
83+
# Visual Studio profiler
84+
*.psess
85+
*.vsp
86+
*.vspx
87+
88+
# TFS 2012 Local Workspace
89+
$tf/
90+
91+
# Guidance Automation Toolkit
92+
*.gpState
93+
94+
# ReSharper is a .NET coding add-in
95+
_ReSharper*/
96+
*.[Rr]e[Ss]harper
97+
*.DotSettings.user
98+
99+
# JustCode is a .NET coding add-in
100+
.JustCode
101+
102+
# TeamCity is a build add-in
103+
_TeamCity*
104+
105+
# DotCover is a Code Coverage Tool
106+
*.dotCover
107+
108+
# NCrunch
109+
_NCrunch_*
110+
.*crunch*.local.xml
111+
nCrunchTemp_*
112+
113+
# MightyMoose
114+
*.mm.*
115+
AutoTest.Net/
116+
117+
# Web workbench (sass)
118+
.sass-cache/
119+
120+
# Installshield output folder
121+
[Ee]xpress/
122+
123+
# DocProject is a documentation generator add-in
124+
DocProject/buildhelp/
125+
DocProject/Help/*.HxT
126+
DocProject/Help/*.HxC
127+
DocProject/Help/*.hhc
128+
DocProject/Help/*.hhk
129+
DocProject/Help/*.hhp
130+
DocProject/Help/Html2
131+
DocProject/Help/html
132+
133+
# Click-Once directory
134+
publish/
135+
136+
# Publish Web Output
137+
*.[Pp]ublish.xml
138+
*.azurePubxml
139+
# TODO: Comment the next line if you want to checkin your web deploy settings
140+
# but database connection strings (with potential passwords) will be unencrypted
141+
*.pubxml
142+
*.publishproj
143+
144+
# NuGet Packages
145+
*.nupkg
146+
# The packages folder can be ignored because of Package Restore
147+
**/packages/*
148+
# except build/, which is used as an MSBuild target.
149+
!**/packages/build/
150+
# Uncomment if necessary however generally it will be regenerated when needed
151+
#!**/packages/repositories.config
152+
153+
# Windows Azure Build Output
154+
csx/
155+
*.build.csdef
156+
157+
# Windows Store app package directory
158+
AppPackages/
159+
160+
# Visual Studio cache files
161+
# files ending in .cache can be ignored
162+
*.[Cc]ache
163+
# but keep track of directories ending in .cache
164+
!*.[Cc]ache/
165+
166+
# Others
167+
ClientBin/
168+
[Ss]tyle[Cc]op.*
169+
~$*
170+
*~
171+
*.dbmdl
172+
*.dbproj.schemaview
173+
*.pfx
174+
*.publishsettings
175+
node_modules/
176+
orleans.codegen.cs
177+
178+
# RIA/Silverlight projects
179+
Generated_Code/
180+
181+
# Backup & report files from converting an old project file
182+
# to a newer Visual Studio version. Backup files are not needed,
183+
# because we have git ;-)
184+
_UpgradeReport_Files/
185+
Backup*/
186+
UpgradeLog*.XML
187+
UpgradeLog*.htm
188+
189+
# SQL Server files
190+
*.mdf
191+
*.ldf
192+
193+
# Business Intelligence projects
194+
*.rdl.data
195+
*.bim.layout
196+
*.bim_*.settings
197+
198+
# Microsoft Fakes
199+
FakesAssemblies/
200+
201+
# Node.js Tools for Visual Studio
202+
.ntvs_analysis.dat
203+
204+
# Visual Studio 6 build log
205+
*.plg
206+
207+
# Visual Studio 6 workspace options file
208+
*.opt
209+
210+
# Visual Studio LightSwitch build output
211+
**/*.HTMLClient/GeneratedArtifacts
212+
**/*.DesktopClient/GeneratedArtifacts
213+
**/*.DesktopClient/ModelManifest.xml
214+
**/*.Server/GeneratedArtifacts
215+
**/*.Server/ModelManifest.xml
216+
_Pvt_Extensions

api-example-csharp.sln

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio 14
4+
VisualStudioVersion = 14.0.23107.0
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "api-example-csharp", "api-example-csharp\api-example-csharp.csproj", "{EB2EA619-3408-4397-B584-150EB97F395C}"
7+
EndProject
8+
Global
9+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
10+
Debug|Any CPU = Debug|Any CPU
11+
Release|Any CPU = Release|Any CPU
12+
EndGlobalSection
13+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
14+
{EB2EA619-3408-4397-B584-150EB97F395C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{EB2EA619-3408-4397-B584-150EB97F395C}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{EB2EA619-3408-4397-B584-150EB97F395C}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{EB2EA619-3408-4397-B584-150EB97F395C}.Release|Any CPU.Build.0 = Release|Any CPU
18+
EndGlobalSection
19+
GlobalSection(SolutionProperties) = preSolution
20+
HideSolutionNode = FALSE
21+
EndGlobalSection
22+
EndGlobal

api-example-csharp/ApiClient.cs

Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Net;
4+
using RestSharp;
5+
6+
7+
namespace ApiTest
8+
{
9+
public class ApiException : Exception
10+
{
11+
public ApiException() : base() { }
12+
public ApiException(string message) : base(message) { }
13+
public ApiException(string message, Exception e) : base(message, e) { }
14+
}
15+
16+
17+
public class ApiClient
18+
{
19+
const string BaseUrl = "https://127.0.0.1:10880/api/v1";
20+
readonly public string Company;
21+
RestClient _client;
22+
readonly string _username;
23+
readonly string _password;
24+
25+
public ApiClient(string company, string username, string password)
26+
{
27+
Company = company;
28+
_username = username;
29+
_password = password;
30+
_client = Client();
31+
}
32+
33+
RestClient Client()
34+
{
35+
// Accept self-signed certificate
36+
ServicePointManager.ServerCertificateValidationCallback +=
37+
(sender, certificate, chain, sslPolicyErrors) => true;
38+
39+
var client = new RestClient();
40+
client.BaseUrl = new Uri(BaseUrl);
41+
client.Authenticator = new HttpBasicAuthenticator(_username, _password);
42+
client.FollowRedirects = false;
43+
return client;
44+
}
45+
46+
public T Execute<T>(RestRequest request) where T : new()
47+
{
48+
var response = _client.Execute<T>(request);
49+
50+
HttpStatusCode code = response.StatusCode;
51+
if (code == HttpStatusCode.Unauthorized)
52+
throw new ApiException("Username or password incorrect");
53+
54+
if (response.ErrorException != null)
55+
throw new ApplicationException(response.ErrorMessage,
56+
response.ErrorException);
57+
58+
if (code == HttpStatusCode.OK || code == HttpStatusCode.NoContent)
59+
return response.Data;
60+
61+
if (code == HttpStatusCode.Created)
62+
{
63+
// Manually follow header redirect
64+
foreach (var p in response.Headers)
65+
{
66+
if (p.Name == "Location")
67+
{
68+
var second_request = new RestRequest();
69+
var location = p.Value.ToString();
70+
second_request.Resource = location.Remove(0,
71+
_client.BaseUrl.ToString().Length);
72+
return Execute<T>(second_request);
73+
}
74+
}
75+
}
76+
77+
throw new ApiException(response.Content);
78+
}
79+
}
80+
81+
82+
public abstract class BaseObjectClient
83+
{
84+
protected ApiClient Client;
85+
86+
public BaseObjectClient(ApiClient client)
87+
{
88+
Client = client;
89+
}
90+
91+
string BaseResource
92+
{
93+
get
94+
{
95+
return string.Format("companies/{0}/", Client.Company);
96+
}
97+
}
98+
99+
abstract public string Resource { get; }
100+
101+
public List<T> List<T>()
102+
{
103+
var request = new RestRequest();
104+
request.RequestFormat = DataFormat.Json;
105+
request.Resource = BaseResource + Resource;
106+
request.RootElement = "records";
107+
return Client.Execute<List<T>>(request);
108+
}
109+
110+
public T Fetch<T>(int id) where T : new()
111+
{
112+
var request = new RestRequest();
113+
request.RequestFormat = DataFormat.Json;
114+
request.Resource = BaseResource + Resource + id;
115+
return Client.Execute<T>(request);
116+
}
117+
118+
public T Create<T>(T obj) where T : new()
119+
{
120+
var request = new RestRequest(Method.POST);
121+
request.RequestFormat = DataFormat.Json;
122+
request.Resource = BaseResource + Resource;
123+
request.AddBody(obj);
124+
return Client.Execute<T>(request);
125+
}
126+
127+
public T Update<T>(int id, T obj) where T : new()
128+
{
129+
var request = new RestRequest(Method.PUT);
130+
request.RequestFormat = DataFormat.Json;
131+
request.Resource = BaseResource + Resource + id;
132+
request.AddBody(obj);
133+
return Client.Execute<T>(request);
134+
}
135+
136+
public void Delete(int id)
137+
{
138+
var request = new RestRequest(Method.DELETE);
139+
request.RequestFormat = DataFormat.Json;
140+
request.Resource = BaseResource + Resource + id;
141+
Client.Execute<ErrorResponse>(request);
142+
}
143+
}
144+
145+
146+
public class ErrorResponse
147+
{
148+
public string message { get; set; }
149+
public string error_type { get; set; }
150+
public string traceback { get; set; }
151+
}
152+
}

0 commit comments

Comments
 (0)