Skip to content
This repository has been archived by the owner on Jun 16, 2023. It is now read-only.

Commit

Permalink
added birdsend api, needed to change the interface entity, delete and…
Browse files Browse the repository at this point in the history
… reload when upgrading
  • Loading branch information
hansbak committed Nov 23, 2022
1 parent 9d35405 commit 0d6964b
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 1 deletion.
2 changes: 1 addition & 1 deletion entity/GrowerpEntities.xml
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ along with this software (see the LICENSE.md file). If not, see
<entity entity-name="Interface" package="growerp.general">
<field name="interfaceId" type="id" is-pk="true"/>
<field name="baseUrl" type="text-medium"/>
<field name="apiKey" type="text-intermediate"/>
<field name="apiKey" type="text-long"/>
<field name="secretKey" type="text-medium"/>
<field name="value1" type="text-medium"/>
<field name="value2" type="text-medium"/>
Expand Down
108 changes: 108 additions & 0 deletions service/growerp/100/BirdSendServices100.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
<?xml version="1.0" encoding="UTF-8" ?>
<!--
This software is in the public domain under CC0 1.0 Universal plus a
Grant of Patent License.
To the extent possible under law, the author(s) have dedicated all
copyright and related and neighboring rights to this software to the
public domain worldwide. This software is distributed without any
warranty.
You should have received a copy of the CC0 Public Domaicrn Dedication
along with this software (see the LICENSE.md file). If not, see
<http://creativecommons.org/publicdomain/zero/1.0/>.
-->
<services xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://moqui.org/xsd/service-definition-2.1.xsd">

<service verb="registerAdd" noun="UserToGroup">
<in-parameters>
<parameter name="email" required="true"/>
<parameter name="firstName"/>
<parameter name="lastName"/>
</in-parameters>
<out-parameters>
<parameter name="resultMessage"/>
</out-parameters>
<actions>
<entity-find-one entity-name="growerp.general.Interface" value-field="interfaceValue">
<field-map field-name="interfaceId" value="birdSend"/>
</entity-find-one>
<set field="apiKey" from="interfaceValue?.apiKey"/>
<set field="baseUrl" from="interfaceValue?.baseUrl"/>
<set field="groupName" from="interfaceValue?.value1"/>
<if condition="!interfaceValue || !baseUrl || !apiKey || !groupName">
<return error="false" message="mailerLight not configured: missing parameters in table interfaceValue"/>
</if>
<!-- add user -->
<script><![CDATA[
import org.moqui.util.RestClient
import org.moqui.util.RestClient.Method
// get sequence id
Map result = restGet("v1/sequences")
String sequenceId;
for(sequence in result.data) {
if (sequence.name == groupName) sequenceId = sequence.sequence_id
}
if (sequenceId) {
// add user and sequence
result = restPost("v1/contacts", [email: email, sequenceId: sequenceId])
} else {
logger.warn("Sequence group $groupName not found!")
}
//============ closures =============
def Map restPost(String path, Map parameters) {
RestClient restClient = ec.service.rest().method(RestClient.POST)
.uri("$baseUrl/$path").addHeader("Content-Type", "application/json")
.addHeader("Authorization","Bearer ${apiKey}").addBodyParameters(parameters)
RestClient.RestResponse restResponse = restClient.call()
Map respMap = (Map) restResponse.jsonObject()
if (restResponse.statusCode < 200 || restResponse.statusCode >= 300) {
ec.logger.warn("Unsuccessful register ${restResponse.statusCode}: ${respMap}")
return
}
ec.logger.info("Successful : ${respMap}")
return respMap
}
// cannot use other post closure because of strange parameter passing.....
def Map restAssignToGroup(String subscriberId, String groupId) {
RestClient restClient = ec.service.rest().method(RestClient.POST)
.uri("$baseUrl/api/subscribers/$subscriberId/groups/$groupId")
.addHeader("Content-Type", "application/json")
.addHeader("Authorization","Bearer ${apiKey}")
RestClient.RestResponse restResponse = restClient.call()
Map respMap = (Map) restResponse.jsonObject()
if (restResponse.statusCode < 200 || restResponse.statusCode >= 300) {
ec.logger.warn("Unsuccessful register ${restResponse.statusCode}: ${respMap}")
return
}
ec.logger.info("Successful : ${respMap}")
return respMap
}
def Map restGet(String path) {
// List pList = new ArrayList(parameters.entrySet());
RestClient restClient = ec.service.rest().method(RestClient.GET)
restClient.addHeader("Authorization","Bearer ${apiKey}")
.addHeader("Content-Type", "application/json")
restClient.uri().protocol("https")
.host(baseUrl.substring(baseUrl.indexOf('/')+2)).port(443)
.path(path)
// .parameter(pList[0]?.getKey(), pList[0]?.getValue()).parameter(pList[1]?.getKey(), pList[1]?.getValue())
.build()
RestClient.RestResponse restResponse = restClient.call()
Map respMap = (Map) restResponse.jsonObject()
if (restResponse.statusCode < 200 || restResponse.statusCode >= 300) {
ec.logger.warn("Unsuccessful register ${restResponse.statusCode}: ${respMap}")
return
}
ec.logger.info("Successful : ${respMap}")
return respMap
}
]]></script>
</actions>
</service>
</services>

0 comments on commit 0d6964b

Please sign in to comment.