Skip to content

Commit

Permalink
A (rather hacky) proposed fix for #379
Browse files Browse the repository at this point in the history
  • Loading branch information
csparpa committed Sep 2, 2021
1 parent 788f26a commit 099ef43
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 5 deletions.
9 changes: 6 additions & 3 deletions pyowm/agroapi10/agro_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
from pyowm.agroapi10.polygon import Polygon, GeoPolygon
from pyowm.agroapi10.search import SatelliteImagerySearchResultSet
from pyowm.agroapi10.soil import Soil
from pyowm.agroapi10.uris import ROOT_AGRO_API, POLYGONS_URI, NAMED_POLYGON_URI, SOIL_URI, SATELLITE_IMAGERY_SEARCH_URI
from pyowm.agroapi10.uris import ROOT_AGRO_API, ROOT_DOWNLOAD_PNG_API, ROOT_DOWNLOAD_GEOTIFF_API, POLYGONS_URI, \
NAMED_POLYGON_URI, SOIL_URI, SATELLITE_IMAGERY_SEARCH_URI
from pyowm.commons.http_client import HttpClient
from pyowm.commons.image import Image
from pyowm.commons.tile import Tile
Expand All @@ -33,6 +34,8 @@ def __init__(self, API_key, config):
self.API_key = API_key
assert isinstance(config, dict)
self.http_client = HttpClient(API_key, config, ROOT_AGRO_API)
self.geotiff_downloader_http_client = HttpClient(self.API_key, config, ROOT_DOWNLOAD_GEOTIFF_API)
self.png_downloader_http_client = HttpClient(self.API_key, config, ROOT_DOWNLOAD_PNG_API)

def agro_api_version(self):
return AGRO_API_VERSION
Expand Down Expand Up @@ -279,14 +282,14 @@ def download_satellite_image(self, metaimage, x=None, y=None, zoom=None, palette
# polygon PNG
if isinstance(metaimage, MetaPNGImage):
prepared_url = metaimage.url
status, data = self.http_client.get_png(
status, data = self.png_downloader_http_client.get_png(
prepared_url, params=params)
img = Image(data, metaimage.image_type)
return SatelliteImage(metaimage, img, downloaded_on=timestamps.now(timeformat='unix'), palette=palette)
# GeoTIF
elif isinstance(metaimage, MetaGeoTiffImage):
prepared_url = metaimage.url
status, data = self.http_client.get_geotiff(
status, data = self.geotiff_downloader_http_client.get_geotiff(
prepared_url, params=params)
img = Image(data, metaimage.image_type)
return SatelliteImage(metaimage, img, downloaded_on=timestamps.now(timeformat='unix'), palette=palette)
Expand Down
2 changes: 2 additions & 0 deletions pyowm/agroapi10/uris.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
# -*- coding: utf-8 -*-

ROOT_AGRO_API = 'agromonitoring.com/agro/1.0'
ROOT_DOWNLOAD_PNG_API = 'agromonitoring.com/image/1.0'
ROOT_DOWNLOAD_GEOTIFF_API = 'agromonitoring.com/data/1.0'

# Polygons API subset
POLYGONS_URI = 'polygons'
Expand Down
16 changes: 14 additions & 2 deletions pyowm/commons/http_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,8 +144,14 @@ def get_json(self, path, params=None, headers=None):
raise exceptions.ParseAPIResponseError('Impossible to parse API response data')

def get_png(self, path, params=None, headers=None):
# check URL fromt the metaimage: if it looks like a complete URL, use that one (I know, it's a hack...)
try:
partial_path = path.split(self.root_uri)[1].lstrip('/')
except:
partial_path = '' # fallback so that a 404 is issued

builder = HttpRequestBuilder(self.root_uri, self.api_key, self.config, has_subdomains=self.admits_subdomains)\
.with_path(path)\
.with_path(partial_path)\
.with_api_key()\
.with_language()\
.with_query_params(params if params is not None else dict())\
Expand All @@ -170,8 +176,14 @@ def get_png(self, path, params=None, headers=None):
'API response data')

def get_geotiff(self, path, params=None, headers=None):
# check URL fromt the metaimage: if it looks like a complete URL, use that one (I know, it's a hack...)
try:
partial_path = path.split(self.root_uri)[1].lstrip('/')
except:
partial_path = '' # fallback so that a 404 is issued

builder = HttpRequestBuilder(self.root_uri, self.api_key, self.config, has_subdomains=self.admits_subdomains)\
.with_path(path)\
.with_path(partial_path)\
.with_api_key()\
.with_language()\
.with_query_params(params if params is not None else dict())\
Expand Down
2 changes: 2 additions & 0 deletions tests/unit/agroapi10/test_agro_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,8 @@ class TestAgroManager(unittest.TestCase):
def factory(self, _kls):
sm = AgroManager('APIKey', DEFAULT_CONFIG)
sm.http_client = _kls('APIKey', DEFAULT_CONFIG, 'fake-root.com')
sm.geotiff_downloader_http_client = _kls('APIKey', DEFAULT_CONFIG, 'fake-data-root.com')
sm.png_downloader_http_client = _kls('APIKey', DEFAULT_CONFIG, 'fake-image-root.com')
return sm

def test_instantiation_with_wrong_params(self):
Expand Down

0 comments on commit 099ef43

Please sign in to comment.