Skip to content

Commit

Permalink
Correcting code formatting after #1567
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexxIT committed Jan 25, 2025
1 parent fc02e6f commit f072dab
Show file tree
Hide file tree
Showing 6 changed files with 703 additions and 703 deletions.
102 changes: 0 additions & 102 deletions internal/ring/init.go

This file was deleted.

102 changes: 102 additions & 0 deletions internal/ring/ring.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
package ring

import (
"encoding/json"
"net/http"
"net/url"

"github.com/AlexxIT/go2rtc/internal/api"
"github.com/AlexxIT/go2rtc/internal/streams"
"github.com/AlexxIT/go2rtc/pkg/core"
"github.com/AlexxIT/go2rtc/pkg/ring"
)

func Init() {
streams.HandleFunc("ring", func(source string) (core.Producer, error) {
return ring.Dial(source)
})

api.HandleFunc("api/ring", apiRing)
}

func apiRing(w http.ResponseWriter, r *http.Request) {
query := r.URL.Query()
var ringAPI *ring.RingRestClient
var err error

// Check auth method
if email := query.Get("email"); email != "" {
// Email/Password Flow
password := query.Get("password")
code := query.Get("code")

ringAPI, err = ring.NewRingRestClient(ring.EmailAuth{
Email: email,
Password: password,
}, nil)

if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}

// Try authentication (this will trigger 2FA if needed)
if _, err = ringAPI.GetAuth(code); err != nil {
if ringAPI.Using2FA {
// Return 2FA prompt
json.NewEncoder(w).Encode(map[string]interface{}{
"needs_2fa": true,
"prompt": ringAPI.PromptFor2FA,
})
return
}
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
} else {
// Refresh Token Flow
refreshToken := query.Get("refresh_token")
if refreshToken == "" {
http.Error(w, "either email/password or refresh_token is required", http.StatusBadRequest)
return
}

ringAPI, err = ring.NewRingRestClient(ring.RefreshTokenAuth{
RefreshToken: refreshToken,
}, nil)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
}

// Fetch devices
devices, err := ringAPI.FetchRingDevices()
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}

// Create clean query with only required parameters
cleanQuery := url.Values{}
cleanQuery.Set("refresh_token", ringAPI.RefreshToken)

var items []*api.Source
for _, camera := range devices.AllCameras {
cleanQuery.Set("device_id", camera.DeviceID)

// Stream source
items = append(items, &api.Source{
Name: camera.Description,
URL: "ring:?" + cleanQuery.Encode(),
})

// Snapshot source
items = append(items, &api.Source{
Name: camera.Description + " Snapshot",
URL: "ring:?" + cleanQuery.Encode() + "&snapshot",
})
}

api.ResponseSources(w, items)
}
Loading

0 comments on commit f072dab

Please sign in to comment.