-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1,101 changed files
with
35,583 additions
and
21,791 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 57 additions & 0 deletions
57
rocket-bi-server/src/main/scala/co/datainsider/bi/client/MailChimpClient.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package co.datainsider.bi.client | ||
|
||
import co.datainsider.bi.util.{Serializer, StringUtils, ZConfig} | ||
import com.fasterxml.jackson.databind.JsonNode | ||
import datainsider.client.exception.InternalError | ||
import scalaj.http.{Http, HttpResponse} | ||
|
||
object MailChimpClient { | ||
private val dc: String = ZConfig.getString("mailchimp.dc") | ||
private val apiKey: String = ZConfig.getString("mailchimp.api_key") | ||
private val listId: String = ZConfig.getString("mailchimp.list_id") | ||
|
||
def apply(): MailChimpClient = new MailChimpClient(dc, apiKey, listId) | ||
} | ||
|
||
class MailChimpClient(dc: String, apiKey: String, listId: String) { | ||
private val baseUrl = s"https://$dc.api.mailchimp.com/3.0" | ||
|
||
def ping(): String = { | ||
val resp: HttpResponse[String] = | ||
Http(s"$baseUrl/ping").method("GET").auth("key", apiKey).asString | ||
|
||
extract[String](resp)(resp => resp) | ||
} | ||
|
||
def addMember(email: String, firstName: String, lastName: String): Boolean = { | ||
val memberData = | ||
s""" | ||
|{ | ||
| "email_address": "$email", | ||
| "status": "subscribed", | ||
| "merge_fields": { | ||
| "FNAME": "$firstName", | ||
| "LNAME": "$lastName" | ||
| } | ||
|} | ||
|""".stripMargin | ||
|
||
val resp: HttpResponse[String] = | ||
Http(s"$baseUrl/lists/$listId/members/${StringUtils.md5(email)}") | ||
.method("PUT") | ||
.auth("key", apiKey) | ||
.put(memberData) | ||
.asString | ||
|
||
extract[Boolean](resp)(resp => { | ||
val jsonNode = Serializer.fromJson[JsonNode](resp) | ||
jsonNode.get("id").asText() != null | ||
}) | ||
} | ||
|
||
private def extract[T](resp: HttpResponse[String])(converter: String => T): T = { | ||
if (resp.isSuccess) { | ||
converter(resp.body) | ||
} else throw InternalError(s"call to MailChimp fail with code: ${resp.code}, message: ${resp.body}") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.