Completed
Push — master ( c6c370...5ad28f )
by Edward
20s
created

ZendeskAction.api_search()   A

Complexity

Conditions 1

Size

Total Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
c 1
b 0
f 0
dl 0
loc 2
rs 10
1
from zenpy import Zenpy
2
from zenpy.lib.api_objects import Ticket
3
from zenpy.lib.api_objects import Comment
4
from zenpy.lib.exception import RecordNotFoundException
5
from zenpy.lib.exception import APIException
6
7
from st2actions.runners.pythonrunner import Action
8
9
__all__ = [
10
    'ZendeskAction'
11
]
12
13
14
class ZendeskAction(Action):
15
    def __init__(self, config):
16
        super(ZendeskAction, self).__init__(config=config)
17
        self.email = self.config['email']
18
        self.token = self.config['api_token']
19
        self.subdomain = self.config['subdomain']
20
21
        self.credentials = {
22
            'email': self.email,
23
            'token': self.token,
24
            'subdomain': self.subdomain
25
        }
26
27
        self.api = Zenpy(**self.credentials)
28
29
    def clean_response(self, text):
30
        return text.replace('\n', ' ').replace('  ', ' ').strip()
31
32
    def url_for_ticket(self, ticket):
33
        return 'https://{}.zendesk.com/agent/tickets/{}'.format(self.subdomain, ticket)
34
35
    def api_search(self, query, search_type):
36
        return self.api.search(query, type=search_type, sort_by='created_at', sort_order='desc')
37
38
    def create_ticket(self, subject, description):
39
        ticket = Ticket(subject=subject, description=description)
40
41
        try:
42
            created_ticket_audit = self.api.tickets.create(ticket)
43
            return {
44
                'ticket_id': created_ticket_audit.ticket.id,
45
                'ticket_url': self.url_for_ticket(created_ticket_audit.ticket.id),
46
                'subject': self.clean_response(subject),
47
                'description': self.clean_response(description)
48
            }
49
        except APIException:
50
            return {'error': 'Could not create ticket with provided parameters'}
51
        except Exception as e:
52
            self.logger.error(e)
53
            return {'error': 'Could not make API request'}
54
55
    def search_tickets(self, query, search_type='ticket', limit=10):
56
        try:
57
            query_results = self.api_search(query, search_type)
58
            results_clean = map(lambda t: {
59
                'ticket_id': t.id,
60
                'ticket_url': self.url_for_ticket(t.id),
61
                'ticket_status': t.status,
62
                'subject': self.clean_response(t.subject),
63
                'description': self.clean_response(t.description)},
64
                list(query_results)[:limit]
65
            )
66
            return {'search_results': results_clean}
67
        except APIException:
68
            return {'error': 'Could not execute search for query: {}'.format(query)}
69
        except Exception as e:
70
            self.logger.error(e)
71
            return {'error': 'There was an error executing your search'}
72
73
    def update_ticket(self, ticket_id, comment_text, public):
74
        try:
75
            ticket = self.api.tickets(id=ticket_id)
76
            ticket.comment = Comment(body=comment_text, public=public)
77
            self.api.tickets.update(ticket)
78
            return {
79
                'ticket_id': ticket_id,
80
                'ticket_url': self.url_for_ticket(ticket_id),
81
                'body': self.clean_response(comment_text),
82
                'public': public
83
            }
84
        except RecordNotFoundException:
85
            return {'error': 'Could not find ticket #{}'.format(ticket_id)}
86
        except Exception as e:
87
            self.logger.error(e)
88
            return {'error': 'Could not update ticket'}
89
90
    def update_ticket_status(self, ticket_id, status):
91
        valid_statuses = ['new', 'open', 'pending', 'solved', 'closed']
92
        if status in valid_statuses:
93
            try:
94
                ticket = self.api.tickets(id=ticket_id)
95
                ticket.status = status
96
                self.api.tickets.update(ticket)
97
                return {
98
                    'ticket_id': ticket_id,
99
                    'ticket_url': self.url_for_ticket(ticket_id),
100
                    'status': status
101
                }
102
            except RecordNotFoundException:
103
                return {'error': 'Could not find ticket #{}'.format(ticket_id)}
104
            except Exception as e:
105
                self.logger.error(e)
106
                return {'error': 'Could not update ticket status'}
107
        else:
108
            return {'error': 'Invalid status given for ticket'}
109