51 lines
1.4 KiB
Go
51 lines
1.4 KiB
Go
package middleware
|
|
|
|
import (
|
|
"github.com/Neur0toxine/wa-profile-api/internal/cache"
|
|
"github.com/Neur0toxine/wa-profile-api/internal/dto"
|
|
"github.com/Neur0toxine/wa-profile-api/internal/log"
|
|
"github.com/Neur0toxine/wa-profile-api/internal/waapi"
|
|
"github.com/kataras/iris/v12"
|
|
)
|
|
|
|
// GetProfileHandler godoc
|
|
//
|
|
// @Summary Get WhatsApp profile info via phone number.
|
|
// @Description Returns profile info.
|
|
// @Tags profile
|
|
// @Produce json
|
|
// @Param phone query string true "Phone number in E.134 format."
|
|
// @Success 200 {object} dto.WAProfile
|
|
// @Failure 400 {object} dto.APIError
|
|
// @Failure 404 {object} dto.APIError
|
|
// @Failure 422 {object} dto.APIError
|
|
// @Router /profile [get]
|
|
func GetProfileHandler(c iris.Context) {
|
|
var req dto.WAProfileRequest
|
|
if err := c.ReadQuery(&req); err != nil {
|
|
c.StopWithJSON(iris.StatusBadRequest, dto.APIError{Error: err.Error()})
|
|
return
|
|
}
|
|
|
|
if req.Phone[0] == '+' {
|
|
req.Phone = req.Phone[1:]
|
|
}
|
|
|
|
log.Debug("resolving", req.Phone)
|
|
if profile, ok := cache.Profiles.Get(req.Phone); ok {
|
|
log.Debug("cache hit for", req.Phone)
|
|
c.JSON(profile)
|
|
return
|
|
}
|
|
|
|
client := c.Value(waapi.ClientKey).(*waapi.Client)
|
|
profile, err := client.ProfileByPhone(req.Phone)
|
|
if err != nil {
|
|
c.StopWithJSON(iris.StatusBadRequest, dto.APIError{Error: err.Error()})
|
|
return
|
|
}
|
|
|
|
cache.Profiles.Set(req.Phone, profile)
|
|
c.JSON(profile)
|
|
}
|