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

161 lines
4.5 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) ([]map[string]interface{}, int, error) {
2020-07-29 21:11:28 +00:00
var result []map[string]interface{}
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
}
json.Unmarshal([]byte(data), &result)
2020-08-16 15:08:37 +00:00
jf.userCache = result
jf.CacheExpiry = time.Now().Add(time.Minute * time.Duration(jf.cacheLength))
if id, ok := result[0]["Id"]; ok {
if id.(string)[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) (map[string]interface{}, int, error) {
var match map[string]interface{}
find := func() (map[string]interface{}, int, error) {
users, status, err := jf.GetUsers(public)
if err != nil || status != 200 {
return nil, status, err
}
for _, user := range users {
if user["Name"].(string) == username {
return user, status, err
}
}
2020-07-29 21:11:28 +00:00
return nil, status, err
}
match, status, err := find()
if match == nil {
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) (map[string]interface{}, int, error) {
if jf.CacheExpiry.After(time.Now()) {
2020-08-16 15:08:37 +00:00
for _, user := range jf.userCache {
if user["Id"].(string) == 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 nil, status, err
}
for _, user := range users {
if user["Id"].(string) == userID {
2020-07-29 21:11:28 +00:00
return user, status, nil
}
}
return nil, status, err
}
var result map[string]interface{}
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 nil, 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) (map[string]interface{}, 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)
2020-07-29 21:11:28 +00:00
var recv map[string]interface{}
json.Unmarshal([]byte(response), &recv)
2020-07-29 21:11:28 +00:00
if err != nil || !(status == 200 || status == 204) {
return nil, status, err
}
return recv, status, nil
}
func jfSetPolicy(jf *MediaBrowser, userID string, policy map[string]interface{}) (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 map[string]interface{}) (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
}