-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathZohoReader.cs
More file actions
146 lines (128 loc) · 5.33 KB
/
ZohoReader.cs
File metadata and controls
146 lines (128 loc) · 5.33 KB
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
146
namespace ZohoSync
{
using System;
using System.IO;
using System.Linq;
using System.Text;
using System.Xml.Linq;
/// <summary>
/// zoho data reader
/// </summary>
internal class ZohoReader
{
/// <summary>
/// login API
/// </summary>
const string LOGIN_API = "https://accounts.zoho.com/apiauthtoken/nb/create?SCOPE=ZohoCRM/crmapi&EMAIL_ID={0}&PASSWORD={1}";
/// <summary>
/// table API
/// </summary>
const string TABLE_API = "https://crm.zoho.com/crm/private/xml/{0}/getRecords?newFormat=1&authtoken={1}&scope=crmapi&fromIndex={2}&toIndex={3}";
/// <summary>
/// token
/// </summary>
private string token = string.Empty;
/// <summary>
/// authenticate
/// </summary>
public bool Authenticate()
{
// write to console
Program.OutputWrite("Zoho: authenticate - ");
// authenticate
var webClient = new SlowWebClient();
webClient.Encoding = Encoding.UTF8;
string response = webClient.DownloadString(string.Format(LOGIN_API, ConfigReader.ZohoLogin, ConfigReader.ZohoPassword));
// split by lines
var lines = response.Split(new char[] { '\n' }, StringSplitOptions.RemoveEmptyEntries);
// result
var result = bool.Parse((from l in lines
where l.StartsWith("RESULT=")
select l.Split('=')[1]).FirstOrDefault());
if (!result)
{
// failed
var cause = (from l in lines
where l.StartsWith("CAUSE=")
select l.Split('=')[1]).FirstOrDefault();
Program.OutputWriteLine(" failed. [" + cause + "]");
return false;
}
else
{
// auth token
this.token = (from l in lines
where l.StartsWith("AUTHTOKEN=")
select l.Split('=')[1]).FirstOrDefault();
// done
Program.OutputWriteLine(" done. [" + this.token + "]");
return true;
}
}
/// <summary>
/// get data
/// </summary>
/// <param name="zohoTable">zoho table</param>
/// <returns>Xml Element with all data</returns>
public XElement GetData(string zohoTable)
{
// main xml
XElement root = new XElement("response");
// request table
Program.OutputWrite("Zoho: request table '" + zohoTable + "'");
var webClient = new SlowWebClient();
var counter = 0;
for (int i = 0; i < 1000000; i = i + 200)
{
Program.OutputWrite(".");
// request
webClient.Encoding = Encoding.UTF8;
string content = webClient.DownloadString(string.Format(TABLE_API, zohoTable, token, i, i + 199));
// parse it
XElement partRoot = XElement.Parse(content);
if (partRoot.Elements("nodata").Any())
{
Program.OutputWriteLine(" done [" + counter + " records]");
break;
}
else if (partRoot.Elements("error").Any())
{
Program.OutputWriteLine(" failed.");
var error = partRoot.Elements("error").First();
error.Remove();
root.Add(error);
break;
}
else if (partRoot.Descendants(zohoTable).Any())
{
var result = partRoot.Descendants(zohoTable).First().Elements("row").ToList();
foreach (var r in result)
{
// hodnoty
var row = new XElement("record");
var email = r.Elements("FL").Where(e => e.Attribute("val").Value == "Email").FirstOrDefault();
if (email != null) row.Add(new XElement("email", email.Value));
else row.Add(new XElement("email", string.Empty));
var fname = r.Elements("FL").Where(e => e.Attribute("val").Value == "First Name").FirstOrDefault();
if (fname != null) row.Add(new XElement("firstName", fname.Value));
else row.Add(new XElement("firstName", string.Empty));
var lname = r.Elements("FL").Where(e => e.Attribute("val").Value == "Last Name").FirstOrDefault();
if (lname != null) row.Add(new XElement("lastName", lname.Value));
else row.Add(new XElement("lastName", string.Empty));
counter++;
root.Add(row);
}
}
}
if (Program.WriteFiles)
{
// save it
string file = Path.Combine(Directory.GetCurrentDirectory(), zohoTable + ".xml");
root.Save(file);
Program.OutputWriteLine("Zoho: export saved to '" + file + "' done.");
}
return root;
}
}
}