Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
P
PyOTRS
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
External wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package Registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Coopdevs
Som Connexió
OTRS
PyOTRS
Commits
fdc0c0ad
Commit
fdc0c0ad
authored
8 years ago
by
Robert Habermann
Browse files
Options
Downloads
Patches
Plain Diff
refactor (cont.)
parent
dc42abc7
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
pyotrs/client.py
+223
-169
223 additions, 169 deletions
pyotrs/client.py
with
223 additions
and
169 deletions
pyotrs/client.py
+
223
−
169
View file @
fdc0c0ad
...
...
@@ -73,6 +73,10 @@ class NoCredentials(PyOTRSError):
pass
class
ResponseJSONParseError
(
PyOTRSError
):
pass
class
SessionError
(
PyOTRSError
):
pass
...
...
@@ -93,6 +97,10 @@ class TicketDynamicFieldsParseError(PyOTRSError):
pass
class
TicketError
(
PyOTRSError
):
pass
class
OTRSAPIError
(
PyOTRSError
):
pass
...
...
@@ -288,6 +296,37 @@ class Client(object):
"""
def
ticket_get_by_id
(
self
,
ticket_id
,
dynamic_fields
=
1
,
all_articles
=
0
):
"""
ticket_get_by_id
Args:
ticket_id (int): Integer value of a Ticket ID
dynamic_fields (int): will request OTRS to include all Dynamic Fields (default: 1)
all_articles (int): will request OTRS to include all Articles (default: 0)
Returns:
Ticket as dict
"""
url
=
"
{0.baseurl}/otrs/nph-genericinterface.pl/Webservice/
"
\
"
{0.webservicename}/Ticket/{1}
"
.
format
(
self
,
ticket_id
)
payload
=
{
"
SessionID
"
:
self
.
session_id
,
"
AllArticles
"
:
all_articles
,
"
DynamicFields
"
:
dynamic_fields
}
if
dynamic_fields
==
0
:
self
.
ticket_dynamic_fields_requested
=
False
elif
dynamic_fields
==
1
:
self
.
ticket_dynamic_fields_requested
=
True
# TODO 2016-04-13 (RH): decide what to return here.. bool or ticket content
if
self
.
_ticket_get_json
(
url
,
payload
):
return
self
.
ticket_json
[
'
Ticket
'
][
0
]
def
_ticket_get_json
(
self
,
url
,
payload
):
"""
_ticket_get_json
...
...
@@ -298,6 +337,7 @@ class Client(object):
Raises:
OTRSHTTPError
OTRSAPIError
ResponseJSONParseError
Returns:
True if get was successful; otherwise False
...
...
@@ -310,8 +350,16 @@ class Client(object):
params
=
payload
,
verify
=
self
.
https_verify
)
# Just assume that we get valid JSON
self
.
ticket_json
=
json
.
loads
(
self
.
response
.
content
.
decode
())
# critical error: HTTP request resulted in an error!
except
Exception
as
err
:
logger
.
error
(
"
Failed to access OTRS. Check Hostname, Proxy, SSL Certificate!
"
"
Error with http communication: {0}
"
.
format
(
err
))
#raise OTRSHTTPError("get http")
raise
OTRSHTTPError
(
"
Failed to access OTRS. Check Hostname, Proxy, SSL Certificate!
"
"
Error with http communication: {0}
"
.
format
(
err
))
try
:
self
.
ticket_json
=
self
.
response
.
json
()
logger
.
debug
(
self
.
ticket_json
)
if
self
.
ticket_json
.
get
(
"
Ticket
"
)
is
not
None
:
...
...
@@ -323,31 +371,34 @@ class Client(object):
else
:
logger
.
error
(
"
Unknown key in response JSON DICT!
"
)
# critical error: Unknown response from OTRS API - FAIL NOW!
sys
.
exit
(
1
)
logger
.
debug
(
"
Type was set to: {0}
"
.
format
(
self
.
response_type
))
get_request_result_bool
=
True
raise
ResponseJSONParseError
(
"
Unknown key in response JSON DICT!
"
)
# critical error: HTTP request resulted in an error!
except
Exception
as
err
:
logger
.
error
(
"
Error with http communication: {0}
"
.
format
(
err
))
logger
.
error
(
"
Failed to access OTRS HTTP. Check Hostname, Proxy, SSL Certificate...!
"
)
raise
OTRSHTTPError
(
"
get http
"
)
# TODO 2016-04-13 (RH): clean this up!
raise
Exception
(
"
Parse Result to JSON failed.. Message
"
.
format
(
err
))
# Validated response_type
logger
.
debug
(
"
Type was set to: {0}
"
.
format
(
self
.
response_type
))
# critical error: OTRS API sent an error!
if
self
.
response_type
==
"
Error
"
:
logger
.
error
(
self
.
ticket_json
[
"
Error
"
])
logger
.
error
(
"
Failed to access OTRS API. Check Username and Password! Session ID expired?!
"
"
Does requested Ticket exist?
"
)
raise
OTRSAPIError
(
"
get api
"
)
logger
.
error
(
"
TicketGet API Error. Failed to access OTRS API. Check Username
"
"
and Password! Session ID expired?! Does requested Ticket exist?
\n
"
"
OTRS Error_Code; OTRS Message:
"
"
{0}; {1}
"
.
format
(
self
.
ticket_json
[
"
Error
"
][
"
ErrorCode
"
],
self
.
ticket_json
[
"
Error
"
][
"
ErrorMessage
"
]))
raise
OTRSAPIError
(
"
TicketGet API Error. Failed to access OTRS API. Check Username
"
"
and Password! Session ID expired?! Does requested Ticket exist?
\n
"
"
OTRS Error_Code; OTRS Message:
"
"
{0}; {1}
"
.
format
(
self
.
ticket_json
[
"
Error
"
][
"
ErrorCode
"
],
self
.
ticket_json
[
"
Error
"
][
"
ErrorMessage
"
]))
if
self
.
ticket_dynamic_fields_requested
:
self
.
_parse_dynamic_fields
_from_response
()
self
.
_parse_
response_
dynamic_fields
()
return
get_request_result_bool
return
True
def
_parse_dynamic_fields
_from_response
(
self
):
def
_parse_
response_
dynamic_fields
(
self
):
"""
Parses dynamic field from response and stores dict in self.ticket_dynamic_fields
Returns:
...
...
@@ -374,39 +425,6 @@ class Client(object):
return
True
def
ticket_get_by_id
(
self
,
ticket_id
,
dynamic_fields
=
1
,
all_articles
=
0
):
"""
ticket_get_by_id
Args:
ticket_id (int): Integer value of a Ticket ID
dynamic_fields (int): will request OTRS to include all Dynamic Fields (default: 1)
all_articles (int): will request OTRS to include all Articles (default: 0)
Returns:
True if get was successful; otherwise False
"""
url
=
"
{0.baseurl}/otrs/nph-genericinterface.pl/Webservice/
"
\
"
{0.webservicename}/Ticket/{1}
"
.
format
(
self
,
ticket_id
)
payload
=
{
"
SessionID
"
:
self
.
session_id
,
"
AllArticles
"
:
all_articles
,
"
DynamicFields
"
:
dynamic_fields
}
if
dynamic_fields
==
0
:
self
.
ticket_dynamic_fields_requested
=
False
elif
dynamic_fields
==
1
:
self
.
ticket_dynamic_fields_requested
=
True
try
:
return
self
.
_ticket_get_json
(
url
,
payload
)
except
Exception
as
err
:
logger
.
error
(
"
Exception: {0}
"
.
format
(
err
))
return
False
"""
GenericInterface::Operation::Ticket::TicketSearch
public methods:
...
...
@@ -421,84 +439,17 @@ class Client(object):
payload (dict)
Returns:
True if search was successful; otherwise False
List of tickets that were found
"""
url
=
"
{0.baseurl}/otrs/nph-genericinterface.pl/Webservice/
"
\
"
{0.webservicename}/Ticket
"
.
format
(
self
)
return
self
.
_search_json_ticket_data
(
url
,
payload
)
def
_search_json_ticket_data
(
self
,
url
,
payload
):
"""
_search_json_ticket_data
Args:
url (str):
payload (dict):
Raises:
OTRSHTTPError
OTRSAPIError
Returns:
True if search was successful; otherwise False
"""
headers
=
{
"
Content-Type
"
:
"
application/json
"
}
json_payload
=
json
.
dumps
(
payload
)
try
:
self
.
response
=
requests
.
get
(
url
,
proxies
=
proxies
,
data
=
json_payload
,
headers
=
headers
,
verify
=
self
.
https_verify
)
# Just assume that we get valid JSON
self
.
ticket_json
=
json
.
loads
(
self
.
response
.
content
.
decode
())
logger
.
debug
(
self
.
ticket_json
)
if
self
.
ticket_json
.
get
(
"
TicketID
"
)
is
not
None
:
self
.
response_type
=
"
TicketID
"
get_request_result_bool
=
True
else
:
logger
.
debug
(
"
search found no results
"
)
get_request_result_bool
=
False
# critical error: HTTP request resulted in an error!
except
Exception
as
err
:
logger
.
error
(
"
Error with http communication: {0}
"
.
format
(
err
))
logger
.
error
(
"
Failed to access OTRS HTTP. Check Hostname, Proxy, SSL Certificate and so on!
"
)
raise
OTRSHTTPError
(
"
get http
"
)
# critical error: OTRS API sent an error!
if
self
.
response_type
==
"
Error
"
:
logger
.
error
(
self
.
ticket_json
[
"
Error
"
])
logger
.
error
(
"
Failed to access OTRS API. Check Username and Password! Session ID expired?!
"
"
Does requested Ticket exist?
"
)
raise
OTRSAPIError
(
"
get api
"
)
return
get_request_result_bool
@staticmethod
def
_minutes_since_midnight
(
now_obj
):
"""
helper function for getting amount of minutes since midnight
Args:
now_obj (datetime.datetime): e.g. from datetime.datetime.now
Returns:
integer
"""
seconds_since_midnight
=
(
now_obj
-
now_obj
.
replace
(
hour
=
0
,
minute
=
0
,
second
=
0
,
microsecond
=
0
)).
total_seconds
()
minutes_since_midnight
=
int
(
seconds_since_midnight
/
60
)
return
minutes_since_midnight
if
self
.
_ticket_search_json
(
url
,
payload
):
return
self
.
ticket_json
[
'
TicketID
'
]
else
:
return
[]
def
ticket_search_by_past_days
(
self
,
from_past_day
=
1
,
days
=
1
):
"""
Search for tickets that where changed in a certain timeframe in the
...
...
@@ -520,8 +471,7 @@ class Client(object):
e.g. on a Monday use 3, 1 to get tickets from last Friday
Returns:
True if update was successful; otherwise False
List of tickets that were found
"""
...
...
@@ -546,22 +496,15 @@ class Client(object):
logger
.
debug
(
payload
)
try
:
return
self
.
_search_json_ticket_data
(
url
,
payload
)
except
Exception
as
err
:
logger
.
error
(
"
Exception: {0}
"
.
format
(
err
))
return
False
# GenericInterface :: Operation::Ticket :: TicketUpdate
# public methods
# * ticket_update_add_article
# * ticket_update_set_dynamic_field_value
# * ticket_update_set_state
# * ticket_update_set_state_pending
# * ticket_update_set_title
# TODO 2016-04-13 (RH): decide what to return here.. bool or result list (IDs as str)
if
self
.
_ticket_search_json
(
url
,
payload
):
return
self
.
ticket_json
[
'
TicketID
'
]
else
:
return
[]
def
_update_by_ticket_id
(
self
,
url
,
payload
):
"""
update_by_ticket_id
def
_ticket_search_json
(
self
,
url
,
payload
):
"""
_ticket_search_json
_search_json_ticket_data
Args:
url (str):
...
...
@@ -570,52 +513,94 @@ class Client(object):
Raises:
OTRSHTTPError
OTRSAPIError
ResponseJSONParseError
Returns:
True if
update
was successful; otherwise False
True if
search
was successful; otherwise False
"""
headers
=
{
"
Content-Type
"
:
"
application/json
"
}
json_payload
=
json
.
dumps
(
payload
)
try
:
self
.
response
=
requests
.
patch
(
url
,
proxies
=
proxies
,
data
=
json
.
dumps
(
payload
),
verify
=
self
.
https_verify
)
self
.
response
=
requests
.
get
(
url
,
proxies
=
proxies
,
data
=
json_payload
,
headers
=
headers
,
verify
=
self
.
https_verify
)
logger
.
debug
(
"
Received HTTP Status Code: {0}
"
.
format
(
self
.
response
.
status_code
))
logger
.
debug
(
self
.
response
.
content
)
# critical error: HTTP request resulted in an error!
except
Exception
as
err
:
logger
.
error
(
"
Failed to access OTRS HTTP. Check Hostname, Proxy, SSL Certificate!
"
"
!Error with http communication: {0}
"
.
format
(
err
))
#raise OTRSHTTPError("get http")
raise
OTRSHTTPError
(
"
Failed to access OTRS. Check Hostname, Proxy, SSL Certificate!
"
"
Error with http communication: {0}
"
.
format
(
err
))
# TODO 2016-04-11 (RH): Just assume that we get valid JSON
self
.
ticket_json
=
json
.
loads
(
self
.
response
.
content
.
decode
()
)
#
logger.debug(self.
data
_json)
try
:
self
.
ticket_json
=
self
.
response
.
json
()
logger
.
debug
(
self
.
ticket
_json
)
if
self
.
ticket_json
.
get
(
"
Error
"
)
is
not
None
:
if
self
.
ticket_json
.
get
(
"
TicketID
"
)
is
not
None
:
self
.
response_type
=
"
TicketID
"
elif
self
.
ticket_json
.
get
(
"
Error
"
)
is
not
None
:
self
.
response_type
=
"
Error
"
patch_request_result_bool
=
True
elif
self
.
ticket_json
.
get
(
"
TicketID
"
)
is
not
None
:
elif
not
len
(
self
.
ticket_json
):
self
.
response_type
=
"
TicketID
"
patch_request_result_bool
=
True
logger
.
debug
(
"
search found no results
"
)
return
False
else
:
logger
.
error
(
"
Unknown key in response JSON DICT!
"
)
patch_request_result_bool
=
False
logger
.
debug
(
"
Type was set to: {0}
"
.
format
(
self
.
response_type
)
)
# critical error: Unknown response from OTRS API - FAIL NOW!
raise
ResponseJSONParseError
(
"
Unknown key in response JSON DICT!
"
)
# critical error: HTTP request resulted in an error!
except
Exception
as
err
:
logger
.
error
(
"
Error with http communication: {0}
"
.
format
(
err
))
logger
.
error
(
"
Failed to access OTRS HTTP. Check Hostname, Proxy, SSL Certificate and so on!
"
)
raise
OTRSHTTPError
(
"
update http
"
)
# TODO 2016-04-13 (RH): clean this up!
raise
Exception
(
"
Parse Result to JSON failed.. Message
"
.
format
(
err
))
# Validated response_type
logger
.
debug
(
"
Type was set to: {0}
"
.
format
(
self
.
response_type
))
# critical error: OTRS API sent an error!
if
self
.
response_type
==
"
Error
"
:
logger
.
error
(
self
.
ticket_json
[
"
Error
"
])
logger
.
error
(
"
Failed to access OTRS API. Check Username and Password! Does requested Ticket
"
"
exist?
"
)
raise
OTRSAPIError
(
"
update api
"
)
logger
.
error
(
"
TicketGet API Error. Failed to access OTRS API. Check Username
"
"
and Password! Session ID expired?!
\n
"
"
OTRS Error_Code; OTRS Message:
"
"
{0}; {1}
"
.
format
(
self
.
ticket_json
[
"
Error
"
][
"
ErrorCode
"
],
self
.
ticket_json
[
"
Error
"
][
"
ErrorMessage
"
]))
raise
OTRSAPIError
(
"
TicketGet API Error. Failed to access OTRS API. Check Username
"
"
and Password! Session ID expired?!
\n
"
"
OTRS Error_Code; OTRS Message:
"
"
{0}; {1}
"
.
format
(
self
.
ticket_json
[
"
Error
"
][
"
ErrorCode
"
],
self
.
ticket_json
[
"
Error
"
][
"
ErrorMessage
"
]))
return
True
@staticmethod
def
_minutes_since_midnight
(
now_obj
):
"""
helper function for getting amount of minutes since midnight
Args:
now_obj (datetime.datetime): e.g. from datetime.datetime.now
Returns:
integer
"""
seconds_since_midnight
=
(
now_obj
-
now_obj
.
replace
(
hour
=
0
,
minute
=
0
,
second
=
0
,
microsecond
=
0
)).
total_seconds
()
minutes_since_midnight
=
int
(
seconds_since_midnight
/
60
)
return
minutes_since_midnight
return
patch_request_result_bool
# GenericInterface :: Operation::Ticket :: TicketUpdate
# public methods
# * ticket_update_add_article
# * ticket_update_set_dynamic_field_value
# * ticket_update_set_state
# * ticket_update_set_state_pending
# * ticket_update_set_title
def
ticket_update_add_article
(
self
,
ticket_id
,
article_subject
,
article_body
):
"""
ticket_update_by_ticket_id_set_scout_id
...
...
@@ -789,4 +774,73 @@ class Client(object):
logger
.
error
(
"
Exception: {0}
"
.
format
(
err
))
return
False
def
_update_by_ticket_id
(
self
,
url
,
payload
):
"""
update_by_ticket_id
Args:
url (str):
payload (dict):
Raises:
OTRSHTTPError
OTRSAPIError
Returns:
True if update was successful; otherwise False
"""
"""
TODO 2016-04-13 (RH):why not?!
headers = {
"
Content-Type
"
:
"
application/json
"
}
json_payload = json.dumps(payload)
"""
try
:
self
.
response
=
requests
.
patch
(
url
,
proxies
=
proxies
,
data
=
json
.
dumps
(
payload
),
verify
=
self
.
https_verify
)
# critical error: HTTP request resulted in an error!
except
Exception
as
err
:
logger
.
error
(
"
Failed to access OTRS. Check Hostname, Proxy, SSL Certificate!
"
"
Error with http communication: {0}
"
.
format
(
err
))
#raise OTRSHTTPError("get http")
raise
OTRSHTTPError
(
"
Failed to access OTRS. Check Hostname, Proxy, SSL Certificate!
"
"
Error with http communication: {0}
"
.
format
(
err
))
try
:
self
.
ticket_json
=
self
.
response
.
json
()
logger
.
debug
(
self
.
ticket_json
)
if
self
.
ticket_json
.
get
(
"
TicketID
"
)
is
not
None
:
self
.
response_type
=
"
TicketID
"
elif
self
.
ticket_json
.
get
(
"
Error
"
)
is
not
None
:
self
.
response_type
=
"
Error
"
else
:
logger
.
error
(
"
Unknown key in response JSON DICT!
"
)
# critical error: Unknown response from OTRS API - FAIL NOW!
raise
ResponseJSONParseError
(
"
Unknown key in response JSON DICT!
"
)
except
Exception
as
err
:
# TODO 2016-04-13 (RH): clean this up!
raise
Exception
(
"
Parse Result to JSON failed.. Message
"
.
format
(
err
))
# Validated response_type
logger
.
debug
(
"
Type was set to: {0}
"
.
format
(
self
.
response_type
))
if
self
.
response_type
==
"
Error
"
:
logger
.
error
(
"
TicketUpdate API Error. Failed to access OTRS API. Check Username
"
"
and Password! Session ID expired?!
\n
"
"
OTRS Error_Code; OTRS Message:
"
"
{0}; {1}
"
.
format
(
self
.
ticket_json
[
"
Error
"
][
"
ErrorCode
"
],
self
.
ticket_json
[
"
Error
"
][
"
ErrorMessage
"
]))
raise
OTRSAPIError
(
"
TicketUpdate API Error. Failed to access OTRS API. Check Username
"
"
and Password! Session ID expired?!
\n
"
"
OTRS Error_Code; OTRS Message:
"
"
{0}; {1}
"
.
format
(
self
.
ticket_json
[
"
Error
"
][
"
ErrorCode
"
],
self
.
ticket_json
[
"
Error
"
][
"
ErrorMessage
"
]))
return
True
# EOF
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment