github.com/smugmug/godynamo@v0.0.0-20151122084750-7913028f6623/tests/get_item-livetest.go (about)

     1  package main
     2  
     3  import (
     4  	"encoding/json"
     5  	"fmt"
     6  	"github.com/smugmug/godynamo/conf"
     7  	"github.com/smugmug/godynamo/conf_file"
     8  	conf_iam "github.com/smugmug/godynamo/conf_iam"
     9  	get "github.com/smugmug/godynamo/endpoints/get_item"
    10  	keepalive "github.com/smugmug/godynamo/keepalive"
    11  	"github.com/smugmug/godynamo/types/attributevalue"
    12  	"log"
    13  	"net/http"
    14  )
    15  
    16  func main() {
    17  	conf_file.Read()
    18  	conf.Vals.ConfLock.RLock()
    19  	if conf.Vals.Initialized == false {
    20  		panic("the conf.Vals global conf struct has not been initialized")
    21  	}
    22  
    23  	// launch a background poller to keep conns to aws alive
    24  	if conf.Vals.Network.DynamoDB.KeepAlive {
    25  		log.Printf("launching background keepalive")
    26  		go keepalive.KeepAlive([]string{})
    27  	}
    28  
    29  	// deal with iam, or not
    30  	if conf.Vals.UseIAM {
    31  		iam_ready_chan := make(chan bool)
    32  		go conf_iam.GoIAM(iam_ready_chan)
    33  		_ = <-iam_ready_chan
    34  	}
    35  	conf.Vals.ConfLock.RUnlock()
    36  
    37  	get1 := get.NewGetItem()
    38  	get1.TableName = "test-godynamo-livetest"
    39  	// make sure this item has actually been inserted previously
    40  	get1.Key["TheHashKey"] = &attributevalue.AttributeValue{S: "AHashKey264"}
    41  	get1.Key["TheRangeKey"] = &attributevalue.AttributeValue{N: "264"}
    42  	body, code, err := get1.EndpointReq()
    43  	if err != nil || code != http.StatusOK {
    44  		fmt.Printf("get failed %d %v %s\n", code, err, body)
    45  	}
    46  	fmt.Printf("%v\n%v\n,%v\n", string(body), code, err)
    47  
    48  	resp := get.NewResponse()
    49  	um_err := json.Unmarshal([]byte(body), resp)
    50  	if um_err != nil {
    51  		log.Fatal(um_err)
    52  	}
    53  	j, jerr := json.Marshal(resp)
    54  	if jerr != nil {
    55  		log.Fatal(jerr)
    56  	}
    57  	fmt.Printf("RESP:%s\n", string(j))
    58  
    59  	// Try converting the Response to a ResponseItemJSON
    60  	c, cerr := resp.ToResponseItemJSON()
    61  	if cerr != nil {
    62  		log.Fatal(cerr)
    63  	}
    64  	jc, jcerr := json.Marshal(c)
    65  	if jcerr != nil {
    66  		log.Fatal(jcerr)
    67  	}
    68  	fmt.Printf("JSON:%s\n", string(jc))
    69  }