-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathProgram.cs
executable file
·132 lines (108 loc) · 4.56 KB
/
Program.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
using System;
using System.Collections.Generic;
using CS_threescale;
using System.Text;
using System.Net;
using System.Collections;
namespace ConsoleDemo
{
class Program
{
static void print(AuthorizeResponse ar)
{
if (ar.authorized)
{
Console.WriteLine("Authorized!!");
}
else
{
Console.WriteLine("NOT Authorized!!" + ar.reason);
}
Console.WriteLine("PLAN: " + ar.plan);
Console.WriteLine("#usages: " + ar.usages.Count);
int i = 0;
foreach (UsageItem item in ar.usages)
{
Console.WriteLine("Usage: " + i);
Console.WriteLine(" Metric: " + item.metric);
Console.WriteLine(" Period: " + item.period);
Console.WriteLine(" CurrValue: " + item.current_value);
Console.WriteLine(" MaxValue: " + item.max_value);
Console.WriteLine(" PeriodStart:" + item.period_start);
Console.WriteLine(" PeriodEnd: " + item.period_start);
i++;
}
}
static void Main(string[] args)
{
try
{
string provider_key = "YOUR_PROVIDER_KEY";
// Add your service_id
string service_id = "YOUR_SERVICE_ID";
// Authorise using app_id (and app_key if necessary)
string app_id = "YOUR_APP_ID";
string app_key = "YOUR_APP_KEY";
string backend = "http://su1.3scale.net:80";
// Or alternatively you can authorise using user_key
// string user_key = "YOUR_USER_KEY";
Api _3ScaleAPI = new Api(backend, provider_key);
// Try authorize
Hashtable parameters = new Hashtable();
// Add service_id
parameters.Add("service_id", service_id);
// Add app_id (and app_key if necessary) to list of parameters to send
parameters.Add("app_id", app_id);
parameters.Add("app_key", app_key);
// Alternatively, add user_key to list of parameters to send
//parameters.Add("user_key", user_key);
System.Collections.Hashtable usage = new Hashtable();
usage.Add("hits", "1");
parameters.Add("usage", usage);
AuthorizeResponse resp = _3ScaleAPI.authorize(parameters);
print(resp);
Console.WriteLine("Done authorize...");
// Try authrep
AuthorizeResponse authRepResp = _3ScaleAPI.authrep(parameters);
print(authRepResp);
Console.WriteLine("Done authrep");
// Try report
System.Collections.Hashtable transactions = new System.Collections.Hashtable();
System.Collections.Hashtable transaction = null;
transaction = new System.Collections.Hashtable();
transaction.Add("app_id", app_id);
transaction.Add("service_id", service_id);
//transaction.Add("user_key", user_key);
usage = new System.Collections.Hashtable();
usage.Add("hits", 10);
transaction.Add("usage", usage);
transactions.Add("0", transaction);
transaction = new System.Collections.Hashtable();
transaction.Add("app_id", app_id);
//transaction.Add("user_key", user_key);
usage = new System.Collections.Hashtable();
usage.Add("hits", 1);
transaction.Add("usage", usage);
transactions.Add("1", transaction);
_3ScaleAPI.report(transactions);
Console.WriteLine("Done report...");
// Try oauth_authorize and report
AuthorizeResponse oAuthResp = _3ScaleAPI.oauth_authorize(parameters);
print(oAuthResp);
if(oAuthResp.authorized)
{
_3ScaleAPI.report(transactions);
Console.WriteLine("Done OAuth authorize and report");
}
else
{
Console.WriteLine("OAuth authorize called, report not done as not authorized");
}
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
}
}
}