-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathclient.go
44 lines (36 loc) · 896 Bytes
/
client.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
package radigo
import (
"context"
"fmt"
"os"
"github.com/yyoshiki41/go-radiko"
)
const (
envRadikoMail = "RADIKO_MAIL"
envRadikoPassword = "RADIKO_PASSWORD"
)
func getClient(ctx context.Context, areaID string) (*radiko.Client, error) {
var client *radiko.Client
var err error
client, err = radiko.New("")
if err != nil {
return nil, err
}
// When a currentAreaID is not equal to the given areaID,
// it is necessary to use the area free as the premium member.
if areaID != "" && areaID != currentAreaID {
mail := os.Getenv(envRadikoMail)
password := os.Getenv(envRadikoPassword)
login, err := client.Login(ctx, mail, password)
switch {
case err != nil:
return nil, err
case login.StatusCode() != 200:
return nil, fmt.Errorf(
"invalid login status code: %d", login.StatusCode())
default:
client.SetAreaID(areaID)
}
}
return client, nil
}