Skip to content

Instantly share code, notes, and snippets.

@kubicek
Last active November 12, 2019 18:25
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kubicek/758e4aea7bbd9e724feb651de8d6a81c to your computer and use it in GitHub Desktop.
Save kubicek/758e4aea7bbd9e724feb651de8d6a81c to your computer and use it in GitHub Desktop.
VSHosting invoice downloader
require 'net/https'
require 'json'
auth={
email: 'mujmail',
password: 'mojeheslo'
}
http = Net::HTTP.new('admin.vshosting.cloud', 443)
http.use_ssl = true
login_response = http.post('/api/public/auth/login', auth.to_json)
login_data = JSON.parse(login_response.body, symbolize_names: true)
auth_header = { 'Authorization'=> "Bearer #{login_data[:auth][:accessToken]}" }
login_data[:user][:clientUserList].each{ |client|
puts client[:client][:companyName]
client_id=client[:clientId]
invoice_response = http.get("/api/public/client/#{client_id}/invoice", auth_header)
invoices = JSON.parse(invoice_response.body, symbolize_names: true)
File.open("vs_invoices.json", "w"){|f| f<<JSON.pretty_generate(invoices)}
invoices[:invoiceList].each{|invoice|
puts invoice[:number]
create_pdf_response = http.get("/api/public/invoice/#{invoice[:id]}/create-pdf", auth_header)
token = JSON.parse(create_pdf_response.body)['token']
invoice_pdf_response = http.get("/api/public/file/#{token}")
File.open("vs_#{invoice[:number]}.pdf","w"){|f| f << invoice_pdf_response.body}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment