Class: AdobeDocApi::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/adobe_doc_api/client.rb

Constant Summary collapse

JWT_URL =
"https://ims-na1.adobelogin.com/ims/exchange/jwt/".freeze
API_ENDPOINT_URL =
"https://cpf-ue1.adobe.io".freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(private_key: nil, client_id: nil, client_secret: nil, org_id: nil, tech_account_id: nil, access_token: nil) ⇒ Client

Returns a new instance of Client.



13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/adobe_doc_api/client.rb', line 13

def initialize(private_key: nil, client_id: nil, client_secret: nil, org_id: nil, tech_account_id: nil, access_token: nil)
  # TODO Need to validate if any params are missing and return error
  @client_id = client_id || AdobeDocApi.configuration.client_id
  @client_secret = client_secret || AdobeDocApi.configuration.client_secret
  @org_id = org_id || AdobeDocApi.configuration.org_id
  @tech_account_id =  || AdobeDocApi.configuration.
  @private_key_path = private_key || AdobeDocApi.configuration.private_key_path
  @location_url = nil
  @output_file_path = nil
  @raw_response = nil
  @access_token = access_token || get_access_token(@private_key_path)
end

Instance Attribute Details

#access_tokenObject (readonly)

Returns the value of attribute access_token.



11
12
13
# File 'lib/adobe_doc_api/client.rb', line 11

def access_token
  @access_token
end

#client_idObject (readonly)

Returns the value of attribute client_id.



11
12
13
# File 'lib/adobe_doc_api/client.rb', line 11

def client_id
  @client_id
end

#client_secretObject (readonly)

Returns the value of attribute client_secret.



11
12
13
# File 'lib/adobe_doc_api/client.rb', line 11

def client_secret
  @client_secret
end

#location_urlObject (readonly)

Returns the value of attribute location_url.



11
12
13
# File 'lib/adobe_doc_api/client.rb', line 11

def location_url
  @location_url
end

#org_idObject (readonly)

Returns the value of attribute org_id.



11
12
13
# File 'lib/adobe_doc_api/client.rb', line 11

def org_id
  @org_id
end

#raw_responseObject (readonly)

Returns the value of attribute raw_response.



11
12
13
# File 'lib/adobe_doc_api/client.rb', line 11

def raw_response
  @raw_response
end

#tech_account_idObject (readonly)

Returns the value of attribute tech_account_id.



11
12
13
# File 'lib/adobe_doc_api/client.rb', line 11

def 
  @tech_account_id
end

Instance Method Details

#get_access_token(private_key) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/adobe_doc_api/client.rb', line 26

def get_access_token(private_key)
  # TODO: JWT token deprecated and will stop working Jan 1, 2024.
  # jwt_payload = {
  #   "iss" => @org_id,
  #   "sub" => @tech_account_id,
  #   "https://ims-na1.adobelogin.com/s/ent_documentcloud_sdk" => true,
  #   "aud" => "https://ims-na1.adobelogin.com/c/#{@client_id}",
  #   "exp" => (Time.now.utc + 60).to_i
  # }
  #
  # rsa_private = OpenSSL::PKey::RSA.new File.read(private_key)
  #
  # jwt_token = JWT.encode jwt_payload, rsa_private, "RS256"
  #
  connection = Faraday.new do |conn|
    conn.response :json, content_type: "application/json"
  end
  # response = connection.post JWT_URL do |req|
  #   req.params["client_id"] = @client_id
  #   req.params["client_secret"] = @client_secret
  #   req.params["jwt_token"] = jwt_token
  # end
  scopes = "openid, DCAPI, AdobeID"

  response = connection.post "https://ims-na1.adobelogin.com/ims/token/v3" do |req|
    req.params["client_id"] = @client_id
    req.body = "client_secret=#{@client_secret}&grant_type=client_credentials&scope=#{scopes}"
  end
  return response.body["access_token"]
end

#get_asset_idObject



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/adobe_doc_api/client.rb', line 57

def get_asset_id
  # Create new asset ID to get upload pre-signed Uri
  connection = Faraday.new do |conn|
    conn.request :authorization, "Bearer", @access_token
    conn.headers["x-api-key"] = @client_id
    conn.headers["Content-Type"] = ""
    conn.response :json, content_type: "application/json"
  end
  response = connection.post "https://pdf-services.adobe.io/assets" do |req|
    req.body = {mediaType: "application/vnd.openxmlformats-officedocument.wordprocessingml.document"}.to_json
    req.headers["Content-Type"] = "application/json"
  end
  # Return pre-signed uploadUri and assedID
  return response.body["assetID"], response.body["uploadUri"]
end

#submit(json:, template:, output:) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/adobe_doc_api/client.rb', line 73

def submit(json:, template:, output:)
  asset_id, upload_uri = get_asset_id

  # Upload template to asset created earlier.
  response = Faraday.put upload_uri do |req|
    req.headers["Content-Type"] = "application/vnd.openxmlformats-officedocument.wordprocessingml.document"
    req.body = File.binread(template)
  end
  raise("Failed to upload template.") if response.status.to_i != 200

  # Post JSON Data for Merge to documentGeneration
  content_request = {
    assetID: asset_id,
    outputFormat: File.extname(output).delete("."),
    jsonDataForMerge: json
  }.to_json
  payload = content_request
  connection = Faraday.new do |conn|
    conn.request :authorization, "Bearer", @access_token
    conn.headers["x-api-key"] = @client_id
  end
  res = connection.post "https://pdf-services-ue1.adobe.io/operation/documentgeneration" do |req|
    req.body = payload
    req.headers["Content-Type"] = "application/json"
  end
  # TODO need to check status of response
  # Begin polling for status of file
  poll_for_file(res.headers["location"], output)
end