Last active
November 12, 2019 18:25
-
-
Save kubicek/758e4aea7bbd9e724feb651de8d6a81c to your computer and use it in GitHub Desktop.
VSHosting invoice downloader
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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