Skip to content
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

provide http_get via requests. #1012

Merged
merged 12 commits into from
Dec 5, 2023
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
1 change: 1 addition & 0 deletions .github/workflows/ci-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ jobs:
sudo ldconfig -v && \
poetry config virtualenvs.create false && \
poetry install --no-interaction && \
poetry install --no-interaction -C utils/mavlink-api && \
sudo dpkg -r --force-depends python3-numpy
- name: test gamutrf-samples2raw
env:
Expand Down
47 changes: 20 additions & 27 deletions gamutrf/mqtt_reporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
import time

import gpsd
import httpx
import paho.mqtt.client as mqtt
from gamutrf.utils import http_get


class MQTTReporter:
Expand Down Expand Up @@ -66,23 +66,19 @@

def get_heading(self):
if self.use_external_heading:
try:
self.heading = float(
json.loads(
httpx.get(
f"http://{self.external_gps_server}:{self.external_gps_server_port}/heading"
).text
)["heading"]
)
except Exception as err:
logging.error("could not update external heading: %s", err)
heading_result = http_get(

Check warning on line 69 in gamutrf/mqtt_reporter.py

View check run for this annotation

Codecov / codecov/patch

gamutrf/mqtt_reporter.py#L69

Added line #L69 was not covered by tests
f"http://{self.external_gps_server}:{self.external_gps_server_port}/heading"
)
if heading_result is None:
logging.error("could not update external heading")

Check warning on line 73 in gamutrf/mqtt_reporter.py

View check run for this annotation

Codecov / codecov/patch

gamutrf/mqtt_reporter.py#L72-L73

Added lines #L72 - L73 were not covered by tests
else:
self.heading = float(json.loads(heading_result.text)["heading"])

Check warning on line 75 in gamutrf/mqtt_reporter.py

View check run for this annotation

Codecov / codecov/patch

gamutrf/mqtt_reporter.py#L75

Added line #L75 was not covered by tests
else:
try:
self.heading = str(
float(httpx.get(f"http://{self.gps_server}:8000/v1/heading").text)
)
except Exception as err:
logging.error("could not update heading: %s", err)
heading_result = http_get(f"http://{self.gps_server}:8000/v1/heading")
if heading_result is None:
logging.error("could not update heading")

Check warning on line 79 in gamutrf/mqtt_reporter.py

View check run for this annotation

Codecov / codecov/patch

gamutrf/mqtt_reporter.py#L77-L79

Added lines #L77 - L79 were not covered by tests
else:
self.heading = str(float(heading_result.text))

Check warning on line 81 in gamutrf/mqtt_reporter.py

View check run for this annotation

Codecov / codecov/patch

gamutrf/mqtt_reporter.py#L81

Added line #L81 was not covered by tests

def add_gps(self, publish_args):
if not self.gps_configured:
Expand All @@ -100,13 +96,13 @@

# Use external external GPS
if self.use_external_gps:
try:
self.external_gps_msg = json.loads(
httpx.get(
f"http://{self.external_gps_server}:{self.external_gps_server_port}/gps-data"
).text
)

external_gps_msg = http_get(

Check warning on line 99 in gamutrf/mqtt_reporter.py

View check run for this annotation

Codecov / codecov/patch

gamutrf/mqtt_reporter.py#L99

Added line #L99 was not covered by tests
f"http://{self.external_gps_server}:{self.external_gps_server_port}/gps-data"
)
if external_gps_msg is None:
logging.error("could not update with external GPS")

Check warning on line 103 in gamutrf/mqtt_reporter.py

View check run for this annotation

Codecov / codecov/patch

gamutrf/mqtt_reporter.py#L102-L103

Added lines #L102 - L103 were not covered by tests
else:
self.external_gps_msg = json.loads(external_gps_msg.text)

Check warning on line 105 in gamutrf/mqtt_reporter.py

View check run for this annotation

Codecov / codecov/patch

gamutrf/mqtt_reporter.py#L105

Added line #L105 was not covered by tests
publish_args.update(
{
"position": (
Expand All @@ -121,9 +117,6 @@
}
)

except Exception as err:
logging.error("could not update with external GPS: %s", err)

# Use internal GPIO GPS
else:
try:
Expand Down
12 changes: 2 additions & 10 deletions gamutrf/sigfinder.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import falcon
import jinja2
import numpy as np
import requests
import schedule

from prometheus_client import Counter
Expand All @@ -25,7 +24,7 @@
from gamutrf.sigwindows import parse_freq_excluded
from gamutrf.sigwindows import find_sig_windows
from gamutrf.sigwindows import ROLLING_FACTOR
from gamutrf.utils import rotate_file_n, SCAN_FRES
from gamutrf.utils import http_get, rotate_file_n, SCAN_FRES
from gamutrf.zmqreceiver import ZmqReceiver, parse_scanners


Expand Down Expand Up @@ -255,14 +254,7 @@


def recorder_req(recorder, recorder_args, timeout):
url = f"{recorder}/v1/{recorder_args}"
try:
req = requests.get(url, timeout=timeout)
logging.debug(str(req))
return req
except (requests.exceptions.ConnectionError, requests.exceptions.Timeout) as err:
logging.debug(str(err))
return None
return http_get(f"{recorder}/v1/{recorder_args}", timeout=timeout)

Check warning on line 257 in gamutrf/sigfinder.py

View check run for this annotation

Codecov / codecov/patch

gamutrf/sigfinder.py#L257

Added line #L257 was not covered by tests


def get_freq_exclusions(args):
Expand Down
12 changes: 12 additions & 0 deletions gamutrf/utils.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
#!/usr/bin/python3
import logging
import os
import re
import sys
from pathlib import Path

import numpy as np
import requests

MTU = 8962
SCAN_FRES = 1e4
Expand Down Expand Up @@ -121,3 +123,13 @@
for path in Path(filedir).rglob(glob)
if not os.path.basename(path).startswith(".")
]


def http_get(url, timeout=10):
try:
req = requests.get(url, timeout=timeout)
logging.debug(str(req))
return req
except (requests.exceptions.ConnectionError, requests.exceptions.Timeout) as err:
logging.error(f"{url}: {str(err)}")
return None

Check warning on line 135 in gamutrf/utils.py

View check run for this annotation

Codecov / codecov/patch

gamutrf/utils.py#L129-L135

Added lines #L129 - L135 were not covered by tests
Loading
Loading