Skip to content

Instantly share code, notes, and snippets.

@ardyantohermawan
Created January 9, 2017 08:36
Show Gist options
  • Save ardyantohermawan/84cf8201ec99d6a9658379263f1f2918 to your computer and use it in GitHub Desktop.
Save ardyantohermawan/84cf8201ec99d6a9658379263f1f2918 to your computer and use it in GitHub Desktop.
apicall
package main
import (
"fmt"
"github.com/rhinoman/couchdb-go"
"time"
)
type APICall struct {
AppId string `json:"appId"`
Total int `json:"total"`
Remaining int `json:"remaining"`
Date string `json:"date"`
Type string `json:"type"`
}
func upsert() {
var timeout = time.Duration(500 * time.Millisecond)
conn, err := couchdb.NewConnection("127.0.0.1", 5984, timeout)
fmt.Println(err)
// auth := couchdb.BasicAuth{Username: "user", Password: "password"}
db := conn.SelectDB("apicalls", nil)
appID := "wawan"
t := time.Now()
fulldate := t.Format("2006-01-02")
apiCall := APICall{
AppId: appID,
Total: 3000,
Remaining: 2900,
Date: fulldate,
Type: "REST",
}
theId := fmt.Sprintf("%s%sREST", appID, fulldate)
var docs interface{}
rev, err := db.Read(theId, &docs, nil)
if err != nil {
rev, err := db.Save(apiCall, theId, "")
fmt.Println(rev)
fmt.Println(err)
} else {
rev, err := db.Save(apiCall, theId, rev)
fmt.Println(rev)
fmt.Println(err)
}
}
func main() {
go upsert()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment