|
45 | 45 | _CRS = "EPSG:4269" |
46 | 46 |
|
47 | 47 |
|
| 48 | +def _parse_json_or_raise(response: requests.Response) -> pd.DataFrame: |
| 49 | + """Parse a JSON NWIS response, raising a helpful error on HTML responses.""" |
| 50 | + try: |
| 51 | + return _read_json(response.json()) |
| 52 | + except (ValueError, JSONDecodeError) as e: |
| 53 | + text_lower = response.text.lower() |
| 54 | + content_type = response.headers.get("Content-Type", "").lower() |
| 55 | + if ( |
| 56 | + "<html>" in text_lower |
| 57 | + or "<!doctype" in text_lower |
| 58 | + or "text/html" in content_type |
| 59 | + ): |
| 60 | + raise ValueError( |
| 61 | + f"Received HTML response instead of JSON from {response.url} " |
| 62 | + f"(Status: {response.status_code}). This often indicates " |
| 63 | + "that the service is currently unavailable." |
| 64 | + ) from e |
| 65 | + raise |
| 66 | + |
| 67 | + |
48 | 68 | def format_response( |
49 | 69 | df: pd.DataFrame, service: str | None = None, **kwargs |
50 | 70 | ) -> pd.DataFrame: |
@@ -482,20 +502,7 @@ def get_dv( |
482 | 502 | kwargs["multi_index"] = multi_index |
483 | 503 |
|
484 | 504 | response = query_waterservices("dv", format="json", ssl_check=ssl_check, **kwargs) |
485 | | - try: |
486 | | - df = _read_json(response.json()) |
487 | | - except (ValueError, JSONDecodeError) as e: |
488 | | - if ( |
489 | | - "<html>" in response.text.lower() |
490 | | - or "<!doctype" in response.text.lower() |
491 | | - or "text/html" in response.headers.get("Content-Type", "").lower() |
492 | | - ): |
493 | | - raise ValueError( |
494 | | - f"Received HTML response instead of JSON from {response.url} " |
495 | | - f"(Status: {response.status_code}). This often indicates " |
496 | | - "that the service is currently unavailable." |
497 | | - ) from e |
498 | | - raise |
| 505 | + df = _parse_json_or_raise(response) |
499 | 506 |
|
500 | 507 | return format_response(df, **kwargs), NWIS_Metadata(response, **kwargs) |
501 | 508 |
|
@@ -681,20 +688,7 @@ def get_iv( |
681 | 688 | service="iv", format="json", ssl_check=ssl_check, **kwargs |
682 | 689 | ) |
683 | 690 |
|
684 | | - try: |
685 | | - df = _read_json(response.json()) |
686 | | - except (ValueError, JSONDecodeError) as e: |
687 | | - if ( |
688 | | - "<html>" in response.text.lower() |
689 | | - or "<!doctype" in response.text.lower() |
690 | | - or "text/html" in response.headers.get("Content-Type", "").lower() |
691 | | - ): |
692 | | - raise ValueError( |
693 | | - f"Received HTML response instead of JSON from {response.url} " |
694 | | - f"(Status: {response.status_code}). This often indicates " |
695 | | - "that the service is currently unavailable." |
696 | | - ) from e |
697 | | - raise |
| 691 | + df = _parse_json_or_raise(response) |
698 | 692 | return format_response(df, **kwargs), NWIS_Metadata(response, **kwargs) |
699 | 693 |
|
700 | 694 |
|
@@ -915,13 +909,19 @@ def get_record( |
915 | 909 | """ |
916 | 910 | _check_sites_value_types(sites) |
917 | 911 |
|
918 | | - defunct_services = ["measurements", "gwlevels", "pmcodes", "water_use"] |
919 | | - if service in defunct_services: |
| 912 | + defunct_replacements = { |
| 913 | + "measurements": "`waterdata.get_field_measurements`", |
| 914 | + "gwlevels": "`waterdata.get_field_measurements`", |
| 915 | + "pmcodes": "`waterdata.get_reference_table`", |
| 916 | + "water_use": "no replacement available", |
| 917 | + } |
| 918 | + if service in defunct_replacements: |
920 | 919 | raise NameError( |
921 | | - f"The NWIS service '{service}' is no longer supported by get_record." |
| 920 | + f"The NWIS service '{service}' is no longer supported by " |
| 921 | + f"get_record. Use {defunct_replacements[service]} instead." |
922 | 922 | ) |
923 | 923 |
|
924 | | - if service not in WATERSERVICES_SERVICES + WATERDATA_SERVICES + defunct_services: |
| 924 | + if service not in WATERSERVICES_SERVICES + WATERDATA_SERVICES: |
925 | 925 | raise TypeError(f"Unrecognized service: {service}") |
926 | 926 |
|
927 | 927 | if service == "iv": |
|
0 commit comments