]> git.lizzy.rs Git - go-anidb.git/blobdiff - anidb.go
anidb: Try to get (*AniDB).User() from the cache if it's unset
[go-anidb.git] / anidb.go
index 1b69e714255c8ac729a9b42d044d9282a6a1fba1..bb0d65846ba51572601b8c0959afa3bab7a37179 100644 (file)
--- a/anidb.go
+++ b/anidb.go
@@ -6,6 +6,17 @@ import (
 )
 
 // Main struct for the client, contains all non-shared state.
+//
+// All ObjectByKey methods (AnimeByID, GroupByName, etc) first try to read
+// from the cache. If the sought object isn't cached, or if the cache is
+// stale, then the appropriate API is queried.
+//
+// Queries return their results using channels. Most queries only have one result,
+// but some have 0 or more. All result channels are closed after sending their data.
+//
+// Queries that depend on the UDP API can't be used without first authenticating
+// to the API server. This uses the credentials stored by SetCredentials, or
+// by a previous Auth() call.
 type AniDB struct {
        Timeout time.Duration // Timeout for the various calls (default: 45s)
 
@@ -19,3 +30,16 @@ func NewAniDB() *AniDB {
                udp:     newUDPWrap(),
        }
 }
+
+func (adb *AniDB) User() *User {
+       if adb != nil && adb.udp != nil {
+               if adb.udp.user != nil {
+                       return adb.udp.user
+               } else if adb.udp.credentials != nil {
+                       // see if we can get it from the cache
+                       adb.udp.user = UserByName(decrypt(adb.udp.credentials.username))
+                       return adb.udp.user
+               }
+       }
+       return nil
+}