Consider the example of creating a universal data source connector:
Create the Docker container on mobile platform server using the following files:
Dockerfile:
FROM python:3.12.5-slim-bookworm
ARG UID=1000
ARG GID=0
# Create and set up user account
RUN useradd -m -u ${UID} -g ${GID} -s /bin/bash fmp
# Copy and install Python packages via poetry
RUN python -m pip install fastapi==0.112.2 uvicorn==0.30.6 python-multipart==0.0.20
COPY --chown=${UID}:${GID} src/ /home/fmp/
USER ${UID}
WORKDIR /home/fmp
CMD python main.py
main.py:
from fastapi import FastAPI, UploadFile, File
from fastapi.security import HTTPBasic
import uvicorn
import csv
from io import StringIO
from typing import Dict
app = FastAPI()
security = HTTPBasic()
def clean_key(key: str) -> str:
"""Deletes spaces from key"""
return key.strip().replace(" ", "")
def clean_value(value: str) -> str:
"""Deletes spaces from value"""
return value.strip() if isinstance(value, str) else value
def clean_row(row: Dict) -> Dict:
"""Clears keys and values in string"""
return {clean_key(key): clean_value(value) for key, value in row.items()}
@app.post("/get-json-from-csv/")
async def convert_csv_to_json(file: UploadFile = File(...)):
try:
# Read CSV file contents
contents = await file.read()
decoded_contents = contents.decode('utf-8')
# Create StringIO object to work with CSV
csv_string = StringIO(decoded_contents)
# Read CSV, clear spaces and convert to JSON
reader = csv.DictReader(csv_string)
json_data = [clean_row(row) for row in reader]
except Exception as e:
return {"error": str(e)}
return {"data": json_data}
if __name__ == "__main__":
uvicorn.run("main:app", host="0.0.0.0", port=8007, reload=True)
docker-compose.universal-connector.yaml:
version: '3.7'
services:
request_headers:
image: ${DOCKER_REGISTRY}/request-headers
build:
args:
HTTP_PROXY: ${HTTP_PROXY}
HTTPS_PROXY: ${HTTPS_PROXY}
NO_RPOXY: ${NO_RPOXY}
context: .
ports:
- 8007:8007
volumes:
- ./src:/home/fmp
command:
python main.py
networks: # Specify the same network as of mobile platform server
- backend
Start the Docker container simultaneously with the mobile platform server using the created configuration file docker-compose.universal-connector.yaml:
docker-compose -f docker-compose.standalone.yml -f docker-compose.universal-connector.yaml up -d --build
Set up connection with the JSON or WEB data source. In the Port parameter specify the 8007 port number specified in the file docker-compose.universal-connector.yaml, and in the Host parameter specify IP address or server name specified in the .env file.
Import the resource of the JSON or WEB data source, to which a user query will be sent from a mobile device, for example, get-json-from-csv.
After executing the operations, if the universal connector is used, the SCV files, which data is returned in the JSON format, are sent to the imported resource.
See also:
Creating a Universal Data Source Connector | Creating a Docker Container