Skip to content

Commit

Permalink
📝 add default async sample code (#79)
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastianMindee authored Feb 2, 2024
1 parent 1e14bf2 commit d2b8695
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 0 deletions.
72 changes: 72 additions & 0 deletions docs/code_samples/default_async.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
require 'json'
require 'net/http'
require 'uri'
require 'time'

api_key = "my-api-key"
account = "my-account"
endpoint = "my-endpoint"
version = "my-version"

url_enqueue = "https://api.mindee.net/v1/products/#{account}/#{endpoint}/v#{version}/predict_async"
headers = { "Authorization" => "Token #{api_key}" }

file = "/path/to/the/file.ext"

uri = URI.parse(url_enqueue)
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true

boundary = "----WebKitFormBoundary#{rand(36**32).to_s(36)}"
headers['Content-Type'] = "multipart/form-data; boundary=#{boundary}"

request = Net::HTTP::Post.new(uri.path, headers)
request.set_form([['document', File.open(file)]], 'multipart/form-data')
response_enqueue = http.request(request)

json_response_enqueue = JSON.parse(response_enqueue.body)

unless response_enqueue.is_a?(Net::HTTPSuccess)
raise "Error: #{json_response_enqueue['api_request']['error']}"
end

job_id = json_response_enqueue["job"]["id"]

url_parse_queued = "https://api.mindee.net/v1/products/#{account}/#{endpoint}/v#{version}/documents/queue/#{job_id}"
sleep(4)

tries = 1
while tries < 30
url = URI.parse(url_parse_queued)
request_parse = Net::HTTP::Get.new(url.path, 'Authorization' => "Token #{api_key}")
response_parse = http.request(request_parse)
if response_parse.code.to_i >= 300 && response_parse.code.to_i <= 399

redirection_parse = Net::HTTP::Get.new(response_parse['location'], 'Authorization' => "Token #{api_key}")
response_parse = http.request(redirection_parse)
json_response_parse = JSON.parse(response_parse.body)
break
else
json_response_parse = JSON.parse(response_parse.body)

unless response_parse.is_a?(Net::HTTPSuccess)
puts json_response_parse
raise "Error: #{json_response_parse['api_request']['error']}"
end

if json_response_parse["job"]["status"] == "completed"
break
else
puts json_response_parse["job"]
end

tries += 1
sleep(2)
end
end

unless json_response_parse["job"]["status"] == "completed"
raise "Async parsing timed out after #{tries} tries"
end

puts JSON.pretty_generate(json_response_parse["document"])
8 changes: 8 additions & 0 deletions spec/test_code_samples.sh
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,13 @@ do
sed -i "s/my-endpoint/$ENDPOINT/g" $OUTPUT_FILE
sed -i "s/my-version/1/g" $OUTPUT_FILE
fi

if echo "${f}" | grep -q "default_async.txt"
then
sed -i "s/my-account/mindee/" $OUTPUT_FILE
sed -i "s/my-endpoint/invoice_splitter/" $OUTPUT_FILE
sed -i "s/my-version/1/" $OUTPUT_FILE
fi

bundle exec ruby $OUTPUT_FILE
done

0 comments on commit d2b8695

Please sign in to comment.