Welcome to country-state-city microservice

Python Usage Examples

Here are some Python examples using the requests library to interact with the four endpoints.

1. Get Coords

Endpoint

GET https://api.geocodxyz.rebuscando.info/public/getCoords

Example

 

import requests

# Define the endpoint URL
url = "http://api.geocodxyz.rebuscando.info/public/getCoords" # Define the parameters params = { "postaladdress": "1600 Amphitheatre Parkway", "zipcode": "94043", "city": "Mountain View", "state": "CA", "country": "USA", "apikey": "your_api_key" } # Send the GET request response = requests.get(url, params=params) # Check the response status code and handle accordingly if response.status_code == 200: print("Coordinates:", response.json()) elif response.status_code == 401: print("Error: Apikey not valid") elif response.status_code == 500: print("Error: Internal server error") else: print("Error: Unexpected response code:", response.status_code)  

2. Get Address

Endpoint

GET https://api.geocodxyz.rebuscando.info/public/getAddress

Example

import requests

idstate = 5109 # Example state ID
url = f'https://api.geocodxyz.rebuscando.info/public/getCities?idstate={idstate}'

try:
response = requests.get(url)
response.raise_for_status() # Check if the request was successful
cities = response.json()
print(cities)
except requests.exceptions.HTTPError as http_err:
print(f'HTTP error occurred: {http_err}')
except Exception as err:
print(f'Other error occurred: {err}')

3. Upload File

Endpoint

POST https://api.geocodxyz.rebuscando.info//public/uploadFIle

Example



import requests

# Define the endpoint URL
url = "http://api.geocodxyz.rebuscando.info/public/uploadFile" # Define the file path and parameters file_path = "path/to/your/file.csv" params = { "reversed": "true", # or "false" "apikey": "your_api_key" } # Read the file and prepare the files dictionary with open(file_path, 'rb') as f: files = { 'file': (file_path, f) } # Send the POST request response = requests.post(url, files=files, data=params) # Check the response status code and handle accordingly if response.status_code == 200: print("File uploaded successfully") print("Response:", response.json()) elif response.status_code == 401: print("Error: Apikey not valid") elif response.status_code == 406: print("Error: You must wait to upload another file") elif response.status_code == 500: print("Error: Internal server error") else: print("Error: Unexpected response code:", response.status_code)  

 

These examples demonstrate how to make GET requests to each of the endpoints, handle the response, and manage errors using Python and the requests library.

To run these examples, you need to install the requests library, which can be done using pip:

pip install requests

 

You can then run the examples in your Python environment.