modify Lib Datagouv API

This commit is contained in:
Kara Diaby 2022-07-25 09:15:57 +02:00
parent 181e237cf0
commit d18d89b41b

View file

@ -11,10 +11,9 @@ class APIDatagouv::API
end end
class << self class << self
def upload(path) def upload(io, dataset, resource = nil)
io = File.new(path, 'r')
response = Typhoeus.post( response = Typhoeus.post(
datagouv_upload_url, datagouv_upload_url(dataset, resource),
body: { body: {
file: io file: io
}, },
@ -25,19 +24,27 @@ class APIDatagouv::API
if response.success? if response.success?
response.body response.body
else else
raise RequestFailed.new(datagouv_upload_url, response) raise RequestFailed.new(datagouv_upload_url(dataset, resource), response)
end end
end end
private private
def datagouv_upload_url def datagouv_upload_url(dataset, resource = nil)
[ if resource.present?
datagouv_secret[:api_url], [
"/datasets/", datagouv_secret[:descriptif_demarches_dataset], datagouv_secret[:api_url],
"/resources/", datagouv_secret[:descriptif_demarches_resource], "/datasets/", datagouv_secret[dataset],
"/upload/" "/resources/", datagouv_secret[resource],
].join "/upload/"
].join
else
[
datagouv_secret[:api_url],
"/datasets/", datagouv_secret[dataset],
"/upload/"
].join
end
end end
def datagouv_secret def datagouv_secret