-
Notifications
You must be signed in to change notification settings - Fork 0
/
Semantics3.cs
139 lines (116 loc) · 5.28 KB
/
Semantics3.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#region License
//The MIT License
//Copyright (c) 2013 Semantics3, Inc. https://semantics3.com
//Permission is hereby granted, free of charge, to any person obtaining a copy
//of this software and associated documentation files (the "Software"), to deal
//in the Software without restriction, including without limitation the rights
//to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//copies of the Software, and to permit persons to whom the Software is
//furnished to do so, subject to the following conditions:
//The above copyright notice and this permission notice shall be included in
//all copies or substantial portions of the Software.
//THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
//THE SOFTWARE.
/******************************************************************************
*
* Name: Semantics3.cs
* Project: Semantics3 API C# Client
* Purpose: C# Client to query Semantics3 APIs.
* Author: Srinivasan Kidambi, [email protected]
*
******************************************************************************/
#endregion
using System;
using System.Collections.Generic;
using System.IO;
using System.Web;
using System.Net;
using System.Linq;
using System.Text;
using Google.GData.Client;
namespace Semantics3
{
public class Semantics3
{
private readonly String api_key;
private readonly String api_secret;
private readonly String api_domain;
private readonly String api_base;
private Uri _serviceProviderUri;
OAuth2LeggedAuthenticator oauth_client;
/// <summary>
/// Constructs the Semantics3 object by initializing the api key and secret.
/// Base for API Client interfacing with the Semantics3 APIs
/// Semantics3 sem3 = new Semantics3("API_KEY", "API_SECRET");
/// </summary>
/// <param name="apiKey">API Key from semantics3.com</param>
/// <param name="apiSecret">API Secret from semantics3.com</param>
/// <returns>void.</returns>
public Semantics3(String consumerKey, String consumerSecret, String _custom_api_base = "")
{
api_domain = "api.semantics3.com";
api_base = "https://" + api_domain + "/v1/";
// If custom api base sent in, then use that
if (_custom_api_base != null && _custom_api_base != "")
{
api_base = _custom_api_base;
}
api_key = consumerKey;
api_secret = consumerSecret;
oauth_client = new OAuth2LeggedAuthenticator("AppName", consumerKey, consumerSecret);
}
public String fetch(String endpoint, String parameters, String method)
{
String url = api_base + endpoint;
// If method is GET, then send parameters as part of URL
if (method == "GET")
{
url = url + "?q=" + System.Web.HttpUtility.UrlEncode(parameters);
}
_serviceProviderUri = new Uri(url);
// HttpWebRequest request = GenerateRequest("application/json", "GET");
HttpWebRequest request = oauth_client.CreateHttpWebRequest(method, _serviceProviderUri);
// If not GET request, send parameters as request content
if (method != "GET" && parameters.Length > 0)
{
var data = Encoding.ASCII.GetBytes(parameters);
request.Method = method;
request.ContentType = "application/json";
request.ContentLength = data.Length;
using (Stream stream = request.GetRequestStream())
{
stream.Write(data, 0, data.Length);
}
}
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
// Get the stream associated with the response.
Stream receiveStream = response.GetResponseStream();
// Pipes the stream to a higher level stream reader with the required encoding format.
StreamReader readStream = new StreamReader(receiveStream, Encoding.UTF8);
String result = readStream.ReadToEnd();
response.Close();
readStream.Close();
return result;
}
}
class OAuth2LeggedAuthenticator : OAuthAuthenticator
{
public OAuth2LeggedAuthenticator(string applicationName, string consumerKey, string consumerSecret)
: base(applicationName, consumerKey, consumerSecret)
{
}
public override void ApplyAuthenticationToRequest(HttpWebRequest request)
{
base.ApplyAuthenticationToRequest(request);
string userAgent = "Semantics3 C# Lib/1.0.0.24";
string header = OAuthUtil.GenerateHeader(request.RequestUri, ConsumerKey, ConsumerSecret, null, null, request.Method);
request.Headers.Add(header);
request.UserAgent = userAgent;
}
}
}