/
main.go
333 lines (299 loc) · 14.1 KB
/
main.go
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
package godig
import (
"log"
"net/http"
"strings"
"time"
"github.com/tidwall/gjson"
)
//ClassicDateFormat is used to parse a Salsa timestamp coming from the database.
const ClassicDateFormat = "Mon Jan 2 2006 15:04:05 GMT-0700 (MST)"
//EngageDateFormat is used to format a date for Engage.
const EngageDateFormat = "2006-01-02T15:04:05.000Z"
//DateFormat is used to format a time so that Engage will recognize it.
const DateFormat = "2006-01-02"
//TimestampFormat is used to format a time so that Engage will recognize it.
const TimestampFormat = "2006-01-02T15:04:05"
//API hold the data that we need to do Salsa API calls. That includes
//the cookies from authentication.
type API struct {
Client *http.Client
Cookies []*http.Cookie
Host string
Verbose bool
CredData CredData
}
//Table links an API to a Salsa database table.
type Table struct {
*API
Name string
}
//AuthStatus contains the information returned by Authentication.
type AuthStatus struct {
Status string
Message string
}
//Results is returned by API calls.
type Results struct {
Object string `json:"object"`
Key string `json:"key"`
Result string `json:"result"`
Messages []string `json:"messages"`
}
//CredData contains the info that we need to get into the API.
type CredData struct {
Host string
Email string
Password string
}
//DeleteStatus contins the info returned by deleting a record.
type DeleteStatus struct {
Object string
Key string
Result string
Messages []string
}
//Field is used to describe table fields when calling Desfcribe.
type Field struct {
Name string `json:"name,omitempty"`
Nullable string `json:"nullable,omitempty"`
Type string `json:"type,omitempty"`
DefaultValue string `json:"defaultValue,omitempty"`
Label string `json:"label,omitempty"`
MaxLength string `json:"maxlength,omitempty"`
IsCustom string `json:"isCustom,omitEmpty"`
DisplayName string `json:"displayName,omitEmpty"`
// Values is either a struct or a list of strings in Salsa.
// No return on special marshaler. Ignoring.
//Values []string `json:"values,omitempty"`
}
//FieldList is a slice of Fields returned by Describe.
type FieldList []Field
//MapList is a slice of FieldMaps.
type MapList []gjson.Result
//NewAPI initializes and returns an API object.
func NewAPI() *API {
c := API{}
c.Client = &http.Client{}
return &c
}
//NewTable creates a table using a table/object name.
func (a *API) NewTable(n string) Table {
t := Table{a, n}
return t
}
//Chapter is a shortcut for creating an organization Table object.
func (a *API) Chapter() Table {
return a.NewTable("chapter")
}
//Donation is a shortcut for creating a donation Table object.
func (a *API) Donation() Table {
return a.NewTable("donation")
}
//EmailBlast is a shortcut for creating an EmailBlast Table object.
func (a *API) EmailBlast() Table {
return a.NewTable("email_blast")
}
//Groups is a shorcut for creating a groups Table. Note that
//"groups" is the only table in the API that's plural.
func (a *API) Groups() Table {
return a.NewTable("groups")
}
//GroupsSupporters is a shortcut to join groups to supporters
//via the supporter_groups table. Use LeftJoin to get data for
//this object.
func (a *API) GroupsSupporters() Table {
return a.NewTable("groups(groups_Key)supporter_groups(supporter_Key)supporter")
}
//Org is a shortcut for creating an organization Table object.
func (a *API) Org() Table {
return a.NewTable("organization")
}
//Supporter is a shortcut for creating a supporter Table object.
func (a *API) Supporter() Table {
return a.NewTable("supporter")
}
//SupporterDonation is a shortcut for creating a Table that
//holds supporter and donation records. Use LeftJoin to get
//data for this object.
func (a *API) SupporterDonation() Table {
return a.NewTable("supporter(supporter_Key)donation")
}
//SupporterGroups is a shortcut for creating a supporter_group Table object.
func (a *API) SupporterGroups() Table {
return a.NewTable("supporter_groups")
}
//Publish is a shortcut for creating a publish Table object.
func (a *API) Publish() Table {
return a.NewTable("publish")
}
//Unsubscribe is a shortcut for creating an unsubscribe Table object.
func (a *API) Unsubscribe() Table {
return a.NewTable("unsubscribe")
}
//ClassicTime accepts a Classic timestamp and returns a time object.
func ClassicTime(s string) (t time.Time, err error) {
t, err = time.Parse(ClassicDateFormat, s)
return t, err
}
//ShortDate accepts a time and outputs it as YYYY-mm-dd.
func ShortDate(s string) string {
s = strings.TrimSpace(s)
if len(s) == 0 {
return s
}
t, err := time.Parse(ClassicDateFormat, s)
if err != nil {
log.Fatalf("%v: %v\n", err, s)
}
return t.Format(DateFormat)
}
//EngageDate parses converts a string containing a MySQL date to
//another string containing an Engage date.
func EngageDate(s string) string {
if len(s) == 0 {
return s
}
t, err := time.Parse(ClassicDateFormat, s)
if err != nil {
log.Printf("Warning: parsing %v returned %v\n", s, err)
} else {
// Engage cannot import EngageDateFormat. Use DateFormat instead.
s = t.Format(DateFormat)
}
return s
}
//EngageTimestamp parses converts a string containing a MySQL date to
//another string containing an Engage date and time.
func EngageTimestamp(s string) string {
// Date_Created, Transaction_Date, etc. Convert dates from MySQL to Engage.
p := strings.Split(s, " ")
if len(p) >= 7 {
//Pull out the timezone.
p = append(p[0:5], p[6])
x := strings.Join(p, " ")
if len(x) == 0 {
return x
}
t, err := time.Parse(ClassicDateFormat, x)
if err != nil {
log.Printf("Warning: parsing %v returned %v\n", s, err)
} else {
s = t.Format(TimestampFormat)
}
}
return s
}
//Organization describes a record for an Salsa Classic client.
//Be aware that the bulk of the non-identity and non-status fields
//have been deprecated.
type Organization struct {
OrganizationKey string `json:"organization_KEY"`
RootKey string `json:"root_Key,omitempty"`
ParentKey string `json:"parent_Key,omitempty"`
PartnerKey string `json:"partner_Key,omitempty"`
LastModified string `json:"Last_Modified,omitempty"`
DateCreated string `json:"Date_Created,omitempty"`
PRIVATEDateCreated string `json:"PRIVATE_Date_Created,omitempty"`
Name string `json:"Name"`
Type string `json:"Type"`
Status string `json:"Status"`
READONLYShortName string `json:"READONLY_Short_Name,omitempty"`
Description string `json:"Description,omitempty"`
OrganizationHomepage string `json:"Organization_Homepage,omitempty"`
NewsletterOrListserveName string `json:"Newsletter_or_Listserve_Name,omitempty"`
CustomHeaderHTML string `json:"Custom_Header_HTML,omitempty"`
CustomFooterHTML string `json:"Custom_Footer_HTML,omitempty"`
PrintHeader string `json:"Print_Header,omitempty"`
PrintFooter string `json:"Print_Footer,omitempty"`
BaseURL string `json:"Base_URL"`
SecureURL string `json:"Secure_URL"`
Street string `json:"Street,omitempty"`
Street2 string `json:"Street_2,omitempty"`
City string `json:"City,omitempty"`
State string `json:"State,omitempty"`
Zip string `json:"Zip,omitempty"`
PRIVATEZipPlus4 string `json:"PRIVATE_Zip_Plus_4,omitempty"`
MailServer string `json:"mail_server,omitempty"`
MailUser string `json:"mail_user,omitempty"`
MailPass string `json:"mail_pass,omitempty"`
MailStatus string `json:"Mail_Status,omitempty"`
PromotionalCode string `json:"Promotional_Code,omitempty"`
Interests string `json:"Interests,omitempty"`
Note string `json:"Note,omitempty"`
GlobalMailHTMLFooter string `json:"Global_Mail_HTML_Footer,omitempty"`
GlobalMailTextFooter string `json:"Global_Mail_Text_Footer,omitempty"`
LinkTrackURL string `json:"Link_Track_URL,omitempty"`
OpenTrackURL string `json:"Open_Track_URL,omitempty"`
SalsifiedBOOLVALUE string `json:"salsified_BOOLVALUE,omitempty"`
Salsified bool `json:"salsified,omitempty"`
StatusLastModified string `json:"Status_Last_Modified,omitempty"`
DateTrialStarted string `json:"Date_Trial_Started,omitempty"`
ContractDate string `json:"Contract_Date,omitempty"`
ToolsInContract string `json:"Tools_In_Contract,omitempty"`
ClosedDate string `json:"Closed_Date,omitempty"`
ClosedReason string `json:"Closed_Reason,omitempty"`
DefaultEmailAddress string `json:"default_email_address,omitempty"`
DefaultMerchantAccountKey string `json:"default_merchant_account_Key,omitempty"`
MovedBOOLVALUE string `json:"moved_BOOLVALUE,omitempty"`
Moved bool `json:"moved,omitempty"`
BlastNotificationEmail string `json:"Blast_Notification_Email,omitempty"`
Country string `json:"Country,omitempty"`
ListSize string `json:"List_Size,omitempty"`
Usages string `json:"Usages,omitempty"`
HearAboutUs string `json:"hear_about_us,omitempty"`
Tier string `json:"Tier,omitempty"`
TaxStatus string `json:"Tax_Status,omitempty"`
StaffContact string `json:"Staff_Contact,omitempty"`
LanguageCode string `json:"language_code,omitempty"`
DisableTokenAuthenticationBOOLVALUE string `json:"Disable_Token_Authentication_BOOLVALUE,omitempty"`
DisableTokenAuthentication bool `json:"Disable_Token_Authentication,omitempty"`
EnforcePackagePermissions string `json:"Enforce_Package_Permissions,omitempty"`
RecommendedMailServer string `json:"recommended_mail_server,omitempty"`
OverrideMailServerBOOLVALUE string `json:"override_mail_server_BOOLVALUE,omitempty"`
OverrideMailServer bool `json:"override_mail_server,omitempty"`
EmailBlastBrandingOptOutBOOLVALUE string `json:"Email_Blast_Branding_Opt_Out_BOOLVALUE,omitempty"`
EmailBlastBrandingOptOut bool `json:"Email_Blast_Branding_Opt_Out,omitempty"`
ExternalClientID string `json:"external_client_id,omitempty"`
RecommendEmailBlastBrandingOptOutBOOLVALUE string `json:"Recommend_Email_Blast_Branding_Opt_Out_BOOLVALUE,omitempty"`
RecommendEmailBlastBrandingOptOut bool `json:"Recommend_Email_Blast_Branding_Opt_Out,omitempty"`
OrganizationID string `json:"organization_id,omitempty"`
WebsiteForcedBrandingRequiredBOOLVALUE string `json:"Website_Forced_Branding_Required_BOOLVALUE,omitempty"`
WebsiteForcedBrandingRequired bool `json:"Website_Forced_Branding_Required,omitempty"`
AuthenticateEmailsBOOLVALUE string `json:"Authenticate_Emails_BOOLVALUE,omitempty"`
AuthenticateEmails bool `json:"Authenticate_Emails,omitempty"`
EnforceAutodedupeOnEmailSendBOOLVALUE string `json:"enforce_autodedupe_on_email_send_BOOLVALUE,omitempty"`
EnforceAutodedupeOnEmailSend bool `json:"enforce_autodedupe_on_email_send,omitempty"`
EnforceHTTPSBOOLVALUE string `json:"enforce_https_BOOLVALUE,omitempty"`
EnforceHTTPS bool `json:"enforce_https,omitempty"`
}
//Chapter describes a chapter for an organization. Chapters may be rooted at the
//"national" (organization) level, or may be rooted in other chapters.
type Chapter struct {
ChapterKey string `json:"chapter_KEY"`
OrganizationKey string `json:"organization_KEY"`
ParentKey string `json:"parent_KEY,omitempty"`
ChapterTypeKey string `json:"chapter_type_KEY"`
LastModified string `json:"Last_Modified"`
DateCreated string `json:"Date_Created"`
Name string `json:"Name"`
READONLYShortName string `json:"READONLY_Short_Name,omitempty"`
Description string `json:"Description,omitempty"`
Official bool `json:"Official,omitempty"`
Chartered bool `json:"Chartered,omitempty"`
ChapterType string `json:"Chapter_Type,omitempty"`
HomepageURL string `json:"Homepage_URL,omitempty"`
Slogan string `json:"Slogan,omitempty"`
Municipality string `json:"municipality,omitempty"`
Region string `json:"region,omitempty"`
PostalCode string `json:"postal_code,omitempty"`
Country string `json:"Country,omitempty"`
Status string `json:"Status,omitempty"`
SiteID string `json:"siteID,omitempty"`
Latitude string `json:"Latitude,omitempty"`
Longitude string `json:"Longitude,omitempty"`
Privacy string `json:"Privacy,omitempty"`
BaseURL string `json:"Base_URL,omitempty"`
SecureURL string `json:"Secure_URL,omitempty"`
SyndicationOptions string `json:"Syndication_Options,omitempty"`
}