Skip to content

Commit cb7b244

Browse files
authored
remove requests (#168)
1 parent 794a90b commit cb7b244

File tree

2 files changed

+16
-3
lines changed

2 files changed

+16
-3
lines changed

datadog_lambda/extension.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,15 @@
11
import logging
2-
import requests
32
from os import path
43

4+
try:
5+
# only available in python 3
6+
# not an issue since the extension is not compatible with python 2.x runtime
7+
# https://docs.aws.amazon.com/lambda/latest/dg/using-extensions.html
8+
import urllib.request
9+
except ImportError:
10+
# safe since both calls to urllib are protected with try/expect and will return false
11+
urllib = None
12+
513
AGENT_URL = "http://127.0.0.1:8124"
614
HELLO_PATH = "/lambda/hello"
715
FLUSH_PATH = "/lambda/flush"
@@ -14,7 +22,7 @@ def is_extension_running():
1422
if not path.exists(EXTENSION_PATH):
1523
return False
1624
try:
17-
requests.get(AGENT_URL + HELLO_PATH)
25+
urllib.request.urlopen(AGENT_URL + HELLO_PATH)
1826
except Exception as e:
1927
logger.debug("Extension is not running, returned with error %s", e)
2028
return False
@@ -23,7 +31,8 @@ def is_extension_running():
2331

2432
def flush_extension():
2533
try:
26-
requests.post(AGENT_URL + FLUSH_PATH, data={})
34+
req = urllib.request.Request(AGENT_URL + FLUSH_PATH, "".encode("ascii"))
35+
urllib.request.urlopen(req)
2736
except Exception as e:
2837
logger.debug("Failed to flush extension, returned with error %s", e)
2938
return False

tests/test_extension.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import os
2+
import sys
23
import unittest
34
import httpretty
45

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

2021

2122
class TestLambdaExtension(unittest.TestCase):
23+
# do not execute tests for Python v2.x
24+
__test__ = sys.version_info >= (3, 0)
25+
2226
@patch("datadog_lambda.extension.EXTENSION_PATH", os.path.abspath(__file__))
2327
def test_is_extension_running_true(self):
2428
httpretty.enable()

0 commit comments

Comments
 (0)