Skip to content

Instantly share code, notes, and snippets.

@denro
Created December 6, 2014 16:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save denro/c64f55078fea40f995fc to your computer and use it in GitHub Desktop.
Save denro/c64f55078fea40f995fc to your computer and use it in GitHub Desktop.
Nordnet API test
require 'openssl'
require 'base64'
require 'uri'
require 'json'
require 'net/http'
require 'byebug'
username = 'username'
password = 'password'
service = 'NEXTAPI'
# Create auth
string = Base64.encode64(username) + ':' + Base64.encode64(password) + ':' + Base64.encode64((Time.now.to_i * 1000).to_s)
public_key_data = File.read('./NEXTAPI.pem')
public_key = OpenSSL::PKey::RSA.new(public_key_data)
auth = URI::escape(Base64.encode64(public_key.public_encrypt(string)),
Regexp.new("[^#{URI::PATTERN::UNRESERVED}]"))
# Setup HTTPS
http = Net::HTTP.new('api.test.nordnet.se', 443)
http.use_ssl = true
# Get status of server
puts 'System:'
response = http.get('/next/2/', {'Accept' => 'application/json'})
puts response.body
# POST login
puts 'Login:'
login_resp = http.post('/next/2/login', "auth=#{auth}&service=#{service}", {'Accept' => 'application/json'})
puts login_resp.body
json = JSON.parse(login_resp.body)
session_key = json['session_key']
puts session_key
puts 'Accounts:'
acc_request = Net::HTTP::Get.new('/next/2/accounts', {'Accept' => 'application/json' })
acc_request.basic_auth(session_key, session_key)
account_resp = http.request(acc_request)
puts account_resp.body
acc_nr = JSON.parse(account_resp.body)[0]['accno']
puts acc_nr
puts 'Positions:'
positions_request = Net::HTTP::Get.new("/next/2/accounts/#{acc_nr}/positions", {'Accept' => 'application/json' })
positions_request.basic_auth(session_key, session_key)
positions_resp = http.request(positions_request)
puts positions_resp.body
puts 'Orders:'
order_list_request = Net::HTTP::Get.new("/next/2/accounts/#{acc_nr}/orders", {'Accept' => 'application/json' })
order_list_request.basic_auth(session_key, session_key)
order_list_resp = http.request(order_list_request)
puts order_list_resp.body
puts 'Socket test'
sock = TCPSocket.new('priv.api.test.nordnet.se', 443)
ctx = OpenSSL::SSL::SSLContext.new
@socket = OpenSSL::SSL::SSLSocket.new(sock, ctx).tap do |socket|
socket.sync_close = true
socket.connect
end
@socket.puts %{{"cmd":"login", "args":{"session_key":"#{session_key}", "service":"NEXTAPI"}}}
#{"cmd":"subscribe", "args":{"t":"price", "i":"1869", "m":30}}
subscribe_params = { "cmd" => "subscribe", "args" => { "t" => "order", "i" => "101", "m" => 11 } }
@socket.puts JSON.generate(subscribe_params)
puts 'Create Order:'
orders_params = { "identifier" => "101", "market_id" => 11, "volume" => 100, "side" => "BUY", "price" => 73.15, "currency" => "SEK" }
orders_request = Net::HTTP::Post.new("/next/2/accounts/#{acc_nr}/orders", {'Accept' => 'application/json' })
orders_request.basic_auth(session_key, session_key)
orders_request.set_form_data(orders_params)
orders_resp = http.request(orders_request)
puts orders_resp.body
puts 'Order data from feed:'
while line = @socket.gets
puts line
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment