1
0
mirror of https://github.com/hrfee/jfa-go.git synced 2024-11-05 01:40:10 +00:00
jfa-go/mediabrowser/jfapi.go

163 lines
4.3 KiB
Go
Raw Normal View History

package mediabrowser
2020-07-29 21:11:28 +00:00
import (
"encoding/json"
"fmt"
"net/http"
"time"
)
func jfDeleteUser(jf *MediaBrowser, userID string) (int, error) {
url := fmt.Sprintf("%s/Users/%s", jf.Server, userID)
req, _ := http.NewRequest("DELETE", url, nil)
for name, value := range jf.header {
req.Header.Add(name, value)
}
resp, err := jf.httpClient.Do(req)
defer jf.timeoutHandler()
return resp.StatusCode, err
}
func jfGetUsers(jf *MediaBrowser, public bool) ([]User, int, error) {
var result []User
var data string
2020-07-29 21:11:28 +00:00
var status int
var err error
if time.Now().After(jf.CacheExpiry) {
2020-08-16 15:08:37 +00:00
if public {
url := fmt.Sprintf("%s/users/public", jf.Server)
data, status, err = jf.get(url, nil)
2020-08-16 15:08:37 +00:00
} else {
url := fmt.Sprintf("%s/users", jf.Server)
data, status, err = jf.get(url, jf.loginParams)
2020-08-16 15:08:37 +00:00
}
if err != nil || status != 200 {
return nil, status, err
}
err := json.Unmarshal([]byte(data), &result)
if err != nil {
fmt.Println(err)
return nil, status, err
}
2020-08-16 15:08:37 +00:00
jf.userCache = result
jf.CacheExpiry = time.Now().Add(time.Minute * time.Duration(jf.cacheLength))
if result[0].ID[8] == '-' {
jf.Hyphens = true
}
2020-08-16 15:08:37 +00:00
return result, status, nil
2020-07-29 21:11:28 +00:00
}
return jf.userCache, 200, nil
2020-07-29 21:11:28 +00:00
}
func jfUserByName(jf *MediaBrowser, username string, public bool) (User, int, error) {
var match User
find := func() (User, int, error) {
users, status, err := jf.GetUsers(public)
if err != nil || status != 200 {
return User{}, status, err
}
for _, user := range users {
if user.Name == username {
return user, status, err
}
}
return User{}, status, err
2020-07-29 21:11:28 +00:00
}
match, status, err := find()
if match.Name == "" {
jf.CacheExpiry = time.Now()
match, status, err = find()
2020-07-29 21:11:28 +00:00
}
return match, status, err
2020-07-29 21:11:28 +00:00
}
func jfUserByID(jf *MediaBrowser, userID string, public bool) (User, int, error) {
if jf.CacheExpiry.After(time.Now()) {
2020-08-16 15:08:37 +00:00
for _, user := range jf.userCache {
if user.ID == userID {
2020-08-16 15:08:37 +00:00
return user, 200, nil
}
}
}
2020-07-29 21:11:28 +00:00
if public {
users, status, err := jf.GetUsers(public)
2020-07-29 21:11:28 +00:00
if err != nil || status != 200 {
return User{}, status, err
2020-07-29 21:11:28 +00:00
}
for _, user := range users {
if user.ID == userID {
2020-07-29 21:11:28 +00:00
return user, status, nil
}
}
return User{}, status, err
2020-07-29 21:11:28 +00:00
}
var result User
var data string
var status int
var err error
url := fmt.Sprintf("%s/users/%s", jf.Server, userID)
data, status, err = jf.get(url, jf.loginParams)
if err != nil || status != 200 {
return User{}, status, err
}
json.Unmarshal([]byte(data), &result)
return result, status, nil
2020-07-29 21:11:28 +00:00
}
func jfNewUser(jf *MediaBrowser, username, password string) (User, int, error) {
url := fmt.Sprintf("%s/Users/New", jf.Server)
2020-07-29 21:11:28 +00:00
stringData := map[string]string{
"Name": username,
"Password": password,
}
data := make(map[string]interface{})
for key, value := range stringData {
data[key] = value
}
response, status, err := jf.post(url, data, true)
var recv User
json.Unmarshal([]byte(response), &recv)
2020-07-29 21:11:28 +00:00
if err != nil || !(status == 200 || status == 204) {
return User{}, status, err
2020-07-29 21:11:28 +00:00
}
return recv, status, nil
}
func jfSetPolicy(jf *MediaBrowser, userID string, policy Policy) (int, error) {
url := fmt.Sprintf("%s/Users/%s/Policy", jf.Server, userID)
_, status, err := jf.post(url, policy, false)
2020-07-29 21:11:28 +00:00
if err != nil || status != 200 {
return status, err
}
return status, nil
}
func jfSetConfiguration(jf *MediaBrowser, userID string, configuration Configuration) (int, error) {
url := fmt.Sprintf("%s/Users/%s/Configuration", jf.Server, userID)
_, status, err := jf.post(url, configuration, false)
2020-07-29 21:11:28 +00:00
return status, err
}
func jfGetDisplayPreferences(jf *MediaBrowser, userID string) (map[string]interface{}, int, error) {
url := fmt.Sprintf("%s/DisplayPreferences/usersettings?userId=%s&client=emby", jf.Server, userID)
data, status, err := jf.get(url, nil)
2020-07-29 21:11:28 +00:00
if err != nil || !(status == 204 || status == 200) {
return nil, status, err
}
var displayprefs map[string]interface{}
err = json.Unmarshal([]byte(data), &displayprefs)
2020-07-29 21:11:28 +00:00
if err != nil {
return nil, status, err
}
return displayprefs, status, nil
}
func jfSetDisplayPreferences(jf *MediaBrowser, userID string, displayprefs map[string]interface{}) (int, error) {
url := fmt.Sprintf("%s/DisplayPreferences/usersettings?userId=%s&client=emby", jf.Server, userID)
_, status, err := jf.post(url, displayprefs, false)
2020-07-29 21:11:28 +00:00
if err != nil || !(status == 204 || status == 200) {
return status, err
}
return status, nil
}