-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcheck_message_request_state.py
51 lines (40 loc) · 1.86 KB
/
check_message_request_state.py
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
from scgapi.Scg import Scg
import scgapi.MessageRequest
import argparse
def check_state(api, config, mrq):
# Construct an instance of the authentication object
# with authentication data from a json file (auth.json)
auth = scgapi.AuthInfo(config=config)
# Prepare a session to the server.
scg = Scg()
# Prepare a session to the server.
session = scg.connect(auth, api)
# Get a MessageRequest resource
res = scgapi.MessageRequest.Resource(session)
# Get the message request instance
message_request = res.get(mrq)
print('Message Request {} is in state {} with {} delivered and {} failed messages'.format(
message_request.id,
message_request.state,
message_request.delivered_count,
message_request.failed_count))
# Get a generator to the messages generated by this message request
msgs = message_request.list_messages()
# Print each message.
#
# Since the SDK use a generator and not a list, we will only have a small
# number of messages in memory at any time, allowing us to iterate
# over many messages if required, even on machines with very
# limited internal memory.
for msg in msgs:
print(' - Message {} is in state {}, error code: {}, error reason: {}'.format(
msg.id, msg.state, msg.failure_code, msg.failure_details))
for frag in msg.fragments_info:
print (' - Fragment {} has state {}'.format(frag.fragment_id, frag.fragment_state))
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('mrq', help='Message-request to query')
parser.add_argument('--api', help='URL to the API server', default=None)
parser.add_argument('--auth', help='Location of json auth file', default='auth.json')
args = parser.parse_args()
check_state(api=args.api, config=args.auth, mrq=args.mrq)