99import avro .io
1010import io
1111import structlog
12+ from datetime import datetime
1213
1314logger = structlog .get_logger ()
1415
15- # todo: also read these from dict module
1616ISSUER = os .getenv ("SALESFORCE_CONSUMER_KEY" )
1717DOMAIN = os .getenv ("SALESFORCE_DOMAIN" )
1818SUBJECT = os .getenv ("SALESFORCE_USERNAME" )
1919INSTANCE_URL = os .getenv ("INSTANCE_URL" )
2020TENANT_ID = os .getenv ("TENANT_ID" )
21+ PLATFORM_MESSAGE_AUTHOR = os .getenv ("PLATFORM_MESSAGE_AUTHOR_RECORD_ID" )
2122
2223UPDATE_TOPIC = "/event/Updated_Contacts_From_Pipeline__e"
2324
2425
25- def pipeline_update_message (message_dict ):
26- # todo: look for certificate using both local and container pathing
26+ def send_pipeline_update_messages (contacts_list ):
2727 pem_file = 'bin/connected-app-secrets.pem'
2828 with open (pem_file ) as fd :
2929 private_key = fd .read ()
@@ -42,7 +42,6 @@ def pipeline_update_message(message_dict):
4242 'grant_type' : 'urn:ietf:params:oauth:grant-type:jwt-bearer' ,
4343 'assertion' : assertion ,
4444 })
45- logger .info (r .json ())
4645 access_token = r .json ()['access_token' ]
4746 logger .info ('Made OAuth call to get access token' )
4847
@@ -52,16 +51,25 @@ def pipeline_update_message(message_dict):
5251 auth_meta_data = (('accesstoken' , access_token ),
5352 ('instanceurl' , INSTANCE_URL ),
5453 ('tenantid' , TENANT_ID ))
54+
55+
5556 stub = pb2_grpc .PubSubStub (channel )
5657 schema_id = stub .GetTopic (pb2 .TopicRequest (topic_name = UPDATE_TOPIC ), metadata = auth_meta_data ).schema_id
5758 schema = stub .GetSchema (pb2 .SchemaRequest (schema_id = schema_id ), metadata = auth_meta_data ).schema_json
58- buf = io .BytesIO ()
59- encoder = avro .io .BinaryEncoder (buf )
60- writer = avro .io .DatumWriter (avro .schema .parse (schema ))
61- writer .write (message_dict , encoder )
62- payload = {
63- "schema_id" : schema_id ,
64- "payload" : buf .getvalue ()
65- }
66- stub .Publish (pb2 .PublishRequest (topic_name = UPDATE_TOPIC , events = [payload ]), metadata = auth_meta_data )
67- logger .info ('Pipeline update message sent' )
59+
60+ for contact_dict in contacts_list :
61+ contact_dict ['CreatedDate' ] = int (datetime .now ().timestamp ())
62+ contact_dict ['CreatedById' ] = PLATFORM_MESSAGE_AUTHOR
63+
64+ buf = io .BytesIO ()
65+ encoder = avro .io .BinaryEncoder (buf )
66+ writer = avro .io .DatumWriter (avro .schema .parse (schema ))
67+ writer .write (contact_dict , encoder )
68+ payload = {
69+ "schema_id" : schema_id ,
70+ "payload" : buf .getvalue ()
71+ }
72+ stub .Publish (pb2 .PublishRequest (topic_name = UPDATE_TOPIC , events = [payload ]), metadata = auth_meta_data )
73+ logger .info ('Pipeline update message sent' )
74+
75+ logger .info ("%s total pipeline update messages sent" , len (contacts_list ))
0 commit comments