golang get only one stat from json - go

i tried to get one stat from web api request in json.
This is what called https://api.coinmarketcap.com/v1/ticker/ethereum/
I use this github code example // Get info about coin
coinInfo, err := coinApi.GetCoinData("ethereum")
if err != nil {
log.Println(err)
} else {
fmt.Println(coinInfo)
}
My result in log says
{ethereum Ethereum ETH 2 830.48 0.100287 3.23573e+09 8.0977392218e+10 9.7506734e+07 9.7506734e+07 0.61 -0.65 -7.36 1518176353}
But i want only the price_usd if you look at the api domain. Why cannot get only price?
Already tried coinInfo['price_usd'] but its all not working
Here you find the functions to run the GetCoinData
https://github.com/miguelmota/go-coinmarketcap/blob/master/coinmarketcap.go
Can someone help me to get only the price_usd from api in golang?

According to the docs, you can use the PriceUsd field of the Coin type for this.

Related

Setting status code in grpc server method call

How do I set the response status code in a grpc method in golang. For example lets say I have the following grpc method
func (i *ItemServerImp) Register(ct context.Context, it *item.RegisterItemRequest) (*item.RegisterItemReply, error) {
}
How do I set the response status to 200 or a 400 based on the input or some processing. I had a look around and could not find a proper way to do this.
However I did find the following https://chromium.googlesource.com/external/github.com/grpc/grpc/+/refs/heads/chromium-deps/2016-07-27/doc/statuscodes.md which says the status code can be set.
You can return a gRPC error using the google.golang.org/grpc/status package as follows:
return nil, status.Error(codes.InvalidArgument, "Incorrect request argument")
The different status codes are available in the google.golang.org/grpc/codes package.

Deleting a discord message from discordgo

I am trying to delete a discord message using discordgo. Currently I have the ping pong example, however instead of writing a message I want to delete the message.
I have currently got this far:
func messageCreate(s *discordgo.Session, m *discordgo.MessageCreate) {
if m.Author.ID == s.State.User.ID || m.Author.Bot {
return
}
if m.Content == "ping" {
s.ChannelMessageDelete(m.ChannelID, m.ID)
}
}
But the implementation does not delete the message even though other code in the block will run. I am a bit of a newb to go and I don't know if there is some future error .. or something like the bot does not have the correct discord permissions?
The reason it was not posting was because the bot did not have the Manage Messages permission. This can be done by checking the Manage Messages checkbox when generating the invite link on the OAuth tab.

How do i know, which Peer did the Transaction in Hyperledger Fabric Go?

I am working on getting a transaction id info, which will give the peer details for the transaction. Currently, I am able to retrieve the History for a key, which gives me the list of transaction committed to that key.
MyCode:
historyRes, err := stub.GetHistoryForKey(userNameIndexKey)
if err != nil {
return shim.Error(fmt.Sprintf("Unable to get History key from the ledger: %v", err))
}
for historyRes.HasNext() {
history, errIt := historyRes.Next()
if errIt != nil {
return shim.Error(fmt.Sprintf("Unable to retrieve history in the ledger: %v", errIt))
}
deleted := history.GetIsDelete()
ds := strconv.FormatBool(deleted)
fmt.Println("History TxId = "+history.GetTxId()+" -- Delete = "+ds)
}
Output
History TxId = 78c8d17c668d7a9df8373fd85df4fc398388976a1c642753bbf73abc5c648dd8 -- Deleted = false
History TxId = 102bbb64a7ca93367334a8c98f1f7be17e6a8d5277f0167c73da47072d302fa3 -- Deleted = true
But I don't know, which peer did this transaction. Is there any API available in fabric-sdk-go to retrieve peer info for a transaction id.
please suggest me some solution.
The call stub.GetHistoryForKey(userNameIndexKey) will query the state database and not the ledger (channel). The information about the identity who made the transaction is stored in the block.
I have implemented the same thing with NodeJS SDK. However, Go SDK too contains similar API calls. The following steps worked for me:
Using your SDK, get the transactionId
Use the SDK function for querying block by transactionId. References here.
At this step, you'll get the block. Now the identity of the submitter is located within this block. Hints: Payload -> Header -> Signature Header -> Creater -> IdBytes.
These identity bytes are the X509 certs of the submitter. You can read the certificate info to find out which member did submit this transaction. The subject and OUs will indicate the Organization of the peer that did the transaction.

gRPC context on the client side

I am building a client/server system in go, using gRPC and protobuf (and with a gRPC gateway to REST).
I use metadata in the context on the server side to carry authentication data from the client, and that works perfectly well.
Now, I'd like the server to set some metadata keys/values so that the client can get them, alongside with the response. How can I do that? Using SetHeader and SendHeader? Ideally, I'd like every single response from the server to integrate that metadata (can be seen as some kind of UnaryInterceptor, but on the response rather than the request?)
Here is the code for the server and for the client.
I finally found my way: https://github.com/grpc/grpc-go/blob/master/Documentation/grpc-metadata.md
So basically, grpc.SetHeader() + grpc.SendHeader() and grpc.SetTrailer() are totally what I was looking for. On the client side, grpc.Header() and grpc.Trailer() functions need to be passed to the RPC call, and their argument is a metadata.MD object to be filled.
On the client side, define your receiving metadata:
var header, trailer metadata.MD
Then, pass it to the SomeRPCCall() unary RPC:
response, err := client.SomeRPCCall(
context.Background(),
proto.MyMessage{},
grpc.Header(&header),
grpc.Trailer(&trailer),
)
And now, you can check what's in your metadata:
for key, value := range header {
fmt.Printf("%s => %s", key, value)
}
for key, value := range trailer {
fmt.Printf("%s => %s", key, value)
}
On the server side, you can:
force the data to be sent right after the RPC is received (but before it's processed):
grpc.SendHeader(ctx, metadata.New(map[string]string{"my-key": "my-value"}))
or set & send the metadata at the end of the RPC process (along with the Status):
grpc.SetTrailer(ctx, metadata.New(map[string]string{"my-key": "my-value"}))

MtGox Gem Failing to Login

I am trying to use the MtGox gem to cancel orders, however, I only get the following error message:
{"error":"Must be logged in"}
I am using the following code to make the call:
MtGox.configure {|c| c.key = user.mtgox_key; c.secret = user.mtgox_secret}
MtGox.cancel('oid'=>'a3c348d9-36c3-4927-b84d-206f421907f5', 'type'=>1)
I have also tried getting all of the open orders for a specific user using the following:
MtGox.post('/api/0/getOrders.php', {})
Alas, every call returns the same error message. Am I missing something?
This issue was resolved by using a smaller nonce for v1 api calls. Interestingly, a nonce of length 22 was causing subsequent v0 api calls to fail, but not subsequent v1 calls. I compute my nonce by simply using:
var nonce = ((new Date()).getTime()*1000).toString(); // microseconds since epoch

Resources