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
# File 'lib/adobe_doc_api/client.rb', line 26

def get_access_token(private_key)
  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

  return response.body["access_token"]
end

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

Raises:



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/adobe_doc_api/client.rb', line 51

def submit(json:, template:, output:)
  @output = output
  output_format = /docx/.match?(File.extname(@output)) ? "application/vnd.openxmlformats-officedocument.wordprocessingml.document" : "application/pdf"

  content_request = {
    "cpf:engine": {
      "repo:assetId": "urn:aaid:cpf:Service-52d5db6097ed436ebb96f13a4c7bf8fb"
    },
    "cpf:inputs": {
      documentIn: {
        "dc:format": "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
        "cpf:location": "InputFile0"
      },
      params: {
        "cpf:inline": {
          outputFormat: File.extname(@output).delete("."),
          jsonDataForMerge: json
        }
      }
    },
    "cpf:outputs": {
      documentOut: {
        "dc:format": output_format.to_s,
        "cpf:location": "multipartLabel"
      }
    }
  }.to_json

  connection = Faraday.new API_ENDPOINT_URL do |conn|
    conn.request :authorization, "Bearer", @access_token
    conn.headers["x-api-key"] = @client_id
    conn.request :multipart
    conn.request :url_encoded
    conn.response :json, content_type: "application/json"
  end

  payload = {"contentAnalyzerRequests" => content_request}
  payload[:InputFile0] = Faraday::FilePart.new(template, "application/vnd.openxmlformats-officedocument.wordprocessingml.document")
  res = connection.post("/ops/:create", payload)
  status_code = res.body["cpf:status"]["status"].to_i
  @location_url = res.headers["location"]
  raise Error.new(status_code: status_code, msg: res.body["cpf:status"]) unless status_code == 202
  poll_for_file(@location_url)
end