Add UserID to the API session

This commit is contained in:
Alessio 2023-06-06 22:48:42 -03:00
parent d06a42e2fc
commit a27f12a915
2 changed files with 7 additions and 0 deletions

View File

@ -18,6 +18,7 @@ const API_USER_TIMELINE_BASE_PATH = "https://api.twitter.com/2/timeline/profile/
type API struct { type API struct {
UserHandle UserHandle UserHandle UserHandle
UserID UserID
IsAuthenticated bool IsAuthenticated bool
GuestToken string GuestToken string
Client http.Client Client http.Client
@ -38,6 +39,7 @@ func InitApi(newApi API) {
type api_outstruct struct { type api_outstruct struct {
Cookies []*http.Cookie Cookies []*http.Cookie
UserID UserID
UserHandle UserHandle UserHandle UserHandle
IsAuthenticated bool IsAuthenticated bool
GuestToken string GuestToken string
@ -49,6 +51,7 @@ var TWITTER_BASE_URL = url.URL{Scheme: "https", Host: "twitter.com"}
func (api API) MarshalJSON() ([]byte, error) { func (api API) MarshalJSON() ([]byte, error) {
result, err := json.Marshal(api_outstruct{ result, err := json.Marshal(api_outstruct{
Cookies: api.Client.Jar.Cookies(&TWITTER_BASE_URL), Cookies: api.Client.Jar.Cookies(&TWITTER_BASE_URL),
UserID: api.UserID,
UserHandle: api.UserHandle, UserHandle: api.UserHandle,
IsAuthenticated: api.IsAuthenticated, IsAuthenticated: api.IsAuthenticated,
GuestToken: api.GuestToken, GuestToken: api.GuestToken,
@ -73,6 +76,7 @@ func (api *API) UnmarshalJSON(data []byte) error {
cookie_jar.SetCookies(&TWITTER_BASE_URL, in_struct.Cookies) cookie_jar.SetCookies(&TWITTER_BASE_URL, in_struct.Cookies)
api.IsAuthenticated = in_struct.IsAuthenticated api.IsAuthenticated = in_struct.IsAuthenticated
api.GuestToken = in_struct.GuestToken api.GuestToken = in_struct.GuestToken
api.UserID = in_struct.UserID
api.UserHandle = in_struct.UserHandle api.UserHandle = in_struct.UserHandle
api.Client = http.Client{ api.Client = http.Client{
@ -170,6 +174,7 @@ func (api *API) LogIn(username string, password string) {
Subtasks []struct { Subtasks []struct {
OpenAccount struct { OpenAccount struct {
User struct { User struct {
ID int
Name string Name string
ScreenName string `json:"screen_name"` ScreenName string `json:"screen_name"`
} }
@ -186,6 +191,7 @@ func (api *API) LogIn(username string, password string) {
panic(err) panic(err)
} }
api.UserID = UserID(final_result.Subtasks[0].OpenAccount.User.ID)
api.UserHandle = UserHandle(final_result.Subtasks[0].OpenAccount.User.ScreenName) api.UserHandle = UserHandle(final_result.Subtasks[0].OpenAccount.User.ScreenName)
api.update_csrf_token() api.update_csrf_token()

View File

@ -27,6 +27,7 @@ func TestJsonifyApi(t *testing.T) {
{Name: "name2", Value: "name2", HttpOnly: true}, {Name: "name2", Value: "name2", HttpOnly: true},
}) })
api := API{ api := API{
UserID: UserID(1423),
UserHandle: UserHandle("userhandle"), UserHandle: UserHandle("userhandle"),
IsAuthenticated: true, IsAuthenticated: true,
GuestToken: "guest token", GuestToken: "guest token",