|
31 | 31 | "cell_type": "markdown",
|
32 | 32 | "id": "cdf4207d-7b9d-4ecf-9348-44aa390475dd",
|
33 | 33 | "metadata": {
|
34 |
| - "jp-MarkdownHeadingCollapsed": true, |
35 | 34 | "tags": []
|
36 | 35 | },
|
37 | 36 | "source": [
|
|
61 | 60 | "execution_count": 1,
|
62 | 61 | "id": "26fc1dca-2fa9-4b8e-a7b2-77e95d35094b",
|
63 | 62 | "metadata": {},
|
64 |
| - "outputs": [ |
65 |
| - { |
66 |
| - "name": "stdout", |
67 |
| - "output_type": "stream", |
68 |
| - "text": [ |
69 |
| - "SDK_WEB_HOST environment var not set, using localhost\n" |
70 |
| - ] |
71 |
| - } |
72 |
| - ], |
| 63 | + "outputs": [], |
73 | 64 | "source": [
|
74 | 65 | "## Engineer's preperations:\n",
|
75 | 66 | "# Getting data with the Carbon Aware SDK\n",
|
76 | 67 | "import os\n",
|
77 | 68 | "import datetime\n",
|
78 | 69 | "import openapi_client\n",
|
79 | 70 | "from pprint import pprint\n",
|
80 |
| - "from openapi_client.api import carbon_aware_api\n", |
| 71 | + "from openapi_client.apis.tags import carbon_aware_api\n", |
81 | 72 | "from openapi_client.model.emissions_data import EmissionsData\n",
|
82 | 73 | "from dateutil.parser import parse\n",
|
83 | 74 | "from dotenv import load_dotenv\n",
|
|
111 | 102 | },
|
112 | 103 | {
|
113 | 104 | "cell_type": "code",
|
114 |
| - "execution_count": 2, |
| 105 | + "execution_count": 9, |
115 | 106 | "id": "3048c334-a2a6-4e6a-a72b-6e20c57299f3",
|
116 | 107 | "metadata": {},
|
117 | 108 | "outputs": [
|
118 | 109 | {
|
119 |
| - "name": "stdout", |
120 |
| - "output_type": "stream", |
121 |
| - "text": [ |
122 |
| - "[{'duration': '00:05:00',\n", |
123 |
| - " 'location': 'PACE',\n", |
124 |
| - " 'rating': 928.50358139,\n", |
125 |
| - " 'time': datetime.datetime(2022, 8, 15, 20, 55, tzinfo=tzutc())}]\n" |
| 110 | + "ename": "ApiValueError", |
| 111 | + "evalue": "Invalid content_type returned. Content_type='application/json; charset=utf-8' was returned when only {'application/json'} are defined for status_code=200", |
| 112 | + "output_type": "error", |
| 113 | + "traceback": [ |
| 114 | + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", |
| 115 | + "\u001b[0;31mApiValueError\u001b[0m Traceback (most recent call last)", |
| 116 | + "Cell \u001b[0;32mIn [9], line 22\u001b[0m\n\u001b[1;32m 16\u001b[0m query_params \u001b[38;5;241m=\u001b[39m {\n\u001b[1;32m 17\u001b[0m \u001b[38;5;124m'\u001b[39m\u001b[38;5;124mlocation\u001b[39m\u001b[38;5;124m'\u001b[39m: \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mwestcentralus\u001b[39m\u001b[38;5;124m\"\u001b[39m,\n\u001b[1;32m 18\u001b[0m \u001b[38;5;124m'\u001b[39m\u001b[38;5;124mtime\u001b[39m\u001b[38;5;124m'\u001b[39m: parse(\u001b[38;5;124m'\u001b[39m\u001b[38;5;124m2022-08-15T20:55\u001b[39m\u001b[38;5;124m'\u001b[39m),\n\u001b[1;32m 19\u001b[0m }\n\u001b[1;32m 20\u001b[0m \u001b[38;5;28;01mtry\u001b[39;00m:\n\u001b[1;32m 21\u001b[0m \u001b[38;5;66;03m#api_response = api_instance.get_emissions_data_for_location_by_time(location=location, time=start_time, to_time=to_time, duration_minutes=duration_minutes)\u001b[39;00m\n\u001b[0;32m---> 22\u001b[0m api_response \u001b[38;5;241m=\u001b[39m \u001b[43mapi_instance\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mget_emissions_data_for_location_by_time\u001b[49m\u001b[43m(\u001b[49m\u001b[43mquery_params\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43m \u001b[49m\u001b[43mquery_params\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43maccept_content_types\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43m(\u001b[49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[38;5;124;43mapplication/json; charset=utf-8\u001b[39;49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m)\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 24\u001b[0m pprint(api_response)\n\u001b[1;32m 25\u001b[0m \u001b[38;5;28;01mexcept\u001b[39;00m openapi_client\u001b[38;5;241m.\u001b[39mApiException \u001b[38;5;28;01mas\u001b[39;00m e:\n", |
| 117 | + "File \u001b[0;32m/opt/homebrew/Caskroom/miniconda/base/envs/sdktest2/lib/python3.8/site-packages/openapi_client-1.0.0-py3.8.egg/openapi_client/paths/emissions_bylocation/get.py:312\u001b[0m, in \u001b[0;36mGetEmissionsDataForLocationByTime.get_emissions_data_for_location_by_time\u001b[0;34m(self, query_params, accept_content_types, stream, timeout, skip_deserialization)\u001b[0m\n\u001b[1;32m 304\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21mget_emissions_data_for_location_by_time\u001b[39m(\n\u001b[1;32m 305\u001b[0m \u001b[38;5;28mself\u001b[39m,\n\u001b[1;32m 306\u001b[0m query_params: RequestQueryParams \u001b[38;5;241m=\u001b[39m frozendict\u001b[38;5;241m.\u001b[39mfrozendict(),\n\u001b[0;32m (...)\u001b[0m\n\u001b[1;32m 310\u001b[0m skip_deserialization: \u001b[38;5;28mbool\u001b[39m \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;01mFalse\u001b[39;00m,\n\u001b[1;32m 311\u001b[0m ):\n\u001b[0;32m--> 312\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_get_emissions_data_for_location_by_time_oapg\u001b[49m\u001b[43m(\u001b[49m\n\u001b[1;32m 313\u001b[0m \u001b[43m \u001b[49m\u001b[43mquery_params\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mquery_params\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 314\u001b[0m \u001b[43m \u001b[49m\u001b[43maccept_content_types\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43maccept_content_types\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 315\u001b[0m \u001b[43m \u001b[49m\u001b[43mstream\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mstream\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 316\u001b[0m \u001b[43m \u001b[49m\u001b[43mtimeout\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mtimeout\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 317\u001b[0m \u001b[43m \u001b[49m\u001b[43mskip_deserialization\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mskip_deserialization\u001b[49m\n\u001b[1;32m 318\u001b[0m \u001b[43m \u001b[49m\u001b[43m)\u001b[49m\n", |
| 118 | + "File \u001b[0;32m/opt/homebrew/Caskroom/miniconda/base/envs/sdktest2/lib/python3.8/site-packages/openapi_client-1.0.0-py3.8.egg/openapi_client/paths/emissions_bylocation/get.py:250\u001b[0m, in \u001b[0;36mBaseApi._get_emissions_data_for_location_by_time_oapg\u001b[0;34m(self, query_params, accept_content_types, stream, timeout, skip_deserialization)\u001b[0m\n\u001b[1;32m 248\u001b[0m response_for_status \u001b[38;5;241m=\u001b[39m _status_code_to_response\u001b[38;5;241m.\u001b[39mget(\u001b[38;5;28mstr\u001b[39m(response\u001b[38;5;241m.\u001b[39mstatus))\n\u001b[1;32m 249\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m response_for_status:\n\u001b[0;32m--> 250\u001b[0m api_response \u001b[38;5;241m=\u001b[39m \u001b[43mresponse_for_status\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mdeserialize\u001b[49m\u001b[43m(\u001b[49m\u001b[43mresponse\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mapi_client\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mconfiguration\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 251\u001b[0m \u001b[38;5;28;01melse\u001b[39;00m:\n\u001b[1;32m 252\u001b[0m api_response \u001b[38;5;241m=\u001b[39m api_client\u001b[38;5;241m.\u001b[39mApiResponseWithoutDeserialization(response\u001b[38;5;241m=\u001b[39mresponse)\n", |
| 119 | + "File \u001b[0;32m/opt/homebrew/Caskroom/miniconda/base/envs/sdktest2/lib/python3.8/site-packages/openapi_client-1.0.0-py3.8.egg/openapi_client/api_client.py:929\u001b[0m, in \u001b[0;36mOpenApiResponse.deserialize\u001b[0;34m(self, response, configuration)\u001b[0m\n\u001b[1;32m 927\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mcontent \u001b[38;5;129;01mis\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m \u001b[38;5;28;01mNone\u001b[39;00m:\n\u001b[1;32m 928\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m content_type \u001b[38;5;129;01mnot\u001b[39;00m \u001b[38;5;129;01min\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mcontent:\n\u001b[0;32m--> 929\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m ApiValueError(\n\u001b[1;32m 930\u001b[0m \u001b[38;5;124mf\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mInvalid content_type returned. Content_type=\u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;132;01m{\u001b[39;00mcontent_type\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124m was returned \u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[1;32m 931\u001b[0m \u001b[38;5;124mf\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mwhen only \u001b[39m\u001b[38;5;132;01m{\u001b[39;00m\u001b[38;5;28mstr\u001b[39m(\u001b[38;5;28mset\u001b[39m(\u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mcontent))\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m are defined for status_code=\u001b[39m\u001b[38;5;132;01m{\u001b[39;00m\u001b[38;5;28mstr\u001b[39m(response\u001b[38;5;241m.\u001b[39mstatus)\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m\"\u001b[39m\n\u001b[1;32m 932\u001b[0m )\n\u001b[1;32m 933\u001b[0m body_schema \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mcontent[content_type]\u001b[38;5;241m.\u001b[39mschema\n\u001b[1;32m 934\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m body_schema \u001b[38;5;129;01mis\u001b[39;00m \u001b[38;5;28;01mNone\u001b[39;00m:\n\u001b[1;32m 935\u001b[0m \u001b[38;5;66;03m# some specs do not define response content media type schemas\u001b[39;00m\n", |
| 120 | + "\u001b[0;31mApiValueError\u001b[0m: Invalid content_type returned. Content_type='application/json; charset=utf-8' was returned when only {'application/json'} are defined for status_code=200" |
126 | 121 | ]
|
127 | 122 | }
|
128 | 123 | ],
|
129 | 124 | "source": [
|
| 125 | + "import openapi_client\n", |
| 126 | + "from pprint import pprint\n", |
| 127 | + "from openapi_client.apis.tags import carbon_aware_api\n", |
| 128 | + "from openapi_client.model.emissions_data import EmissionsData\n", |
| 129 | + "from dateutil.parser import parse\n", |
| 130 | + "\n", |
| 131 | + "configuration = openapi_client.Configuration(\n", |
| 132 | + " host = \"https://carbon-aware-api.azurewebsites.net\"\n", |
| 133 | + ")\n", |
| 134 | + "\n", |
130 | 135 | "# Enter a context with an instance of the API client\n",
|
131 | 136 | "api_client = openapi_client.ApiClient(configuration)\n",
|
132 | 137 | "# Create an instance of the API class\n",
|
133 | 138 | "api_instance = carbon_aware_api.CarbonAwareApi(api_client)\n",
|
134 | 139 | "\n",
|
135 |
| - "location = \"westcentralus\" # str | (required)\n", |
136 |
| - "start_time = parse('2022-08-15T21:00') # datetime | (optional)\n", |
137 |
| - "to_time = parse('2022-08-15T20:55') # datetime | (optional)\n", |
138 |
| - "duration_minutes = 0 # int | (optional) (default to 0)\n", |
139 |
| - "\n", |
| 140 | + "query_params = {\n", |
| 141 | + " 'location': \"westcentralus\",\n", |
| 142 | + " 'time': parse('2022-08-15T20:55'),\n", |
| 143 | + " }\n", |
140 | 144 | "try:\n",
|
141 |
| - " api_response = api_instance.get_emissions_data_for_location_by_time(location=location, time=start_time, to_time=to_time, duration_minutes=duration_minutes)\n", |
| 145 | + " #api_response = api_instance.get_emissions_data_for_location_by_time(location=location, time=start_time, to_time=to_time, duration_minutes=duration_minutes)\n", |
| 146 | + " api_response = api_instance.get_emissions_data_for_location_by_time(query_params = query_params, accept_content_types=('application/json; charset=utf-8',))\n", |
142 | 147 | "\n",
|
143 | 148 | " pprint(api_response)\n",
|
144 | 149 | "except openapi_client.ApiException as e:\n",
|
|
1427 | 1432 | "name": "python",
|
1428 | 1433 | "nbconvert_exporter": "python",
|
1429 | 1434 | "pygments_lexer": "ipython3",
|
1430 |
| - "version": "3.8.13" |
| 1435 | + "version": "3.8.15" |
1431 | 1436 | }
|
1432 | 1437 | },
|
1433 | 1438 | "nbformat": 4,
|
|
0 commit comments