Skip to content

Commit

Permalink
Merge pull request #56 from Moesif/refactor-use-datetime
Browse files Browse the repository at this point in the history
Refactor: Use datetime.utcnow for request/response time
  • Loading branch information
dkm199 authored May 11, 2021
2 parents 38fc459 + d77486a commit 669899e
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 8 deletions.
8 changes: 4 additions & 4 deletions moesifdjango/middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ def __call__(self, request):
transaction_id = None

# Request Time
req_time = timezone.now()
req_time = datetime.utcnow().strftime("%Y-%m-%dT%H:%M:%S.%f")[:-3]

try:
request._mo_body = request.body
Expand Down Expand Up @@ -197,14 +197,14 @@ def __call__(self, request):
rsp_body, rsp_body_transfer_encoding = self.logger_helper.prepare_response_body(response, rsp_headers, self.LOG_BODY,
self.middleware_settings)
# Response Time
rsp_time = timezone.now()
rsp_time = datetime.utcnow().strftime("%Y-%m-%dT%H:%M:%S.%f")[:-3]

# Prepare Event Request Model
event_req = self.event_mapper.to_request(req_time.isoformat(), uri,request.method, self.api_version, ip_address,
event_req = self.event_mapper.to_request(req_time, uri,request.method, self.api_version, ip_address,
req_headers, req_body, req_body_transfer_encoding)

# Prepare Event Response Model
event_rsp = self.event_mapper.to_response(rsp_time.isoformat(), response.status_code, rsp_headers, rsp_body,
event_rsp = self.event_mapper.to_response(rsp_time, response.status_code, rsp_headers, rsp_body,
rsp_body_transfer_encoding)

# User Id
Expand Down
8 changes: 4 additions & 4 deletions moesifdjango/middleware_pre19.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ def schedule_event_background_job(self):

@classmethod
def process_request(cls, request):
request.moesif_req_time = timezone.now()
request.moesif_req_time = datetime.utcnow().strftime("%Y-%m-%dT%H:%M:%S.%f")[:-3]
try:
request._mo_body = request.body
request._stream = BytesIO(request.body)
Expand Down Expand Up @@ -199,14 +199,14 @@ def process_response(self, request, response):
self.middleware_settings)

# Response Time
rsp_time = timezone.now()
rsp_time = datetime.utcnow().strftime("%Y-%m-%dT%H:%M:%S.%f")[:-3]

# Prepare Event Request Model
event_req = self.event_mapper.to_request(req_time.isoformat(), uri, request.method, self.api_version,
event_req = self.event_mapper.to_request(req_time, uri, request.method, self.api_version,
ip_address, req_headers, req_body, req_body_transfer_encoding)

# Prepare Event Response Model
event_rsp = self.event_mapper.to_response(rsp_time.isoformat(), response.status_code, rsp_headers, rsp_body,
event_rsp = self.event_mapper.to_response(rsp_time, response.status_code, rsp_headers, rsp_body,
rsp_body_transfer_encoding)

# User Id
Expand Down

0 comments on commit 669899e

Please sign in to comment.