func preflightHookGet(caller *Caller, hctx *HookContext) (*jsonrpc.RPCResponse, error) {
	var (
		urlSuffix, metricLabel string
		isPaidStream           bool
	)
	query := hctx.Query

	response := &jsonrpc.RPCResponse{
		ID:      query.Request.ID,
		JSONRPC: query.Request.JSONRPC,
	}
	responseResult := map[string]interface{}{
		ParamStreamingUrl: "UNSET",
	}

	// uri vs url is not a typo, `get` query parameter will be called `uri`. It's `url(s)` in all other method calls.
	url := query.ParamsAsMap()["uri"].(string)
	log := logger.Log().WithField("url", url)

	claim, err := resolve(caller, query, url)
	if err != nil {
		return nil, err
	}
	stream := claim.Value.GetStream()

	feeAmount := stream.GetFee().GetAmount()
	if feeAmount > 0 {
		isPaidStream = true

		purchaseQuery, err := NewQuery(jsonrpc.NewRequest(
			MethodPurchaseCreate,
			map[string]interface{}{
				"url":      url,
				"blocking": true,
			},
		), query.WalletID)
		if err != nil {
			return nil, err
		}
		purchaseRes, err := caller.callQueryWithRetry(purchaseQuery)
		if err != nil {
			return nil, err
		}
		if purchaseRes.Error != nil {
			if reAlreadyPurchased.MatchString(purchaseRes.Error.Message) {
				log.Debug("purchase_create says stream is already purchased")
			} else if rePurchaseFree.MatchString(purchaseRes.Error.Message) {
				log.Debug("purchase_create says stream is free")
			} else {
				return nil, fmt.Errorf("purchase error: %v", purchaseRes.Error.Message)
			}
		} else {
			// Assuming the stream is of a paid variety and we have just paid for the stream
			metrics.LbrytvPurchases.Inc()
			metrics.LbrytvPurchaseAmounts.Observe(float64(feeAmount))
			log.Infof("made a purchase for %d LBC", feeAmount)
			// This is needed so changes can propagate for the subsequent resolve
			time.Sleep(1 * time.Second)
			claim, err = resolve(caller, query, url)
			if err != nil {
				return nil, err
			}
		}
	}

	if isPaidStream {
		metricLabel = metrics.LabelValuePaid
	} else {
		metricLabel = metrics.LabelValueFree
	}
	metrics.LbrytvStreamRequests.WithLabelValues(metricLabel).Inc()

	sdHash := hex.EncodeToString(stream.GetSource().SdHash)[:6]
	if isPaidStream {
		size := stream.GetSource().GetSize()
		if claim.PurchaseReceipt == nil {
			log.Errorf("stream was paid for but receipt not found in the resolve response")
			return nil, fmt.Errorf("couldn't find purchase receipt for paid stream")
		}

		log.Debugf("creating stream token with stream id=%s, txid=%s, size=%v", claim.Name+"/"+claim.ClaimID, claim.PurchaseReceipt.Txid, size)
		token, err := paid.CreateToken(claim.Name+"/"+claim.ClaimID, claim.PurchaseReceipt.Txid, size, paid.ExpTenSecPer100MB)
		if err != nil {
			return nil, err
		}
		urlSuffix = fmt.Sprintf("paid/%s/%s/%s/%s", claim.Name, claim.ClaimID, sdHash, token)
		responseResult[ParamPurchaseReceipt] = claim.PurchaseReceipt
	} else {
		urlSuffix = fmt.Sprintf("free/%s/%s/%s", claim.Name, claim.ClaimID, sdHash)
	}

	responseResult[ParamStreamingUrl] = config.GetConfig().Viper.GetString("BaseContentURL") + urlSuffix

	response.Result = responseResult
	return response, nil
}
// preflightHookGet will completely replace `get` request from the client with `purchase_create` + `resolve`.
// This workaround is due to stability issues in the lbrynet SDK `get` method implementation.
// Only `ParamStreamingUrl` will be returned, plus `purchase_receipt` if stream has been paid for.
preflightHookGet will completely replace `get` request from the client with `purchase_create` + `resolve`.
This workaround is due to stability issues in the lbrynet SDK `get` method implementation.
Only `ParamStreamingUrl` will be returned, plus `purchase_receipt` if stream has been paid for.
// preflightHookGet will completely replace ` get ` request from the client with ` purchase_create ` + ` resolve ` . // This workaround is due to stability issues in the lbrynet SDK ` get ` method implementation . // Only ` ParamStreamingUrl ` will be returned plus ` purchase_receipt ` if stream has been paid for .
func preflightHookGet(caller *Caller, hctx *HookContext) (*jsonrpc.RPCResponse, error) {
	var (
		urlSuffix, metricLabel string
		isPaidStream           bool
	)
	query := hctx.Query
	response := &jsonrpc.RPCResponse{
		ID:      query.Request.ID,
		JSONRPC: query.Request.JSONRPC,
	}
	responseResult := map[string]interface{}{
		ParamStreamingUrl: "UNSET",
	}
	url := query.ParamsAsMap()["uri"].(string)
	log := logger.Log().WithField("url", url)
	claim, err := resolve(caller, query, url)
	if err != nil {
		return nil, err
	}
	stream := claim.Value.GetStream()
	feeAmount := stream.GetFee().GetAmount()
	if feeAmount > 0 {
		isPaidStream = true
		purchaseQuery, err := NewQuery(jsonrpc.NewRequest(
			MethodPurchaseCreate,
			map[string]interface{}{
				"url":      url,
				"blocking": true,
			},
		), query.WalletID)
		if err != nil {
			return nil, err
		}
		purchaseRes, err := caller.callQueryWithRetry(purchaseQuery)
		if err != nil {
			return nil, err
		}
		if purchaseRes.Error != nil {
			if reAlreadyPurchased.MatchString(purchaseRes.Error.Message) {
				log.Debug("purchase_create says stream is already purchased")
			} else if rePurchaseFree.MatchString(purchaseRes.Error.Message) {
				log.Debug("purchase_create says stream is free")
			} else {
				return nil, fmt.Errorf("purchase error: %v", purchaseRes.Error.Message)
			}
		} else {
			metrics.LbrytvPurchases.Inc()
			metrics.LbrytvPurchaseAmounts.Observe(float64(feeAmount))
			log.Infof("made a purchase for %d LBC", feeAmount)
			time.Sleep(1 * time.Second)
			claim, err = resolve(caller, query, url)
			if err != nil {
				return nil, err
			}
		}
	}
	if isPaidStream {
		metricLabel = metrics.LabelValuePaid
	} else {
		metricLabel = metrics.LabelValueFree
	}
	metrics.LbrytvStreamRequests.WithLabelValues(metricLabel).Inc()
	sdHash := hex.EncodeToString(stream.GetSource().SdHash)[:6]
	if isPaidStream {
		size := stream.GetSource().GetSize()
		if claim.PurchaseReceipt == nil {
			log.Errorf("stream was paid for but receipt not found in the resolve response")
			return nil, fmt.Errorf("couldn't find purchase receipt for paid stream")
		}
		log.Debugf("creating stream token with stream id=%s, txid=%s, size=%v", claim.Name+"/"+claim.ClaimID, claim.PurchaseReceipt.Txid, size)
		token, err := paid.CreateToken(claim.Name+"/"+claim.ClaimID, claim.PurchaseReceipt.Txid, size, paid.ExpTenSecPer100MB)
		if err != nil {
			return nil, err
		}
		urlSuffix = fmt.Sprintf("paid/%s/%s/%s/%s", claim.Name, claim.ClaimID, sdHash, token)
		responseResult[ParamPurchaseReceipt] = claim.PurchaseReceipt
	} else {
		urlSuffix = fmt.Sprintf("free/%s/%s/%s", claim.Name, claim.ClaimID, sdHash)
	}
	responseResult[ParamStreamingUrl] = config.GetConfig().Viper.GetString("BaseContentURL") + urlSuffix
	response.Result = responseResult
	return response, nil
}
func preflightHookGet ( caller * Caller , hctx * HookContext ) ( * jsonrpc . RPCResponse , error ) { var ( urlSuffix , metricLabel string isPaidStream bool ) query := hctx . Query response := & jsonrpc . RPCResponse { ID : query . Request . ID , JSONRPC : query . Request . JSONRPC , } responseResult := map [ string ] interface { } { ParamStreamingUrl : " " , } url := query . ParamsAsMap ( ) [ " " ] . ( string ) log := logger . Log ( ) . WithField ( " " , url ) claim , err := resolve ( caller , query , url ) if err != nil { return nil , err } stream := claim . Value . GetStream ( ) feeAmount := stream . GetFee ( ) . GetAmount ( ) if feeAmount > 0 { isPaidStream = true purchaseQuery , err := NewQuery ( jsonrpc . NewRequest ( MethodPurchaseCreate , map [ string ] interface { } { " " : url , " " : true , } , ) , query . WalletID ) if err != nil { return nil , err } purchaseRes , err := caller . callQueryWithRetry ( purchaseQuery ) if err != nil { return nil , err } if purchaseRes . Error != nil { if reAlreadyPurchased . MatchString ( purchaseRes . Error . Message ) { log . Debug ( " " ) } else if rePurchaseFree . MatchString ( purchaseRes . Error . Message ) { log . Debug ( " " ) } else { return nil , fmt . Errorf ( " " , purchaseRes . Error . Message ) } } else { metrics . LbrytvPurchases . Inc ( ) metrics . LbrytvPurchaseAmounts . Observe ( float64 ( feeAmount ) ) log . Infof ( " " , feeAmount ) time . Sleep ( 1 * time . Second ) claim , err = resolve ( caller , query , url ) if err != nil { return nil , err } } } if isPaidStream { metricLabel = metrics . LabelValuePaid } else { metricLabel = metrics . LabelValueFree } metrics . LbrytvStreamRequests . WithLabelValues ( metricLabel ) . Inc ( ) sdHash := hex . EncodeToString ( stream . GetSource ( ) . SdHash ) [ : 6 ] if isPaidStream { size := stream . GetSource ( ) . GetSize ( ) if claim . PurchaseReceipt == nil { log . Errorf ( " " ) return nil , fmt . Errorf ( " " ) } log . Debugf ( " " , claim . Name + " " + claim . ClaimID , claim . PurchaseReceipt . Txid , size ) token , err := paid . CreateToken ( claim . Name + " " + claim . ClaimID , claim . PurchaseReceipt . Txid , size , paid . ExpTenSecPer100MB ) if err != nil { return nil , err } urlSuffix = fmt . Sprintf ( " " , claim . Name , claim . ClaimID , sdHash , token ) responseResult [ ParamPurchaseReceipt ] = claim . PurchaseReceipt } else { urlSuffix = fmt . Sprintf ( " " , claim . Name , claim . ClaimID , sdHash ) } responseResult [ ParamStreamingUrl ] = config . GetConfig ( ) . Viper . GetString ( " " ) + urlSuffix response . Result = responseResult return response , nil }
preflightHookGet will completely replace `get` request from the client with `purchase_create` + `resolve`.
preflightHookGet will completely replace ` get ` request from the client with ` purchase_create ` + ` resolve ` .
// uri vs url is not a typo, `get` query parameter will be called `uri`. It's `url(s)` in all other method calls. // Assuming the stream is of a paid variety and we have just paid for the stream // This is needed so changes can propagate for the subsequent resolve