hwakabh / random-travelers

People who make thier decisions decided by Cloud Native
https://random-travelers.com
MIT License
0 stars 0 forks source link

Fix KeyError with interactions of MySQL #60

Closed hwakabh closed 9 months ago

hwakabh commented 9 months ago

AsIs

When we call /api/v1/shuffle, the backend returns 500 Error, and this is because FastAPI would got KeyError on endpoint logics. (Ref in #35 )

ToDo

(Optional) Acceptance Criteria

Any API response expected in success case will be returned from /api/v1/shuffle

hwakabh commented 9 months ago

The issues here is that current code base have provided wrong query parameters for restcountries.com in app/api/v1/cruds.py.

url = 'https://restcountries.com/v3.1/all?fields=region;name'
res = urllib.request.urlopen(url)

As described in the docs, we need to list with , instead of ;, when we would like to fetch multiple fields from the API. So correct string of url is:


url = 'https://restcountries.com/v3.1/all?fields=region,name'

and this request can get responses below. Be aware that key name is nested in v3.1 API so the our codes should follow this when accessing them.

[
  {
    "name": {
      "common": "Andorra",
      "official": "Principality of Andorra",
      "nativeName": {
        "cat": {
          "official": "Principat d'Andorra",
          "common": "Andorra"
        }
      }
    },
    "region": "Europe"
  },
  {
    "name": {
      "common": "French Southern and Antarctic Lands",
      "official": "Territory of the French Southern and Antarctic Lands",
      "nativeName": {
        "fra": {
          "official": "Territoire des Terres australes et antarctiques françaises",
          "common": "Terres australes et antarctiques françaises"
        }
      }
    },
    "region": "Antarctic"
  },
// (...)
  {
    "name": {
      "common": "Seychelles",
      "official": "Republic of Seychelles",
      "nativeName": {
        "crs": {
          "official": "Repiblik Sesel",
          "common": "Sesel"
        },
        "eng": {
          "official": "Republic of Seychelles",
          "common": "Seychelles"
        },
        "fra": {
          "official": "République des Seychelles",
          "common": "Seychelles"
        }
      }
    },
    "region": "Africa"
  }
]