I'm Using an API example that transform a string into a dictionary and then calculate its md5. Unfortunately there is just a python example and I never developer in this language. So far I tried my best and I'll append what I did. Python code:
What I tried so far is:
But I got some errors because some of the things are quite difficult for me to convert into vb.net For example, how can I use params_XXX without declaring them? Why is it using the word Json while basically I need to pass a String to it?
I got roughly 25 errors and most of them are in the Public Function add_sign_to_params . It is really difficult for me to convert these functions into vb.net as they might have different construction in python.
I am not asking for a ready code, I'm asking help to convert it step by step.
For example, I've already searched on google how to convert a string to md5, but this code is not just converti a string to md5, it is using kind of weird and complex ( to me ) structures like "sort_keys = True, indent = 4)"..
Thanks
Code:
import hashlib
import json
def params_string_to_dict(params_str):
params = params_str.split("&")
params_dict = {}
for param in params:
key, value = param.split("=")
params_dict[key] = value
return params_dict
def params_dict_to_string(params_dict):
params_str = ""
items = params_dict.items()
for key, value in items:
params_str += key + '=' + value + '&'
return params_str[:-1]
def add_sign_to_params(params_dict, api, secret):
params_dict['api_key'] = api
params_str = json.dumps(params_dict, sort_keys=True, indent=4)
params_str = params_dict_to_string(json.loads(params_str))
params_str += '&secret_key=' + secret
hash_md5 = hashlib.md5(params_str.encode(encoding='utf-8'))
sign = hash_md5.hexdigest().upper()
params_dict['sign'] = sign
return params_dict
params_url="Api url"
params_str = "Api endpoint".replace(" ", "")
params_dict = params_string_to_dict(params_str)
#print(params_dict)
params_dict = add_sign_to_params( params_dict,"Key", "Secret Key")
#print(params_dict)
params_str = params_dict_to_string(params_dict)
print( params_url +"?" + params_str)
Code:
Option Strict On
Imports System.Text
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
End Sub
Dim value() As String
Dim key() As String
Public Function params_string_to_dict(params_str As String)
Dim params() As String
params = params_str.Split(CChar("&"))
Dim params_dict As String
params_dict = Nothing
For Each param In params
value = param.Split(CChar("="))
key = param.Split(CChar("="))
params_dict{key} = value
Next
Return params_dict
End Function
Public Function params_dict_to_string(params_dict As String)
Dim params_str As String
params_str = ""
Dim items As String
items = params_dict.items()
For Each key And value in items
params_str += key & "=" & value & "&"
Next
Return params_str(-1)
End Function
Dim params_dict As String
Public Function add_sign_to_params(params_dict As String, api As String, secret As String)
Dim params_dict As String
params_dict("api_key") = api
Dim params_str As String
params_str = json.dumps(params_dict, sort_keys = True, indent = 4)
params_str = params_dict_to_string(json.loads(params_str))
params_str += "&secret_key=" + secret
Dim hashlibmd5(params_strencode(Encoding, As Object))
Dim hash_md5 As Integer
hashlibmd5(params_strencode(Encoding = "utf-8"))
hash_md5 = hashlibmd5(params_strencode(Encoding))
Dim sign As Integer
sign = hash_md5.hexdigest().upper()
params_dict("sign") = sign
Return params_dict
End Function
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim params_url As String = "https://api.hotbit.io/api/v1/balance.history"
Dim params_str As String = "API endpoint".Replace(" ", "")
Dim params_dict As String = params_string_to_dict(params_str)
Dim params_dict As String = add_sign_to_params(params_dict, "Key", "Secret")
Dim params_str As String = params_dict_to_string(params_dict)
Console.WriteLine(params_url + "?" + params_str)
End Sub
End Class
But I got some errors because some of the things are quite difficult for me to convert into vb.net For example, how can I use params_XXX without declaring them? Why is it using the word Json while basically I need to pass a String to it?
I got roughly 25 errors and most of them are in the Public Function add_sign_to_params . It is really difficult for me to convert these functions into vb.net as they might have different construction in python.
I am not asking for a ready code, I'm asking help to convert it step by step.
For example, I've already searched on google how to convert a string to md5, but this code is not just converti a string to md5, it is using kind of weird and complex ( to me ) structures like "sort_keys = True, indent = 4)"..
Thanks