kossiitkgp / KWoC-Backend

Backend API for Kharagpur Winter of Code
https://api.kwoc.kossiitkgp.org
GNU General Public License v3.0
23 stars 21 forks source link

[v2] Standard JSON Response Format #129

Closed harshkhandeparkar closed 1 year ago

harshkhandeparkar commented 1 year ago

Currently, the format of the responses returned by endpoints is not standard. In some cases, a plain text response is returned, whereas in some cases, it is JSON. Also, the text in the response is different. (For example, there is no standard format for error responses)

A standard JSON format would be better for responses of every endpoint. Please share any ideas below.

harshkhandeparkar commented 1 year ago

@Alph3ga @YoganshSharma do you have any suggestions for this?

YoganshSharma commented 1 year ago

How about adding a response struct similar to this

type HTTPError struct {
    Code    int    `json:"code" example:"400"`
    Message string `json:"message" example:"status bad request"`
}

And refactoring utils.LogWarnAndRespond and utils.LogErrAndRespond to use this instead. Some places where these util functions should be used, but they aren't. That also needs to be fixed.

harshkhandeparkar commented 1 year ago

How about adding a response struct similar to this

type HTTPError struct {
  Code    int    `json:"code" example:"400"`
  Message string `json:"message" example:"status bad request"`
}

Works.

And refactoring utils.LogWarnAndRespond and utils.LogErrAndRespond to use this instead. Some places where these util functions should be used, but they aren't. That also needs to be fixed.

Which places? (apart from #130 ?)

YoganshSharma commented 1 year ago
YoganshSharma commented 1 year ago

ig we should generalize HTTPError to a more general name like HTTPMessage and use it wherever we are return status code 200 and a message like https://github.com/kossiitkgp/KWoC-Backend/blob/00662b266ce2b7cd487a1880efd9fb0dc7c206ee/controllers/mentor.go#L87-L88

harshkhandeparkar commented 1 year ago

Interesting

harshkhandeparkar commented 1 year ago

do you want to work on this? self assign if yes. also ig doing this before #150 would be better