Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
A ruby script to convert an HTTP blueprint JSON file into multiple HAR files and then convert those HAR files in to code snippets using httpsnippet
#!/usr/bin/env ruby
# ./apib2httpsnippets
# Author: Max Novakovic
# Email: max@lateral.io
require 'awesome_print'
require 'json'
require 'fileutils'
require 'uri'
json = JSON.parse(File.read(ARGV[0]))
dir = File.expand_path(File.dirname(ARGV[0]))
api_name = File.basename(ARGV[0], ".json")
# Create directories for hars and snippets if doesn't exist
FileUtils.mkdir_p("hars")
FileUtils.mkdir_p("snippets")
# Simple slug function
def slug(str)
str.downcase.strip.gsub(' ', '-').gsub(/[^\w-]/, '')
end
# Get the first param value from the possible values
def first_param_val(param)
first = param['values'][0]
return '' unless first
first['value']
first['value'].to_i if param['type'] == 'integer'
end
# Get the API meta HOST
host = json['metadata'].find { |item| item['name'] == 'HOST' }['value']
# Step through each resourceGroup, resource and method
json['resourceGroups'].each do |rg|
ap rg['name']
rg['resources'].each do |r|
ap "-- #{r['name']}"
r['actions'].each do |action|
ap "---- #{action['name']}"
# Create the har object
har = {}
har['method'] = action['method']
har['url'] = host + r['uriTemplate']
# Need the Subscription-Key header for every request
har['headers'] = [{ name: 'Subscription-Key', value: 'YOUR_API_KEY' }]
# GET requests
if har['method'] == 'GET'
# Get the params and add them
action['parameters'].map do |param|
{ 'name' => param['name'], 'value' => first_param_val(param) }
end
end
# POST or PUT requests
if (har['method'] == 'POST' || har['method'] == 'PUT') && action['parameters'].count > 0
har['postData'] = {
'mimeType' => 'application/json',
# Create a JSON object from all the params
'text' => action['parameters'].each_with_object({}) do |param, hash|
hash[param['name']] = first_param_val(param)
end.to_json
}
# Use this if your API does not support application/json content type:
# har['postData'] = {
# 'text' => action['parameters'].map do |param|
# "#{param['name']}=#{first_param_val(param)}"
# end.join('&')
# }
end
# Write the HAR to file
name = "#{slug(rg['name'])}-#{slug(r['name'])}-#{slug(har['method'])}"
File.open("hars/#{name}.json", "w") do |f|
f.truncate(0)
f.write(har.to_json)
end
end
end
end
# There are more available! Modify as you see fit - http://git.io/vLwrr
targets = {
'shell' => ['curl'],
'go' => ['native'],
'httpie' => ['native'],
'java' => ['okhttp'],
'javascript' => ['jquery'],
'node' => ['request'],
'objc' => ['native'],
'ocaml' => ['native'],
'php' => ['native'],
'python' => ['native'],
'ruby' => ['native']
}
# Loop through each target and generate the snippets
targets.each do |lang, value|
value.each do |client|
print "Outputting #{lang} #{client}: "
ap `httpsnippet hars/*.json -t #{lang} -c #{client} -o snippets/#{lang}`
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.