more parsing

This commit is contained in:
Andre Medeiros 2021-07-14 20:52:51 -04:00
parent 34b9159f46
commit ad929b98d9
5 changed files with 921 additions and 64 deletions

View file

@ -155,7 +155,7 @@ func (rac *AuthenticatedClient) RefreshTokens() (*RefreshTokenResponse, error) {
return rtr, nil return rtr, nil
} }
func (rac *AuthenticatedClient) MessageInbox(from string) (*MessageListingResponse, error) { func (rac *AuthenticatedClient) MessageInbox(from string) (*ListingResponse, error) {
req := NewRequest( req := NewRequest(
WithTags([]string{"url:/api/v1/message/inbox"}), WithTags([]string{"url:/api/v1/message/inbox"}),
WithMethod("GET"), WithMethod("GET"),
@ -170,9 +170,12 @@ func (rac *AuthenticatedClient) MessageInbox(from string) (*MessageListingRespon
return nil, err return nil, err
} }
mlr := &MessageListingResponse{} val, err := rac.parser.ParseBytes(body)
json.Unmarshal([]byte(body), mlr) if err != nil {
return mlr, nil return nil, err
}
return NewListingResponse(val), nil
} }
func (rac *AuthenticatedClient) MessageUnread(from string) (*MessageListingResponse, error) { func (rac *AuthenticatedClient) MessageUnread(from string) (*MessageListingResponse, error) {

View file

@ -1,8 +1,8 @@
{ {
"is_employee": false, "is_employee": false,
"seen_layout_switch": true, "seen_layout_switch": true,
"has_visited_new_profile": true, "has_visited_new_profile": false,
"pref_no_profanity": false, "pref_no_profanity": true,
"has_external_account": false, "has_external_account": false,
"pref_geopopular": "GLOBAL", "pref_geopopular": "GLOBAL",
"seen_redesign_modal": true, "seen_redesign_modal": true,
@ -16,9 +16,9 @@
"free_form_reports": true, "free_form_reports": true,
"community_icon": null, "community_icon": null,
"show_media": true, "show_media": true,
"icon_color": "#EA0027", "icon_color": "#545452",
"user_is_muted": false, "user_is_muted": false,
"display_name": "u_changelog", "display_name": "u_hugocat",
"header_img": null, "header_img": null,
"title": "", "title": "",
"coins": 0, "coins": 0,
@ -29,20 +29,20 @@
256 256
], ],
"primary_color": "", "primary_color": "",
"icon_img": "https://www.redditstatic.com/avatars/avatar_default_19_EA0027.png", "icon_img": "https://www.redditstatic.com/avatars/avatar_default_07_545452.png",
"description": "", "description": "",
"submit_link_label": "", "submit_link_label": "",
"header_size": null, "header_size": null,
"restrict_commenting": false, "restrict_commenting": false,
"subscribers": 3, "subscribers": 0,
"submit_text_label": "", "submit_text_label": "",
"is_default_icon": true, "is_default_icon": true,
"link_flair_position": "", "link_flair_position": "",
"display_name_prefixed": "u/changelog", "display_name_prefixed": "u/hugocat",
"key_color": "", "key_color": "",
"name": "t5_c30sw", "name": "t5_1e6nc8",
"is_default_banner": true, "is_default_banner": true,
"url": "/user/changelog/", "url": "/user/hugocat/",
"quarantine": false, "quarantine": false,
"banner_size": null, "banner_size": null,
"user_is_moderator": true, "user_is_moderator": true,
@ -52,35 +52,34 @@
"subreddit_type": "user", "subreddit_type": "user",
"user_is_subscriber": false "user_is_subscriber": false
}, },
"pref_show_presence": false, "pref_show_presence": true,
"snoovatar_img": "", "snoovatar_img": "",
"snoovatar_size": null, "snoovatar_size": null,
"gold_expiration": null, "gold_expiration": null,
"has_gold_subscription": false, "has_gold_subscription": false,
"is_sponsor": false, "is_sponsor": false,
"num_friends": 7, "num_friends": 1,
"features": { "features": {
"mod_service_mute_writes": true, "mod_service_mute_writes": true,
"promoted_trend_blanks": true, "promoted_trend_blanks": true,
"show_amp_link": true, "show_amp_link": true,
"chat": true, "top_content_email_digest_v2": {
"mweb_link_tab": {
"owner": "growth", "owner": "growth",
"variant": "control_1", "variant": "control_1",
"experiment_id": 404 "experiment_id": 363
}, },
"chat": true,
"is_email_permission_required": true, "is_email_permission_required": true,
"mod_awards": true, "mod_awards": true,
"mweb_xpromo_revamp_v3": { "expensive_coins_package": true,
"mweb_xpromo_revamp_v2": {
"owner": "growth", "owner": "growth",
"variant": "treatment_4", "variant": "treatment_1",
"experiment_id": 480 "experiment_id": 457
}, },
"chat_subreddit": true,
"awards_on_streams": true, "awards_on_streams": true,
"webhook_config": true,
"mweb_xpromo_modal_listing_click_daily_dismissible_ios": true, "mweb_xpromo_modal_listing_click_daily_dismissible_ios": true,
"live_orangereds": true, "chat_subreddit": true,
"cookie_consent_banner": true, "cookie_consent_banner": true,
"modlog_copyright_removal": true, "modlog_copyright_removal": true,
"do_not_track": true, "do_not_track": true,
@ -91,75 +90,58 @@
"noreferrer_to_noopener": true, "noreferrer_to_noopener": true,
"premium_subscriptions_table": true, "premium_subscriptions_table": true,
"mweb_xpromo_interstitial_comments_android": true, "mweb_xpromo_interstitial_comments_android": true,
"mweb_nsfw_xpromo": {
"owner": "growth",
"variant": "control_2",
"experiment_id": 361
},
"mweb_xpromo_modal_listing_click_daily_dismissible_android": true,
"mweb_sharing_web_share_api": {
"owner": "growth",
"variant": "control_1",
"experiment_id": 314
},
"chat_group_rollout": true, "chat_group_rollout": true,
"resized_styles_images": true, "resized_styles_images": true,
"spez_modal": true, "spez_modal": true,
"mweb_sharing_clipboard": { "mweb_xpromo_modal_listing_click_daily_dismissible_android": true
"owner": "growth",
"variant": "control_2",
"experiment_id": 315
},
"expensive_coins_package": true
}, },
"can_edit_name": false, "can_edit_name": false,
"verified": true, "verified": true,
"new_modmail_exists": null, "new_modmail_exists": true,
"pref_autoplay": false, "pref_autoplay": true,
"coins": 0, "coins": 100,
"has_paypal_subscription": false, "has_paypal_subscription": false,
"has_subscribed_to_premium": false, "has_subscribed_to_premium": false,
"id": "1ia22", "id": "xgeee",
"has_stripe_subscription": false, "has_stripe_subscription": false,
"oauth_client_id": "5JHxEu-4wnFfBA", "oauth_client_id": "5JHxEu-4wnFfBA",
"can_create_subreddit": true, "can_create_subreddit": true,
"over_18": true, "over_18": true,
"is_gold": false, "is_gold": false,
"is_mod": false, "is_mod": true,
"awarder_karma": 0, "awarder_karma": 0,
"suspension_expiration_utc": null, "suspension_expiration_utc": null,
"has_verified_email": true, "has_verified_email": true,
"is_suspended": false, "is_suspended": false,
"pref_video_autoplay": false, "pref_video_autoplay": true,
"in_chat": true, "in_chat": true,
"has_android_subscription": false, "has_android_subscription": false,
"in_redesign_beta": true, "in_redesign_beta": true,
"icon_img": "https://www.redditstatic.com/avatars/avatar_default_19_EA0027.png", "icon_img": "https://www.redditstatic.com/avatars/avatar_default_07_545452.png",
"has_mod_mail": false, "has_mod_mail": true,
"pref_nightmode": true, "pref_nightmode": false,
"awardee_karma": 10, "awardee_karma": 55,
"hide_from_robots": true, "hide_from_robots": false,
"password_set": true, "password_set": true,
"link_karma": 2058, "link_karma": 166,
"force_password_reset": false, "force_password_reset": false,
"total_karma": 3445, "total_karma": 324,
"seen_give_award_tooltip": false, "seen_give_award_tooltip": false,
"inbox_count": 0, "inbox_count": 0,
"seen_premium_adblock_modal": false, "seen_premium_adblock_modal": false,
"pref_top_karma_subreddits": false, "pref_top_karma_subreddits": true,
"has_mail": false, "has_mail": false,
"pref_show_snoovatar": false, "pref_show_snoovatar": false,
"name": "changelog", "name": "hugocat",
"pref_clickgadget": 5, "pref_clickgadget": 5,
"created": 1176750666.0, "created": 1461652799.0,
"gold_creddits": 0, "gold_creddits": 0,
"created_utc": 1176721866.0, "created_utc": 1461623999.0,
"has_ios_subscription": false, "has_ios_subscription": false,
"pref_show_twitter": false, "pref_show_twitter": false,
"in_beta": true, "in_beta": false,
"comment_karma": 1377, "comment_karma": 103,
"has_subscribed": true, "has_subscribed": true,
"linked_identities": [], "linked_identities": [],
"seen_subreddit_chat_ftux": false "seen_subreddit_chat_ftux": true
} }

File diff suppressed because one or more lines are too long

View file

@ -70,3 +70,65 @@ func NewMeResponse(val *fastjson.Value) *MeResponse {
return mr return mr
} }
type Thing struct {
Kind string `json:"kind"`
ID string `json:"id"`
Type string `json:"type"`
Author string `json:"author"`
Subject string `json:"subject"`
Body string `json:"body"`
CreatedAt float64 `json:"created_utc"`
Context string `json:"context"`
ParentID string `json:"parent_id"`
LinkTitle string `json:"link_title"`
Destination string `json:"dest"`
Subreddit string `json:"subreddit"`
}
func NewThing(val *fastjson.Value) *Thing {
t := &Thing{}
t.Kind = string(val.GetStringBytes("kind"))
data := val.Get("data")
t.ID = string(data.GetStringBytes("id"))
t.Type = string(data.GetStringBytes("type"))
t.Author = string(data.GetStringBytes("author"))
t.Subject = string(data.GetStringBytes("subject"))
t.Body = string(data.GetStringBytes("body"))
t.CreatedAt = data.GetFloat64("created_utc")
t.Context = string(data.GetStringBytes("context"))
t.ParentID = string(data.GetStringBytes("parent_id"))
t.LinkTitle = string(data.GetStringBytes("link_title"))
t.Destination = string(data.GetStringBytes("dest"))
t.Subreddit = string(data.GetStringBytes("subreddit"))
return t
}
type ListingResponse struct {
Count int
Children []*Thing
After string
Before string
}
func NewListingResponse(val *fastjson.Value) *ListingResponse {
lr := &ListingResponse{}
data := val.Get("data")
lr.After = string(data.GetStringBytes("after"))
lr.Before = string(data.GetStringBytes("before"))
lr.Count = data.GetInt("dist")
lr.Children = make([]*Thing, lr.Count)
children := data.GetArray("children")
for i := 0; i < lr.Count; i++ {
t := NewThing(children[i])
lr.Children[i] = t
}
return lr
}

View file

@ -22,6 +22,38 @@ func TestMeResponseParsing(t *testing.T) {
me := NewMeResponse(val) me := NewMeResponse(val)
assert.NotNil(t, me) assert.NotNil(t, me)
assert.Equal(t, "1ia22", me.ID) assert.Equal(t, "xgeee", me.ID)
assert.Equal(t, "changelog", me.Name) assert.Equal(t, "hugocat", me.Name)
}
func TestListingResponseParsing(t *testing.T) {
bb, err := ioutil.ReadFile("testdata/message_inbox.json")
assert.NoError(t, err)
val, err := parser.ParseBytes(bb)
assert.NoError(t, err)
l := NewListingResponse(val)
assert.NotNil(t, l)
assert.Equal(t, 25, l.Count)
assert.Equal(t, 25, len(l.Children))
assert.Equal(t, "t1_h470gjv", l.After)
assert.Equal(t, "", l.Before)
thing := l.Children[0]
assert.Equal(t, "t4", thing.Kind)
assert.Equal(t, "138z6ke", thing.ID)
assert.Equal(t, "unknown", thing.Type)
assert.Equal(t, "iamthatis", thing.Author)
assert.Equal(t, "how goes it", thing.Subject)
assert.Equal(t, "how are you today", thing.Body)
assert.Equal(t, 1626285395.0, thing.CreatedAt)
assert.Equal(t, "hugocat", thing.Destination)
thing = l.Children[6]
assert.Equal(t, "/r/calicosummer/comments/ngcapc/hello_i_am_a_cat/h4q5j98/?context=3", thing.Context)
assert.Equal(t, "t1_h46tec3", thing.ParentID)
assert.Equal(t, "hello i am a cat", thing.LinkTitle)
assert.Equal(t, "calicosummer", thing.Subreddit)
} }