Skip to content

Remove requests for urllib #168

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions datadog_lambda/extension.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
import logging
import requests
from os import path

try:
# only available in python 3
# not an issue since the extension is not compatible with python 2.x runtime
# https://docs.aws.amazon.com/lambda/latest/dg/using-extensions.html
import urllib.request
except ImportError:
# safe since both calls to urllib are protected with try/expect and will return false
urllib = None

AGENT_URL = "http://127.0.0.1:8124"
HELLO_PATH = "/lambda/hello"
FLUSH_PATH = "/lambda/flush"
Expand All @@ -14,7 +22,7 @@ def is_extension_running():
if not path.exists(EXTENSION_PATH):
return False
try:
requests.get(AGENT_URL + HELLO_PATH)
urllib.request.urlopen(AGENT_URL + HELLO_PATH)
except Exception as e:
logger.debug("Extension is not running, returned with error %s", e)
return False
Expand All @@ -23,7 +31,8 @@ def is_extension_running():

def flush_extension():
try:
requests.post(AGENT_URL + FLUSH_PATH, data={})
req = urllib.request.Request(AGENT_URL + FLUSH_PATH, "".encode("ascii"))
urllib.request.urlopen(req)
except Exception as e:
logger.debug("Failed to flush extension, returned with error %s", e)
return False
Expand Down
4 changes: 4 additions & 0 deletions tests/test_extension.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import os
import sys
import unittest
import httpretty

Expand All @@ -19,6 +20,9 @@ def exceptionCallback(request, uri, headers):


class TestLambdaExtension(unittest.TestCase):
# do not execute tests for Python v2.x
__test__ = sys.version_info >= (3, 0)

@patch("datadog_lambda.extension.EXTENSION_PATH", os.path.abspath(__file__))
def test_is_extension_running_true(self):
httpretty.enable()
Expand Down