/
MailChimpSubscriber.cs
133 lines (109 loc) · 4.01 KB
/
MailChimpSubscriber.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
#if UNITY_EDITOR || DEVELOPMENT_BUILD
#define ENABLE_MAILCHIMP_ERROR_LOG
#define ENABLE_MAILCHIMP_SUCCESS_LOG
#endif
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.UI;
using System;
using System.Collections;
using System.Text;
using System.Net.Mail;
using JetBrains.Annotations;
using UnityEngine.Networking;
namespace Fiftytwo
{
[PublicAPI]
public class MailChimpSubscriber : MonoBehaviour
{
private const string UrlFormat = "https://{0}.api.mailchimp.com/3.0/lists/{1}/members";
private const string JsonFormat = "{{\"email_address\":\"{0}\", \"status\":\"subscribed\"}}";
public MailChimpEvent SubscribeSuccess = new MailChimpEvent();
public MailChimpEvent SubscribeError = new MailChimpEvent();
[SerializeField] private string _apiKey = string.Empty;
[SerializeField] private string _listId = string.Empty;
public void Subscribe ()
{
var text = GetComponent<Text>();
if( text == null )
{
#if ENABLE_MAILCHIMP_ERROR_LOG
Debug.LogError( "MailChimp — No UI Text found at this GameObject" );
#endif
SubscribeError.Invoke( null );
return;
}
Subscribe( text.text );
}
public void Subscribe ( string email )
{
try
{
var url = BuildUrl();
if( string.IsNullOrEmpty( url ) )
{
#if ENABLE_MAILCHIMP_ERROR_LOG
Debug.LogError( "MailChimp — Invalid API Key format" );
#endif
SubscribeError.Invoke( email );
return;
}
var mailAddress = new MailAddress( email );
StartCoroutine( SendToMailChimp( url, mailAddress.Address ) );
}
catch( Exception ex )
{
#if ENABLE_MAILCHIMP_ERROR_LOG
Debug.LogError( "MailChimp — Invalid email: " + ex.Message );
#endif
SubscribeError.Invoke( email );
}
}
private string BuildUrl ()
{
var separatorIndex = _apiKey.LastIndexOf( '-' );
if( separatorIndex < 0 )
return null;
var urlPrefix = _apiKey.Substring( separatorIndex + 1 );
if( string.IsNullOrEmpty( urlPrefix ) )
return null;
return string.Format( UrlFormat, urlPrefix, _listId );
}
private IEnumerator SendToMailChimp ( string url, string email )
{
var request = new UnityWebRequest( url, UnityWebRequest.kHttpVerbPOST );
request.SetRequestHeader( "Content-Type", "application/json" );
request.SetRequestHeader( "Authorization", "apikey " + _apiKey );
var json = string.Format( JsonFormat, email );
var jsonBytes = Encoding.UTF8.GetBytes( json );
request.uploadHandler = new UploadHandlerRaw( jsonBytes );
request.downloadHandler = new DownloadHandlerBuffer();
yield return request.SendWebRequest();
if( request.isNetworkError )
{
#if ENABLE_MAILCHIMP_ERROR_LOG
Debug.LogErrorFormat( "MailChimp — Network error: {0}", request.error );
#endif
SubscribeError.Invoke( email );
yield break;
}
if( request.isHttpError )
{
#if ENABLE_MAILCHIMP_ERROR_LOG
Debug.LogErrorFormat( "MailChimp — Subscribe error {0}: {1}\n{2}",
request.responseCode, request.error, request.downloadHandler.text );
#endif
SubscribeError.Invoke( email );
yield break;
}
#if ENABLE_MAILCHIMP_SUCCESS_LOG
Debug.LogFormat( "MailChimp — Subscribe success {0}\n{1}", request.responseCode, request.downloadHandler.text );
#endif
SubscribeSuccess.Invoke( email );
}
[Serializable]
public class MailChimpEvent : UnityEvent<string>
{
}
}
}