Switch to gjson
This commit is contained in:
parent
4ea95ff592
commit
86afc22616
23
api/user.go
23
api/user.go
|
@ -1,7 +1,6 @@
|
|||
package api
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"io"
|
||||
"net/http"
|
||||
"strings"
|
||||
|
@ -24,19 +23,19 @@ func FetchUser(username string) (types.User, error) {
|
|||
return types.User{}, err
|
||||
}
|
||||
|
||||
var user types.User
|
||||
err = json.Unmarshal(body, &user)
|
||||
if err != nil {
|
||||
return types.User{}, err
|
||||
}
|
||||
data := gjson.Parse(string(body))
|
||||
|
||||
user.Cover = strings.ReplaceAll(user.Cover, "https://imgur.com", "")
|
||||
user.Avatar = strings.ReplaceAll(user.Avatar, "https://i.imgur.com", "")
|
||||
createdTime, _ := time.Parse(time.RFC3339, data.Get("created_at").String())
|
||||
|
||||
createdTime, _ := time.Parse("2006-01-02T15:04:05Z", user.CreatedAt)
|
||||
user.CreatedAt = createdTime.Format("January 2, 2006")
|
||||
|
||||
return user, nil
|
||||
return types.User{
|
||||
Id: data.Get("id").Int(),
|
||||
Bio: data.Get("bio").String(),
|
||||
Username: data.Get("username").String(),
|
||||
Points: data.Get("reputation_count").Int(),
|
||||
Cover: strings.ReplaceAll(data.Get("cover_url").String(), "https://imgur.com", ""),
|
||||
Avatar: strings.ReplaceAll(data.Get("avatar_url").String(), "https://i.imgur.com", ""),
|
||||
CreatedAt: createdTime.Format("January 2, 2006"),
|
||||
}, nil
|
||||
}
|
||||
|
||||
func FetchSubmissions(username string, sort string, page string) ([]types.Submission, error) {
|
||||
|
|
|
@ -1,13 +1,13 @@
|
|||
package types
|
||||
|
||||
type User struct {
|
||||
Id int64 `json:"id"`
|
||||
Bio string `json:"bio"`
|
||||
Username string `json:"username"`
|
||||
Points int64 `json:"reputation_count"`
|
||||
Cover string `json:"cover_url"`
|
||||
Avatar string `json:"avatar_url"`
|
||||
CreatedAt string `json:"created_at"`
|
||||
Id int64
|
||||
Bio string
|
||||
Username string
|
||||
Points int64
|
||||
Cover string
|
||||
Avatar string
|
||||
CreatedAt string
|
||||
}
|
||||
|
||||
type Submission struct {
|
||||
|
|
Loading…
Reference in New Issue