Switch to gjson

This commit is contained in:
video-prize-ranch 2022-02-02 11:05:36 -05:00
parent 4ea95ff592
commit 86afc22616
No known key found for this signature in database
GPG Key ID: D8EAA4C5B12A7281
2 changed files with 18 additions and 19 deletions

View File

@ -1,7 +1,6 @@
package api package api
import ( import (
"encoding/json"
"io" "io"
"net/http" "net/http"
"strings" "strings"
@ -24,19 +23,19 @@ func FetchUser(username string) (types.User, error) {
return types.User{}, err return types.User{}, err
} }
var user types.User data := gjson.Parse(string(body))
err = json.Unmarshal(body, &user)
if err != nil {
return types.User{}, err
}
user.Cover = strings.ReplaceAll(user.Cover, "https://imgur.com", "") createdTime, _ := time.Parse(time.RFC3339, data.Get("created_at").String())
user.Avatar = strings.ReplaceAll(user.Avatar, "https://i.imgur.com", "")
createdTime, _ := time.Parse("2006-01-02T15:04:05Z", user.CreatedAt) return types.User{
user.CreatedAt = createdTime.Format("January 2, 2006") Id: data.Get("id").Int(),
Bio: data.Get("bio").String(),
return user, nil 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) { func FetchSubmissions(username string, sort string, page string) ([]types.Submission, error) {

View File

@ -1,13 +1,13 @@
package types package types
type User struct { type User struct {
Id int64 `json:"id"` Id int64
Bio string `json:"bio"` Bio string
Username string `json:"username"` Username string
Points int64 `json:"reputation_count"` Points int64
Cover string `json:"cover_url"` Cover string
Avatar string `json:"avatar_url"` Avatar string
CreatedAt string `json:"created_at"` CreatedAt string
} }
type Submission struct { type Submission struct {