https://github.com/hekmon/plexwebhooks
Golang bindings for Plex Webhooks
https://github.com/hekmon/plexwebhooks
bindings golang golang-library library plex plex-media-server webhook webhooks
Last synced: about 1 year ago
JSON representation
Golang bindings for Plex Webhooks
- Host: GitHub
- URL: https://github.com/hekmon/plexwebhooks
- Owner: hekmon
- License: mit
- Created: 2019-05-05T14:15:58.000Z (about 7 years ago)
- Default Branch: master
- Last Pushed: 2021-05-15T07:25:25.000Z (about 5 years ago)
- Last Synced: 2024-06-21T00:13:32.731Z (about 2 years ago)
- Topics: bindings, golang, golang-library, library, plex, plex-media-server, webhook, webhooks
- Language: Go
- Homepage:
- Size: 58.6 KB
- Stars: 5
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Plex Webhooks
[](https://pkg.go.dev/github.com/hekmon/plexwebhooks) [](https://goreportcard.com/report/github.com/hekmon/plexwebhooks)
Golang binding for [Plex Webhooks](https://support.plex.tv/articles/115002267687-webhooks/).
This library provides:
* Golang binding for webhook (JSON) payloads
* Auto convert values to Golang types when possible (time, duration, IP, URL, etc...)
* Multipart reader extractor which returns the hook payload and the thumbnail if present
## Example
```go
package main
import (
"fmt"
"net/http"
"time"
"github.com/hekmon/plexwebhooks"
)
func main() {
http.HandleFunc("/", processHandler)
http.ListenAndServe(":7095", http.DefaultServeMux)
}
func processHandler(w http.ResponseWriter, r *http.Request) {
defer r.Body.Close()
// Create the multi part reader
multiPartReader, err := r.MultipartReader()
if err != nil {
// Detect error type for the http answer
if err == http.ErrNotMultipart || err == http.ErrMissingBoundary {
w.WriteHeader(http.StatusBadRequest)
} else {
w.WriteHeader(http.StatusInternalServerError)
}
// Try to write the error as http body
_, wErr := w.Write([]byte(err.Error()))
if wErr != nil {
err = fmt.Errorf("request error: %v | write error: %v", err, wErr)
}
// Log the error
fmt.Println("can't create a multipart reader from request:", err)
return
}
// Use the multipart reader to parse the request body
payload, thumb, err := plexwebhooks.Extract(multiPartReader)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
// Try to write the error as http body
_, wErr := w.Write([]byte(err.Error()))
if wErr != nil {
err = fmt.Errorf("request error: %v | write error: %v", err, wErr)
}
// Log the error
fmt.Println("can't create a multipart reader from request:", err)
return
}
// Do something
fmt.Println()
fmt.Println(time.Now())
fmt.Printf("%+v\n", *payload)
if thumb != nil {
fmt.Printf("Name: %s | Size: %d\n", thumb.Filename, len(thumb.Data))
}
fmt.Println()
}
```