Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions show-me-the-money/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class
.pytest_cache/

# Local development environment
.env

# IDE files
.vscode/
.idea/

# Dependency directories
.venv/
venv/
env/
lib/
bin/

# Compiled Python files
*.pyc

# Logs
*.log

# Build artifacts
build/
dist/

# Database files
*.db

# Cache files
*.cache

# Environment variables
.env

# Compiled files
*.exe
*.dll
*.so
*.dylib

# Miscellaneous
.DS_Store
2 changes: 2 additions & 0 deletions show-me-the-money/Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,5 @@ There is none, ensure you submit your best attempt and as soon as you possibly c
### How to submit?

Submit a GitHub / Bitbucket repo for review. No ZIP files!

`npm start`
13 changes: 13 additions & 0 deletions show-me-the-money/backend/Dockerfile.backend
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
FROM python:3.12

WORKDIR /app

COPY requirements.txt .

RUN pip install --no-cache-dir --no-deps -r requirements.txt

COPY . .

EXPOSE 8000

CMD ["fastapi", "run", "main.py", "--port", "8000"]
4 changes: 4 additions & 0 deletions show-me-the-money/backend/backend_apis.http
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
### GET request to example server
GET http://localhost:8000/balance-sheet

###
4 changes: 4 additions & 0 deletions show-me-the-money/backend/common.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import os

XERO_HOST = os.getenv("XERO_API_URL", "http://localhost:3000")
XERO_ENDPOINT = f"{XERO_HOST}/api.xro/2.0/Reports/BalanceSheet"
40 changes: 40 additions & 0 deletions show-me-the-money/backend/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import JSONResponse
import httpx

from common import XERO_ENDPOINT

app = FastAPI()


app.add_middleware(
CORSMiddleware,
allow_origins=["http://localhost:3000", "http://localhost:3001", "http://localhost:8080"],
allow_methods=["*"],
allow_headers=["*"],
)


@app.get("/balance-sheet")
async def fetch_data():
url = XERO_ENDPOINT
headers = {
"Content-Type": "application/json",
}
try:
response = httpx.get(url, headers=headers)
response.raise_for_status()
data = response.json()
return data
except httpx.HTTPStatusError as e:
# This block catches errors specifically related to HTTP status codes (400 and above)
print("HTTP status error occurred: ", e)
return JSONResponse(
status_code=e.response.status_code,
content={"error": "HTTP status error: " + str(e)},
)
except httpx.HTTPError as e:
# This block catches other HTTP errors, like connection issues
print("Other HTTP error occurred: ", e)
return JSONResponse(status_code=500, content={"error": "HTTP error: " + str(e)})
39 changes: 39 additions & 0 deletions show-me-the-money/backend/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
annotated-types==0.7.0
anyio==4.4.0
certifi==2024.7.4
click==8.1.7
dnspython==2.6.1
email_validator==2.2.0
fastapi==0.111.0
fastapi-cli==0.0.4
h11==0.14.0
httpcore==1.0.5
httptools==0.6.1
httpx==0.27.0
idna==3.7
iniconfig==2.0.0
Jinja2==3.1.4
markdown-it-py==3.0.0
MarkupSafe==2.1.5
mdurl==0.1.2
orjson==3.10.6
packaging==24.1
pluggy==1.5.0
pydantic==2.8.2
pydantic_core==2.20.1
Pygments==2.18.0
pytest==8.2.2
python-dotenv==1.0.1
python-multipart==0.0.9
PyYAML==6.0.1
rich==13.7.1
shellingham==1.5.4
sniffio==1.3.1
starlette==0.37.2
typer==0.12.3
typing_extensions==4.12.2
ujson==5.10.0
uvicorn==0.30.1
uvloop==0.19.0
watchfiles==0.22.0
websockets==12.0
52 changes: 52 additions & 0 deletions show-me-the-money/backend/test_main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import pytest
from fastapi.testclient import TestClient
import httpx
from httpx import Request, Response
from main import app


@pytest.fixture
def client():
return TestClient(app)


@pytest.fixture
def mock_httpx_get(monkeypatch):
def mock_get(url, headers):
request = Request(method="GET", url=url)
response = Response(200, request=request, json={"data": "mocked_data"})
return response

monkeypatch.setattr("httpx.get", mock_get)


def test_fetch_data_success(client, mock_httpx_get):
response = client.get("/fetch-data")
assert response.status_code == 200
assert response.json() == {"data": "mocked_data"}


def test_fetch_data_http_status_error(client, monkeypatch):
def mock_get(url, headers):
request = Request(method="GET", url=url)
response = Response(404, request=request)
raise httpx.HTTPStatusError(
message="404 Not Found", request=request, response=response
)

monkeypatch.setattr("httpx.get", mock_get)

response = client.get("/fetch-data")
assert response.status_code == 404
assert response.json() == {"error": "HTTP status error: 404 Not Found"}


def test_fetch_data_other_http_error(client, monkeypatch):
def mock_get(url, headers):
raise httpx.HTTPError("Connection error")

monkeypatch.setattr("httpx.get", mock_get)

response = client.get("/fetch-data")
assert response.status_code == 500
assert response.json() == {"error": "HTTP error: Connection error"}
26 changes: 26 additions & 0 deletions show-me-the-money/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
version: '3'
services:
backend:
build:
context: ./backend
dockerfile: Dockerfile.backend
ports:
- "8000:8000"
environment:
- XERO_API_URL=http://xero-mock:3000
depends_on:
- xero-mock

frontend:
build:
context: ./frontend
dockerfile: Dockerfile.frontend
ports:
- "8080:80"
depends_on:
- backend

xero-mock:
image: jaypeng2015/show-me-the-money
ports:
- "3000:3000"
23 changes: 23 additions & 0 deletions show-me-the-money/frontend/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.

# dependencies
/node_modules
/.pnp
.pnp.js

# testing
/coverage

# production
/build

# misc
.DS_Store
.env.local
.env.development.local
.env.test.local
.env.production.local

npm-debug.log*
yarn-debug.log*
yarn-error.log*
15 changes: 15 additions & 0 deletions show-me-the-money/frontend/Dockerfile.frontend
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
FROM node:20 as build

WORKDIR /app

COPY package*.json ./
RUN npm install

COPY . .

RUN npm run build

FROM nginx:latest
COPY --from=build /app/build /usr/share/nginx/html
EXPOSE 80
CMD ["nginx", "-g", "daemon off;"]
46 changes: 46 additions & 0 deletions show-me-the-money/frontend/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# Getting Started with Create React App

This project was bootstrapped with [Create React App](https://github.com/facebook/create-react-app).

## Available Scripts

In the project directory, you can run:

### `npm start`

Runs the app in the development mode.\
Open [http://localhost:3000](http://localhost:3000) to view it in the browser.

The page will reload if you make edits.\
You will also see any lint errors in the console.

### `npm test`

Launches the test runner in the interactive watch mode.\
See the section about [running tests](https://facebook.github.io/create-react-app/docs/running-tests) for more information.

### `npm run build`

Builds the app for production to the `build` folder.\
It correctly bundles React in production mode and optimizes the build for the best performance.

The build is minified and the filenames include the hashes.\
Your app is ready to be deployed!

See the section about [deployment](https://facebook.github.io/create-react-app/docs/deployment) for more information.

### `npm run eject`

**Note: this is a one-way operation. Once you `eject`, you can’t go back!**

If you aren’t satisfied with the build tool and configuration choices, you can `eject` at any time. This command will remove the single build dependency from your project.

Instead, it will copy all the configuration files and the transitive dependencies (webpack, Babel, ESLint, etc) right into your project so you have full control over them. All of the commands except `eject` will still work, but they will point to the copied scripts so you can tweak them. At this point you’re on your own.

You don’t have to ever use `eject`. The curated feature set is suitable for small and middle deployments, and you shouldn’t feel obligated to use this feature. However we understand that this tool wouldn’t be useful if you couldn’t customize it when you are ready for it.

## Learn More

You can learn more in the [Create React App documentation](https://facebook.github.io/create-react-app/docs/getting-started).

To learn React, check out the [React documentation](https://reactjs.org/).
Loading