How to use checkErr method of cdp Package

Best Rod code snippet using cdp.checkErr

dycdp.order.add.go

Source:dycdp.order.add.go Github

copy

Full Screen

1package redeem2import (3 "github.com/aliyun/alibaba-cloud-sdk-go/sdk/requests"4 "github.com/nlopes/slack"5 //"github.com/davecgh/go-spew/spew"6 "encoding/json"7 "github.com/gin-gonic/gin"8 "github.com/mkideal/log"9 "github.com/nu7hatch/gouuid"10 "github.com/shopspring/decimal"11 "github.com/tokenme/tmm/common"12 . "github.com/tokenme/tmm/handler"13 "github.com/tokenme/tmm/tools/dycdp"14 "github.com/tokenme/tmm/tools/forex"15 "github.com/tokenme/tmm/utils"16 "github.com/xluohome/phonedata"17 "net/http"18 "strconv"19 "strings"20 "time"21)22type DycdpOrderAddRequest struct {23 OfferId uint64 `json:"offer_id" form:"offer_id" binding:"required"`24 DeviceId string `json:"device_id" form:"device_id" binding:"required"`25}26func DycdpOrderAddHandler(c *gin.Context) {27 userContext, exists := c.Get("USER")28 if CheckWithCode(!exists, UNAUTHORIZED_ERROR, "need login", c) {29 return30 }31 user := userContext.(common.User)32 var req DycdpOrderAddRequest33 if CheckErr(c.Bind(&req), c) {34 return35 }36 if CheckWithCode(user.CountryCode != 86, INVALID_CDP_VENDOR_ERROR, "the cdp vendor not supported", c) {37 return38 }39 if CheckErr(user.IsBlocked(Service), c) {40 log.Error("Blocked User:%d", user.Id)41 return42 }43 userMobile := strings.TrimSpace(user.Mobile)44 phone, err := phonedata.Find(userMobile)45 if CheckErr(err, c) {46 return47 }48 if CheckWithCode(phone.CardType != "中国移动" && phone.CardType != "中国联通" && phone.CardType != "中国电信", INVALID_CDP_VENDOR_ERROR, "the cdp vendor not supported", c) {49 return50 }51 cdpClient, err := dycdp.NewClientWithAccessKey(Config.Aliyun.RegionId, Config.Aliyun.AK, Config.Aliyun.AS)52 if CheckErr(err, c) {53 return54 }55 cdpRequest := dycdp.CreateQueryCdpOfferByIdRequest()56 cdpRequest.OfferId = strconv.FormatUint(req.OfferId, 10)57 cdpResponse, err := cdpClient.QueryCdpOfferById(cdpRequest)58 if CheckErr(err, c) {59 return60 }61 offers := cdpResponse.FlowOffers.FlowOffer62 if CheckWithCode(len(offers) == 0, NOTFOUND_ERROR, "not found", c) {63 return64 }65 offer := offers[0]66 price := decimal.New(int64(offer.Price), 0).Div(decimal.New(100, 0))67 pointPrice := common.GetPointPrice(Service, Config)68 currencyRate := forex.Rate(Service, "USD", "CNY")69 pointPrice = pointPrice.Mul(currencyRate)70 if CheckWithCode(!pointPrice.GreaterThan(decimal.Zero), INTERNAL_ERROR, "system error", c) {71 return72 }73 points := price.Div(pointPrice)74 db := Service.Db75 rows, _, err := db.Query(`SELECT points FROM tmm.devices WHERE id='%s' AND user_id=%d`, db.Escape(req.DeviceId), user.Id)76 if CheckErr(err, c) {77 return78 }79 if CheckWithCode(len(rows) == 0, NOTFOUND_ERROR, "not found", c) {80 return81 }82 userPoints, _ := decimal.NewFromString(rows[0].Str(0))83 if CheckWithCode(userPoints.LessThan(points), NOT_ENOUGH_POINTS_ERROR, "not enough points", c) {84 return85 }86 token, err := uuid.NewV4()87 if CheckErr(err, c) {88 return89 }90 outerOrderId := utils.Sha1(token.String())91 orderRequest := dycdp.CreateCreateCdpOrderRequest()92 orderRequest.PhoneNumber = userMobile93 orderRequest.OfferId = requests.NewInteger64(int64(req.OfferId))94 orderRequest.OutOrderId = outerOrderId95 orderResponse, err := cdpClient.CreateCdpOrder(orderRequest)96 if CheckErr(err, c) {97 return98 }99 if orderResponse.Code == "OK" {100 pointsPerTs, _ := common.GetTMMPerTs(Config, Service)101 ts := points.Div(pointsPerTs)102 _, _, err := db.Query(`UPDATE tmm.devices SET points = IF(points > %s, points - %s, 0), consumed_ts = consumed_ts + %d WHERE id='%s'`, points.String(), points.String(), ts.IntPart(), db.Escape(req.DeviceId))103 if err != nil {104 log.Error(err.Error())105 } else {106 _, _, err = db.Query(`INSERT INTO tmm.cdp_orders (outer_order_id, device_id, order_id, points, offer_id, grade) VALUES ('%s', '%s', '%s', %s, %d, '%s')`, db.Escape(outerOrderId), db.Escape(req.DeviceId), db.Escape(orderResponse.Data.OrderId), points.String(), req.OfferId, db.Escape(offer.Grade))107 if err != nil {108 log.Error(err.Error())109 }110 }111 } else if Check(orderResponse.Message != "", orderResponse.Message, c) {112 return113 }114 params := slack.PostMessageParameters{Parse: "full", UnfurlMedia: true, Markdown: true}115 attachment := slack.Attachment{116 Color: "success",117 AuthorName: user.ShowName,118 AuthorIcon: user.Avatar,119 Title: "Redeem Mobile Data",120 Fallback: "Fallback message",121 Fields: []slack.AttachmentField{122 {123 Title: "CountryCode",124 Value: strconv.FormatUint(uint64(user.CountryCode), 10),125 Short: true,126 },127 {128 Title: "UserID",129 Value: strconv.FormatUint(user.Id, 10),130 Short: true,131 },132 {133 Title: "Points",134 Value: points.StringFixed(4),135 Short: true,136 },137 {138 Title: "Grade",139 Value: offer.Grade,140 Short: true,141 },142 {143 Title: "Price",144 Value: strconv.FormatUint(offer.Price, 10),145 Short: true,146 },147 {148 Title: "Discount",149 Value: offer.Discount.String(),150 Short: true,151 },152 },153 Ts: json.Number(strconv.FormatInt(time.Now().Unix(), 10)),154 }155 params.Attachments = []slack.Attachment{attachment}156 _, _, err = Service.Slack.PostMessage(Config.Slack.OpsChannel, "Redeem Mobile Data", params)157 if err != nil {158 log.Error(err.Error())159 return160 }161 c.JSON(http.StatusOK, APIResponse{Msg: "ok"})162}...

Full Screen

Full Screen

records.go

Source:records.go Github

copy

Full Screen

1package exchange2import (3 //"github.com/davecgh/go-spew/spew"4 "github.com/gin-gonic/gin"5 "github.com/mkideal/log"6 "github.com/shopspring/decimal"7 "github.com/tokenme/tmm/coins/eth/utils"8 "github.com/tokenme/tmm/common"9 . "github.com/tokenme/tmm/handler"10 "net/http"11 "time"12)13const DEFAULT_PAGE_SIZE = 1014type RecordType = uint815const (16 RedeemCdpRecordType RecordType = 117)18type RecordsRequest struct {19 Page uint `json:"page" form:"page"`20 PageSize uint `json:"page_size" form:"page_size"`21 Direction common.TMMExchangeDirection `json:"direction" form:"direction"`22 Type RecordType `json:"type" form:"type"`23}24func RecordsHandler(c *gin.Context) {25 userContext, exists := c.Get("USER")26 if CheckWithCode(!exists, UNAUTHORIZED_ERROR, "need login", c) {27 return28 }29 user := userContext.(common.User)30 var req RecordsRequest31 if CheckErr(c.Bind(&req), c) {32 return33 }34 if req.Page == 0 {35 req.Page = 136 }37 if req.PageSize == 0 || req.PageSize > DEFAULT_PAGE_SIZE {38 req.PageSize = DEFAULT_PAGE_SIZE39 }40 db := Service.Db41 if req.Type == RedeemCdpRecordType {42 query := `SELECT43 co.device_id, co.points, co.grade, co.inserted_at44 FROM tmm.cdp_orders AS co45 INNER JOIN tmm.devices AS d ON (d.id=co.device_id)46 WHERE d.user_id=%d ORDER BY co.inserted_at DESC LIMIT %d, %d`47 rows, _, err := db.Query(query, user.Id, (req.Page-1)*req.PageSize, req.PageSize)48 if CheckErr(err, c) {49 return50 }51 var records []common.RedeemCdpRecord52 for _, row := range rows {53 points, _ := decimal.NewFromString(row.Str(1))54 record := common.RedeemCdpRecord{55 DeviceId: row.Str(0),56 Points: points,57 Grade: row.Str(2),58 InsertedAt: row.ForceLocaltime(3).Format(time.RFC3339),59 }60 records = append(records, record)61 }62 c.JSON(http.StatusOK, records)63 return64 }65 pointsPerTs, err := common.GetPointsPerTs(Service)66 if CheckErr(err, c) {67 return68 }69 query := `SELECT70 tx, status, device_id, tmm, points, direction, inserted_at71 FROM tmm.exchange_records72 WHERE user_id=%d AND direction=%d ORDER BY inserted_at DESC LIMIT %d, %d`73 rows, _, err := db.Query(query, user.Id, req.Direction, (req.Page-1)*req.PageSize, req.PageSize)74 if CheckErr(err, c) {75 return76 }77 var records []common.ExchangeRecord78 for _, row := range rows {79 tmm, _ := decimal.NewFromString(row.Str(3))80 points, _ := decimal.NewFromString(row.Str(4))81 record := common.ExchangeRecord{82 Tx: row.Str(0),83 Status: common.ExchangeTxStatus(row.Uint(1)),84 DeviceId: row.Str(2),85 Tmm: tmm,86 Points: points,87 Direction: common.TMMExchangeDirection(row.Int(5)),88 InsertedAt: row.ForceLocaltime(6).Format(time.RFC3339),89 }90 if record.Status == common.ExchangeTxPending {91 receipt, err := utils.TransactionReceipt(Service.Geth, c, record.Tx)92 if err == nil {93 record.Status = common.ExchangeTxStatus(receipt.Status)94 if record.Status == common.ExchangeTxFailed && record.Direction == common.TMMExchangeIn {95 _, _, err = db.Query(`UPDATE tmm.devices AS d, tmm.exchange_records AS er SET d.points=d.points + er.points, d.total_ts = CEIL(d.total_ts + %s), er.status=0 WHERE d.id=er.device_id AND er.tx='%s'`, pointsPerTs.String(), db.Escape(record.Tx))96 if err != nil {97 log.Error(err.Error())98 }99 } else if record.Status == common.ExchangeTxFailed && record.Direction == common.TMMExchangeOut {100 _, _, err = db.Query(`UPDATE tmm.devices AS d, tmm.exchange_records AS er SET d.points=IF(d.points > er.points, d.points - er.points, 0), d.consumed_ts = CEIL(d.consumed_ts + %s), er.status=0 WHERE d.id=er.device_id AND er.tx='%s'`, pointsPerTs.String(), db.Escape(record.Tx))101 if err != nil {102 log.Error(err.Error())103 }104 } else {105 _, _, err := db.Query(`UPDATE tmm.exchange_records SET status=%d WHERE tx='%s'`, receipt.Status, db.Escape(record.Tx))106 if err != nil {107 log.Error(err.Error())108 }109 }110 }111 }112 records = append(records, record)113 }114 c.JSON(http.StatusOK, records)115}...

Full Screen

Full Screen

dycdp.list.go

Source:dycdp.list.go Github

copy

Full Screen

1package redeem2import (3 "encoding/json"4 //"github.com/davecgh/go-spew/spew"5 "fmt"6 "github.com/garyburd/redigo/redis"7 "github.com/gin-gonic/gin"8 "github.com/mkideal/log"9 "github.com/shopspring/decimal"10 "github.com/tokenme/tmm/common"11 . "github.com/tokenme/tmm/handler"12 "github.com/tokenme/tmm/tools/dycdp"13 "github.com/tokenme/tmm/tools/forex"14 "github.com/xluohome/phonedata"15 "net/http"16 "sort"17 "strings"18)19const REDEEMCDPS_CACHE_KEY = "CDPSOFFERS-%s-%s"20func DycdpListHandler(c *gin.Context) {21 userContext, exists := c.Get("USER")22 if CheckWithCode(!exists, UNAUTHORIZED_ERROR, "need login", c) {23 return24 }25 user := userContext.(common.User)26 if CheckWithCode(user.CountryCode != 86, INVALID_CDP_VENDOR_ERROR, "the cdp vendor not supported", c) {27 return28 }29 phone, err := phonedata.Find(strings.TrimSpace(user.Mobile))30 if CheckErr(err, c) {31 log.Error("%s %s", err.Error(), user.Mobile)32 return33 }34 if CheckWithCode(phone.CardType != "中国移动" && phone.CardType != "中国联通" && phone.CardType != "中国电信", INVALID_CDP_VENDOR_ERROR, "the cdp vendor not supported", c) {35 log.Error(err.Error())36 return37 }38 var offers []dycdp.FlowOffer39 redisConn := Service.Redis.Master.Get()40 defer redisConn.Close()41 cacheKey := fmt.Sprintf(REDEEMCDPS_CACHE_KEY, phone.CardType, phone.Province)42 {43 js, _ := redis.Bytes(redisConn.Do("GET", cacheKey))44 json.Unmarshal(js, &offers)45 }46 if len(offers) == 0 {47 cdpClient, err := dycdp.NewClientWithAccessKey(Config.Aliyun.RegionId, Config.Aliyun.AK, Config.Aliyun.AS)48 if CheckErr(err, c) {49 log.Error(err.Error())50 return51 }52 cdpRequest := dycdp.CreateQueryCdpOfferRequest()53 cdpRequest.ChannelType = "分省"54 cdpRequest.Vendor = phone.CardType55 cdpRequest.Province = phone.Province56 cdpResponse, err := cdpClient.QueryCdpOffer(cdpRequest)57 if CheckWithCode(err != nil, INTERNAL_ERROR, "internal error", c) {58 log.Error(err.Error())59 return60 }61 offers = cdpResponse.FlowOffers.FlowOffer62 {63 cdpRequest := dycdp.CreateQueryCdpOfferRequest()64 cdpRequest.ChannelType = "全国"65 cdpRequest.Vendor = phone.CardType66 cdpResponse, err := cdpClient.QueryCdpOffer(cdpRequest)67 if CheckWithCode(err != nil, INTERNAL_ERROR, "internal error", c) {68 log.Error(err.Error())69 return70 }71 newOffers := cdpResponse.FlowOffers.FlowOffer72 offers = append(offers, newOffers...)73 js, err := json.Marshal(offers)74 if err == nil {75 redisConn.Do("SETEX", cacheKey, 2*60*60, js)76 }77 }78 }79 gradeMap := make(map[string]*common.RedeemCdp)80 for _, offer := range offers {81 if offer.Discount.GreaterThan(decimal.NewFromFloat(0.7)) {82 continue83 }84 price := decimal.New(int64(offer.Price), 0).Div(decimal.New(100, 0))85 if grade, found := gradeMap[offer.Grade]; found {86 if grade.Price.GreaterThan(price) {87 grade.OfferId = offer.OfferId88 grade.Price = price89 }90 } else {91 gradeMap[offer.Grade] = &common.RedeemCdp{92 OfferId: offer.OfferId,93 Grade: offer.Grade,94 Price: price,95 }96 }97 }98 var redeemCdpsSlice common.RedeemCdpSlice99 pointPrice := common.GetPointPrice(Service, Config)100 currencyRate := forex.Rate(Service, "USD", "CNY")101 pointPrice = pointPrice.Mul(currencyRate)102 if pointPrice.GreaterThan(decimal.Zero) {103 for _, grade := range gradeMap {104 grade.Points = grade.Price.Div(pointPrice)105 redeemCdpsSlice = append(redeemCdpsSlice, grade)106 }107 }108 sort.Sort(redeemCdpsSlice)109 c.JSON(http.StatusOK, redeemCdpsSlice)110}...

Full Screen

Full Screen

checkErr

Using AI Code Generation

copy

Full Screen

1import (2const (3type cdp struct {4}5func (c *cdp) draw(renderer *sdl.Renderer) {6 renderer.CopyEx(c.img, nil, &sdl.Rect{int32(c.x), int32(c.y), c.width, c.height}, 0, nil, sdl.FLIP_NONE)7}8func (c *cdp) update() {9 if c.x < 0 {10 } else if c.x > float64(c.windowWidth-c.width) {11 c.x = float64(c.windowWidth - c.width)12 }13 if c.y < 0 {14 } else if c.y > float64(c.windowHeight-c.height) {15 c.y = float64(c.windowHeight - c.height)16 }17}18func (c *cdp) checkErr(e error) {19 if e != nil {20 panic(e)21 }22}23func newCdp(renderer *sdl.Renderer, windowWidth, windowHeight int32) *cdp {24 c := &cdp{windowWidth: windowWidth, windowHeight: windowHeight}25 c.img, err = img.LoadTexture(renderer, "cdp.png")26 c.checkErr(err)27 _, _, c.width, c.height, err = c.img.Query()

Full Screen

Full Screen

checkErr

Using AI Code Generation

copy

Full Screen

1import (2type cdp struct {3}4func (c *cdp) checkErr() {5 if c.err != nil {6 fmt.Println(c.err)7 os.Exit(1)8 }9}10func main() {11 c := cdp{}12 c.err = fmt.Errorf("error")13 c.checkErr()14}15type error interface {16 Error() string17}18import (19func main() {20 err := errors.New("error")21 fmt.Println(err)22}23The errors.New() method is a helper method that is defined as follows:24func New(text string) error {25 return errorsString(text)26}27func (e errorsString) Error() string {28 return string(e)29}30The errors.New() method is a helper method that is defined as follows:31func New(text string) error {32 return errorsString(text)33}34func (e errorsString) Error() string {35 return string(e)36}37The errors.New() method is a helper method that is defined as

Full Screen

Full Screen

checkErr

Using AI Code Generation

copy

Full Screen

1import "fmt"2func main() {3 cdp := cdp{err: fmt.Errorf("error")}4 cdp.checkErr()5}6import "fmt"7func main() {8 cdp = cdp{err: fmt.Errorf("error")}9 cdp.checkErr()10}11import "fmt"12func main() {13 cdp = cdp{err: fmt.Errorf("error")}14 cdp.checkErr()15}16import "fmt"17func main() {18 cdp = cdp{err: fmt.Errorf("error")}19 cdp.checkErr()20}21import "fmt"22func main() {23 cdp = cdp{err: fmt.Errorf("error")}24 cdp.checkErr()25}26import "fmt"27func main() {28 cdp = cdp{err: fmt.Errorf("error")}29 cdp.checkErr()30}31import "fmt"32func main() {33 cdp = cdp{err: fmt.Errorf("error")}34 cdp.checkErr()35}36import "fmt"37func main() {38 cdp = cdp{err: fmt.Errorf("error")}39 cdp.checkErr()40}41import "fmt"42func main() {43 cdp = cdp{err: fmt.Errorf("error")}44 cdp.checkErr()45}

Full Screen

Full Screen

checkErr

Using AI Code Generation

copy

Full Screen

1public class 2 {2 public static void main(String[] args) {3 CDP c = new CDP();4 c.checkErr();5 }6}7public class CDP {8 public void checkErr() {9 System.out.println("Method of CDP class");10 }11}

Full Screen

Full Screen

checkErr

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 file, err := os.Open("test.txt")4 if err != nil {5 fmt.Println(err)6 }7 defer file.Close()8 scanner := bufio.NewScanner(file)9 for scanner.Scan() {10 cdp.checkErr(scanner.Text())11 }12 if err := scanner.Err(); err != nil {13 fmt.Println(err)14 }15}16import (17type cdp struct {18}19func (cdp cdp) checkErr(cdp string) {20 if strings.Contains(cdp, "error") {21 fmt.Println(cdp)22 }23}24cannot use scanner (type *bufio.Scanner) as type string in argument to fmt.Println25import (26func main() {27 file, err := os.Open("test.txt")28 if err != nil {29 fmt.Println(err)30 }31 defer file.Close()32 scanner := bufio.NewScanner(file)33 for scanner.Scan() {34 fmt.Println(scanner)35 }36 if err := scanner.Err(); err != nil {37 fmt.Println(err)38 }39}

Full Screen

Full Screen

checkErr

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 file, err := os.Open("1.go")4 cdp.checkErr(err)5 fmt.Println(file.Name())6 err = file.Close()7 cdp.checkErr(err)8}

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful