How to return a result from a fastapi endpoint processing an array of int32 numbers? [closed]

I have to do the following assignment:

Please create a public http or https service. It must have a publicendpoint that accepts array of int32 numbers encoded as json in the request body and returns result as a number. Once deployed, pass the url of that endpoint to this service root in the request body as utf8 encoded string.
If successful you will get a success code to give to us via upwork chat.

I have made a service (currently still local) using FastAPI:

from fastapi import FastAPI, HTTPException
from pydantic import BaseModel

app = FastAPI()

class ListRequest(BaseModel):
    numbers: list[int]

@app.post("/")
async def root(request: ListRequest):
    # int32 range
    int32_min = -2**31
    int32_max = 2**31 - 1

    for number in request.numbers:
        if not (int32_min <= number <= int32_max):
            raise HTTPException(status_code=400, detail="All elements must be int32 numbers")
    
    return {"result": "something???"}

I don’t understand this part: return result as a number?

Can someone help me what exactly do I need to return?

Thank you.

4

I Would still recommend asking more question when working on task such as this one…. it is an unclear task and if this is something that would go to production it might lead to multiple problems so don’t be shy of asking questions about unclear things…. Also I’ve gone with the sum result because it’s the most “human” go to thing when asked for a “result”.

I am assuming that you cant ask question this is why: I’m assuming what they want from you is to get the sum of the result from the request.number and after that you just need to pass the URL where the numbers will be add together or where the function will return an HTTPException:

from fastapi import FastAPI, HTTPException
from pydantic import BaseModel
from typing import List

app = FastAPI()

class ListRequest(BaseModel):
     numbers: List[int]


@app.post("/")

async def root(request: ListRequest):
    # int32 range

    int32_min = -2**31

    int32_max = 2**31 - 1

    for number in request.numbers:
         if not (int32_min <= number <= int32_max):
             raise HTTPException(status_code=400, detail="All 
            elements must be int32 numbers")


    result = sum(request.numbers)

    return {"result": result}

10

I think the task here is to create the interface of such endpoint (schema).

So, to complete this task you need to specify response model and return correct value.

Since it’s not said how you should coerce array to one number, you can use sum or just return 42.

Pay attention, that it’s said that you should return result as a number, not as JSON with number.

Also, you can make an improvement of your code by specifying list item’s constraints in your model:

from typing import Annotated, TypeAlias
from annotated_types import Gt, Lt

MyInt: TypeAlias = Annotated[int, Gt(-2**31 - 1), Lt(2**31)]

class InputData(BaseModel):
    numbers: list[MyInt]

In this case it will be shown in openapi schema. And you don’t need to check items in your endpoint (FastAPI will do it for you)

7

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật