]> git.lizzy.rs Git - go-anidb.git/blob - anidb.go
anidb: Use a map instead of various fields that have to be switched
[go-anidb.git] / anidb.go
1 // Attempt at high level client library for AniDB's APIs
2 package anidb
3
4 import (
5         "time"
6 )
7
8 // Main struct for the client, contains all non-shared state.
9 //
10 // All ObjectByKey methods (AnimeByID, GroupByName, etc) first try to read
11 // from the cache. If the sought object isn't cached, or if the cache is
12 // stale, then the appropriate API is queried.
13 //
14 // Queries return their results using channels. Most queries only have one result,
15 // but some have 0 or more. All result channels are closed after sending their data.
16 //
17 // Queries that depend on the UDP API can't be used without first authenticating
18 // to the API server. This uses the credentials stored by SetCredentials, or
19 // by a previous Auth() call.
20 type AniDB struct {
21         Timeout time.Duration // Timeout for the various calls (default: 45s)
22
23         udp *udpWrap
24 }
25
26 // Initialises a new AniDB.
27 func NewAniDB() *AniDB {
28         return &AniDB{
29                 Timeout: 45 * time.Second,
30                 udp:     newUDPWrap(),
31         }
32 }
33
34 func (adb *AniDB) User() *User {
35         if adb != nil && adb.udp != nil {
36                 if adb.udp.user != nil {
37                         return adb.udp.user
38                 } else if adb.udp.credentials != nil {
39                         // see if we can get it from the cache
40                         adb.udp.user = UserByName(decrypt(adb.udp.credentials.username))
41                         return adb.udp.user
42                 }
43         }
44         return nil
45 }