-
Notifications
You must be signed in to change notification settings - Fork 2
/
ProjectRunner.cs
145 lines (118 loc) · 5.35 KB
/
ProjectRunner.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
140
141
142
143
144
145
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using ParseHub.Client.Models;
using RestSharp;
using RestSharp.Extensions.MonoHttp;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ParseHub.Client
{
public class ProjectRunner
{
private string ApiKey;
private string ApiUrl = @"https://www.parsehub.com/api/v2";
public ProjectRunner(string apiKey)
{
ApiKey = apiKey;
}
public ProjectRunner(string apiKey, string apiUrl)
{
ApiKey = apiKey;
ApiUrl = apiUrl;
}
public List<Project> GetAllProjects()
{
var client = new RestClient(ApiUrl);
var request = new RestRequest("projects", Method.GET);
request.AddParameter("api_key", ApiKey);
List<Project> projects = null;
IRestResponse response = client.Execute(request);
if (response.StatusCode == System.Net.HttpStatusCode.OK)
{
JObject jsonResponse = (JObject)JsonConvert.DeserializeObject(response.Content);
projects = JsonConvert.DeserializeObject<List<Project>>(jsonResponse["projects"].ToString());
}
else
throw new Exception("Expected response status code OK, received response status code " + response.StatusCode);
return projects;
}
public Project GetProject(string projectToken)
{
var client = new RestClient(ApiUrl);
var request = new RestRequest("projects/{token}", Method.GET);
request.AddParameter("api_key", ApiKey);
request.AddUrlSegment("token", projectToken);
Project project = null;
IRestResponse response = client.Execute(request);
if (response.StatusCode == System.Net.HttpStatusCode.OK)
{
project = JsonConvert.DeserializeObject<Project>(response.Content);
}
else
throw new Exception("Expected response status code OK, received response status code " + response.StatusCode);
return project;
}
public Run RunProject(string projectToken, string startUrl = null, string startTemplate = null, string startValueOverride = null, bool sendEmail = false)
{
var client = new RestClient(ApiUrl);
var request = new RestRequest("projects/{token}/run", Method.POST);
request.AddParameter("api_key", ApiKey);
request.AddUrlSegment("token", projectToken);
if (!string.IsNullOrEmpty(startUrl))
request.AddParameter("start_url", startUrl);
if (!string.IsNullOrEmpty(startTemplate))
request.AddParameter("start_template", startTemplate);
if (!string.IsNullOrEmpty(startValueOverride))
request.AddParameter("start_value_override", startValueOverride);
if (sendEmail)
request.AddParameter("send_email", 1);
Run run = null;
IRestResponse response = client.Execute(request);
if (response.StatusCode == System.Net.HttpStatusCode.OK)
run = JsonConvert.DeserializeObject<Run>(response.Content);
else
throw new Exception("Expected response status code OK, received response status code " + response.StatusCode);
return run;
}
public Run GetRun(string runToken)
{
var client = new RestClient(ApiUrl);
var request = new RestRequest("runs/{token}", Method.GET);
request.AddParameter("api_key", ApiKey);
request.AddUrlSegment("token", runToken);
Run run = null;
IRestResponse response = client.Execute(request);
if (response.StatusCode == System.Net.HttpStatusCode.OK)
{
run = JsonConvert.DeserializeObject<Run>(response.Content);
if (run != null && run.Status != null)
{
if (run.Status.ToLower().Equals("complete"))
run.Data = GetRunData(runToken);
}
else
throw new Exception("Response was OK, but ParseHub Run or Run Status is null");
}
else
throw new Exception("Expected response status code OK, received response status code " + response.StatusCode + ". Error Message: " + response.ErrorMessage);
return run;
}
private string GetRunData(string runToken)
{
var client = new RestClient(ApiUrl);
var request = new RestRequest("runs/{token}/data", Method.GET);
request.AddParameter("api_key", ApiKey);
request.AddUrlSegment("token", runToken);
string runData = String.Empty;
IRestResponse response = client.Execute(request);
if (response.StatusCode == System.Net.HttpStatusCode.OK)
runData = response.Content;
else
throw new Exception("Expected response status code OK, received response status code " + response.StatusCode);
return runData;
}
}
}