Skip to content

Commit

Permalink
v1.2.0
Browse files Browse the repository at this point in the history
- Integration tests for the default endpoints.yaml file
- Remove poorly supported port flag, which caused docker deployments to fail. To be re-added later!
- Added status check endpoint
- Small documentation updates
  • Loading branch information
umutseven92 committed Mar 6, 2021
1 parent 30110d5 commit 5b73482
Show file tree
Hide file tree
Showing 15 changed files with 621 additions and 29 deletions.
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@ poetry install # Install dependencies
poetry run apyr # Run apyr
```

Port can be set by `-p PORT`. The default port is `8000`.

### Via Docker

```bash
Expand Down Expand Up @@ -133,5 +131,8 @@ Currently supported functions are:
| :--- | :--- | :--- | :--- |
| `%random_first_name(gender)%` | `gender`: Optional string. Can be `male` or `female`. If left empty, will default to both | Will be replaced by a random first name | `%random_first_name(male)%`, `%random_first_name(female)%`, `%random_first_name()%`
| `%random_last_name()%` | | Will be replaced by a random last name | `%random_last_name()%` |
| `%random_int(start, end)%` | `start`: Required int, `end`: Required int | Will be replaced by a random integer between `start` and `end` | `%random_int(0, 20)%`, `%random_int(20, 50)%` |
| `%random_int(start, end)%` | `start`: Required int, `end`: Required int | Will be replaced by a random integer between `start` and `end` (both inclusive) | `%random_int(0, 20)%`, `%random_int(20, 50)%` |

## Contributing

If you like this project, please consider [donating to the Electronic Frontier Foundation](https://supporters.eff.org/donate).
8 changes: 3 additions & 5 deletions apyr/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,17 @@
import uvicorn
from fastapi import FastAPI

from apyr.routers.apyr_endpoints import apyr_router
from apyr.routers.endpoints import endpoint_router

parser = argparse.ArgumentParser()
parser.add_argument("-p", "--port", help="Port", default=8000, type=int)
args = parser.parse_args()

app = FastAPI()

app.include_router(apyr_router)
app.include_router(endpoint_router)


def run():
uvicorn.run(app, host="0.0.0.0", port=args.port)
uvicorn.run(app, host="0.0.0.0", port=8000)


if __name__ == "__main__":
Expand Down
10 changes: 6 additions & 4 deletions apyr/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,15 @@ class Endpoint(BaseModel):
content_path: Optional[str]

@validator("content_path", pre=True, always=True)
def content_path_correct(cls, value, values): # pylint:disable=no-self-argument, no-self-use
def content_path_correct(
cls, value, values
): # pylint:disable=no-self-argument, no-self-use
if values["content"] is not None and value is not None:
raise ValueError("Cannot set both content and content_path.")
return value


class ContentFunction(BaseModel):
full: str
name: str
params: List[str]
full: str # E.g. %random_first_name(female)%
name: str # E.g. random_first_name
params: List[str] # E.g. female
16 changes: 16 additions & 0 deletions apyr/routers/apyr_endpoints.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
""" Endpoints used by apyr itself.
Prefixed by `/apyr` so as to not block user defined endpoints from being mocked.
"""
from fastapi import APIRouter

from starlette.responses import Response
from starlette.status import HTTP_200_OK

apyr_router = APIRouter(prefix="/apyr")


@apyr_router.get("/status")
async def status():
"""Status check for the apyr service. Used mainly as a health check."""
return Response(status_code=HTTP_200_OK)
4 changes: 3 additions & 1 deletion apyr/routers/endpoints.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
""" Endpoints that are defined by user. """

from functools import lru_cache

from fastapi import APIRouter, Depends, Request, HTTPException
Expand All @@ -8,7 +10,7 @@
endpoint_router = APIRouter()


@lru_cache
@lru_cache()
def endpoints_dependency() -> EndpointsRepo:
return EndpointsRepo()

Expand Down
18 changes: 9 additions & 9 deletions endpoints.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
[
{ "first_name": "Peter", "last_name": "Venkman" },
{ "first_name": "Ray", "last_name": "Stantz" },
{ "first_name": "Egon", "last_name": "Spengler" },
{ "first_name": "Egon", "last_name": "Spengler" }
]
# A GET method that returns an employee.
# Take note of the two %functions%- the employee's first name, last name and age will be random at every response.
Expand All @@ -24,7 +24,7 @@
path: test/employee
media_type: text
status_code: 500
content: An unexpected error occured while creating the employee.
content: An unexpected error occurred while creating the employee.
# A PUT method that returns a 201. Does not return a body- content is optional.
- method: PUT
path: test/employee/3
Expand All @@ -34,14 +34,14 @@
path: test/help
status_code: 200
media_type: text/html
content: >
content: |-
<!DOCTYPE html>
<html>
<body>
<h1>I've quit better jobs than this.</h1>
<p>Ghostbusters, whaddya want.</p>
</body>
</html>
<html>
<body>
<h1>I've quit better jobs than this.</h1>
<p>Ghostbusters, whaddya want.</p>
</body>
</html>
# The same method as above, but the content is referenced from another file. Path is relative to project root.
- method: GET
path: test/help2
Expand Down
Loading

0 comments on commit 5b73482

Please sign in to comment.