sync api change: GWM

This commit is contained in:
Gowtham M 2026-04-07 15:19:23 +05:30
parent e175e986ce
commit 3d347f9511
19 changed files with 59 additions and 44 deletions

47
main.go
View File

@ -2749,7 +2749,7 @@ func main() {
} }
data, _ := json.Marshal(loginPayload) data, _ := json.Marshal(loginPayload)
loginReq, _ := http.NewRequest("POST", fmt.Sprintf("%s/api/admins/auth-with-password", "https://pb.venbait.in"), bytes.NewBuffer(data)) loginReq, _ := http.NewRequest("POST", fmt.Sprintf("%s/api/admins/auth-with-password", "http://127.0.0.1:8090"), bytes.NewBuffer(data))
loginReq.Header.Set("Content-Type", "application/json") loginReq.Header.Set("Content-Type", "application/json")
loginResp, err := http.DefaultClient.Do(loginReq) loginResp, err := http.DefaultClient.Do(loginReq)
@ -2769,25 +2769,32 @@ func main() {
return c.JSON(500, map[string]string{"error": "failed to parse login response"}) return c.JSON(500, map[string]string{"error": "failed to parse login response"})
} }
// 2⃣ Use token to fetch records // 2⃣ Fetch all records with pagination.
url := fmt.Sprintf("%s/api/collections/%s/records?perPage=500", "https://pb.venbait.in", reqBody.Collection) // PocketBase (v0.22.x) caps perPage at 500 (tools/search.MaxPerPage); larger values are ignored,
// so a single ?perPage=5000 request only returns the first 500 rows.
const recordsPerPage = 500
records := []map[string]interface{}{}
for page := 1; ; page++ {
url := fmt.Sprintf("%s/api/collections/%s/records?perPage=%d&page=%d", "http://127.0.0.1:8090", reqBody.Collection, recordsPerPage, page)
fmt.Println("Fetching collection from URL:", url) fmt.Println("Fetching collection from URL:", url)
req, _ := http.NewRequest("GET", url, nil) req, _ := http.NewRequest("GET", url, nil)
req.Header.Set("Authorization", loginRes.Token) req.Header.Set("Authorization", loginRes.Token)
fmt.Println("Using token:", loginRes.Token)
resp, err := http.DefaultClient.Do(req) resp, err := http.DefaultClient.Do(req)
if err != nil { if err != nil {
fmt.Println("Error fetching collection:", err) fmt.Println("Error fetching collection:", err)
return c.JSON(500, map[string]string{"error": "failed to fetch collection"}) return c.JSON(500, map[string]string{"error": "failed to fetch collection"})
} }
defer resp.Body.Close()
body, _ := ioutil.ReadAll(resp.Body) body, _ := ioutil.ReadAll(resp.Body)
fmt.Println("HTTP Status Code:", resp.StatusCode) resp.Body.Close()
fmt.Println("HTTP Status Code:", resp.StatusCode, "page", page)
if resp.StatusCode != 200 {
return c.JSON(500, map[string]string{"error": fmt.Sprintf("fetch collection failed: %s", string(body))})
}
// parse JSON
var parsed map[string]interface{} var parsed map[string]interface{}
if err := json.Unmarshal(body, &parsed); err != nil { if err := json.Unmarshal(body, &parsed); err != nil {
fmt.Println("JSON Unmarshal error:", err) fmt.Println("JSON Unmarshal error:", err)
@ -2798,13 +2805,27 @@ func main() {
if !ok { if !ok {
return c.JSON(500, map[string]string{"error": "no items found"}) return c.JSON(500, map[string]string{"error": "no items found"})
} }
records := []map[string]interface{}{}
for _, i := range items { for _, i := range items {
rec := i.(map[string]interface{}) rec := i.(map[string]interface{})
records = append(records, rec) records = append(records, rec)
} }
var totalPages int
if tp, ok := parsed["totalPages"].(float64); ok {
totalPages = int(tp)
}
if totalPages > 0 {
if page >= totalPages {
break
}
continue
}
// skipTotal or missing totals: stop after a short / empty page
if len(items) < recordsPerPage {
break
}
}
syncResult := map[string]interface{}{"synced": []string{}, "errors": []string{}} syncResult := map[string]interface{}{"synced": []string{}, "errors": []string{}}
// 3⃣ If syncWithLive=true, sync to live PB // 3⃣ If syncWithLive=true, sync to live PB
@ -2819,10 +2840,10 @@ func main() {
"password": reqBody.LivePassword, "password": reqBody.LivePassword,
} }
data, _ := json.Marshal(livePayload) data, _ := json.Marshal(livePayload)
liveReq, _ := http.NewRequest("POST", fmt.Sprintf("%s/api/admins/auth-with-password", "https://pocket.fcsc.gov.ae"), bytes.NewBuffer(data)) liveReq, _ := http.NewRequest("POST", fmt.Sprintf("%s/api/admins/auth-with-password", "https://pb.venbait.in"), bytes.NewBuffer(data))
liveReq.Header.Set("Content-Type", "application/json") liveReq.Header.Set("Content-Type", "application/json")
liveResp, err := http.DefaultClient.Do(liveReq) liveResp, err := http.DefaultClient.Do(liveReq)
fmt.Println("Live url:", "https://pocket.fcsc.gov.ae") fmt.Println("Live url:", "https://pb.venbait.in")
fmt.Println("Live req:", liveReq) fmt.Println("Live req:", liveReq)
if err != nil { if err != nil {
return c.JSON(500, map[string]string{"error": "failed to connect to Live PB"}) return c.JSON(500, map[string]string{"error": "failed to connect to Live PB"})
@ -2845,7 +2866,7 @@ func main() {
data, _ := json.Marshal(rec) data, _ := json.Marshal(rec)
// Try PATCH first // Try PATCH first
patchURL := fmt.Sprintf("%s/api/collections/%s/records/%s", "https://pocket.fcsc.gov.ae", reqBody.Collection, id) patchURL := fmt.Sprintf("%s/api/collections/%s/records/%s", "https://pb.venbait.in", reqBody.Collection, id)
req, _ := http.NewRequest("PATCH", patchURL, bytes.NewBuffer(data)) req, _ := http.NewRequest("PATCH", patchURL, bytes.NewBuffer(data))
req.Header.Set("Authorization", liveLogin.Token) req.Header.Set("Authorization", liveLogin.Token)
req.Header.Set("Content-Type", "application/json") req.Header.Set("Content-Type", "application/json")
@ -2859,7 +2880,7 @@ func main() {
if resp.StatusCode == 404 { if resp.StatusCode == 404 {
// Create instead // Create instead
postURL := fmt.Sprintf("%s/api/collections/%s/records", "https://pocket.fcsc.gov.ae", reqBody.Collection) postURL := fmt.Sprintf("%s/api/collections/%s/records", "https://pb.venbait.in", reqBody.Collection)
req, _ := http.NewRequest("POST", postURL, bytes.NewBuffer(data)) req, _ := http.NewRequest("POST", postURL, bytes.NewBuffer(data))
req.Header.Set("Authorization", liveLogin.Token) req.Header.Set("Authorization", liveLogin.Token)
req.Header.Set("Content-Type", "application/json") req.Header.Set("Content-Type", "application/json")

Binary file not shown.

Binary file not shown.

View File

Before

Width:  |  Height:  |  Size: 14 KiB

After

Width:  |  Height:  |  Size: 14 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 14 KiB

View File

@ -1 +0,0 @@
{"user.cache_control":"","user.content_disposition":"","user.content_encoding":"","user.content_language":"","user.content_type":"image/png","user.metadata":{"original-filename":"IMD.png"},"md5":"8bGx6LhXqxPbgVjRi2Mdgw=="}

View File

@ -1 +0,0 @@
{"user.cache_control":"","user.content_disposition":"","user.content_encoding":"","user.content_language":"","user.content_type":"image/png","user.metadata":null,"md5":"y/a73YxDdhHm9l0Kw6Pi1Q=="}

Binary file not shown.

Before

Width:  |  Height:  |  Size: 169 KiB

View File

@ -1 +0,0 @@
{"user.cache_control":"","user.content_disposition":"","user.content_encoding":"","user.content_language":"","user.content_type":"image/png","user.metadata":{"original-filename":"Open Data.png"},"md5":"zqS1mfeZDSDnS4lCDYYVFA=="}

View File

@ -1 +0,0 @@
{"user.cache_control":"","user.content_disposition":"","user.content_encoding":"","user.content_language":"","user.content_type":"image/png","user.metadata":null,"md5":"BoezhRm3qH1p+B0LKtT2MQ=="}

Binary file not shown.

Before

Width:  |  Height:  |  Size: 14 KiB

View File

@ -1 +0,0 @@
{"user.cache_control":"","user.content_disposition":"","user.content_encoding":"","user.content_language":"","user.content_type":"image/png","user.metadata":{"original-filename":"IMD.png"},"md5":"8bGx6LhXqxPbgVjRi2Mdgw=="}

View File

@ -1 +0,0 @@
{"user.cache_control":"","user.content_disposition":"","user.content_encoding":"","user.content_language":"","user.content_type":"image/png","user.metadata":null,"md5":"y/a73YxDdhHm9l0Kw6Pi1Q=="}