diff --git a/persistence/session.go b/persistence/session.go new file mode 100644 index 0000000..6d2c020 --- /dev/null +++ b/persistence/session.go @@ -0,0 +1,22 @@ +package persistence + +import ( + "offline_twitter/scraper" +) + +func (p Profile) SaveSession(api scraper.API) { + // TODO session-saving:1 + // - To understand what's going on here, look at the `MarshalJSON` function in `scraper/api_request_utils.go`, + // and the output of `git show 390c83154117aa2a339a83f05820fb904a32298e`. + // - use `json.Marshal` on the API object and write the resulting bytes to like "[api.UserHandle].session" or something + // - use `os.WriteFile` to write the file + panic("TODO") +} + +func (p Profile) LoadSession(userhandle scraper.UserHandle) scraper.API { + // TODO session-saving:2 + // - use `os.ReadFile` to read "[userhandle].session" + // - create a variable of type scraper.API + // - use `json.Unmarshal` to load the file contents into the new API variable + panic("TODO") +} diff --git a/persistence/session_test.go b/persistence/session_test.go new file mode 100644 index 0000000..5b706a2 --- /dev/null +++ b/persistence/session_test.go @@ -0,0 +1,28 @@ +package persistence_test + +import ( + "github.com/go-test/deep" + "offline_twitter/persistence" + "offline_twitter/scraper" + "testing" +) + +// Save and load an API session; it should come back the same +func TestSaveAndLoadAuthenticatedSession(t *testing.T) { + assert := assert.New(t) + profile_path := "test_profiles/TestSession" + profile := create_or_load_profile(profile_path) + + api := scraper.API{ + // TODO session-saving + // - Fill out some fields here like Cookies and CSRFToken and UserHandle + } + + // Save and load the session; it should come back the same + profile.SaveSession(api) + new_api = profile.LoadSession(api.UserHandle) + + if diff := deep.Equal(api, new_api); diff != nil { + t.Errorf(diff) + } +}