Class: GoogleAPI

Inherits:
Processor show all
Defined in:
lib/GoogleAPI.rb

Instance Attribute Summary collapse

Attributes inherited from Processor

#baseExecutePath, #config, #configFilePath

Instance Method Summary collapse

Methods inherited from Processor

#processReviews

Constructor Details

#initialize(keyFilePath, baseExecutePath, scopes = []) ⇒ GoogleAPI

Returns a new instance of GoogleAPI.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/GoogleAPI.rb', line 10

def initialize(keyFilePath, baseExecutePath, scopes = [])

    @logger = ZLogger.new(baseExecutePath)

    keyFileContent = JSON.parse(File.read(keyFilePath))

    @keyContent = Helper.unwrapRequiredParameter(keyFileContent,"private_key")
    @keyID = Helper.unwrapRequiredParameter(keyFileContent,"private_key_id")
    @clientEmail = Helper.unwrapRequiredParameter(keyFileContent,"client_email")
    @tokenURI = Helper.unwrapRequiredParameter(keyFileContent,"token_uri")

    @scopes = scopes

    @token = generateJWT()

    puts "[GoogleAPI] Init Success."
end

Instance Attribute Details

#clientEmailObject

Returns the value of attribute clientEmail.



8
9
10
# File 'lib/GoogleAPI.rb', line 8

def clientEmail
  @clientEmail
end

#keyContentObject

Returns the value of attribute keyContent.



8
9
10
# File 'lib/GoogleAPI.rb', line 8

def keyContent
  @keyContent
end

#keyIDObject

Returns the value of attribute keyID.



8
9
10
# File 'lib/GoogleAPI.rb', line 8

def keyID
  @keyID
end

#loggerObject

Returns the value of attribute logger.



8
9
10
# File 'lib/GoogleAPI.rb', line 8

def logger
  @logger
end

#scopesObject

Returns the value of attribute scopes.



8
9
10
# File 'lib/GoogleAPI.rb', line 8

def scopes
  @scopes
end

#tokenObject

Returns the value of attribute token.



8
9
10
# File 'lib/GoogleAPI.rb', line 8

def token
  @token
end

#tokenURIObject

Returns the value of attribute tokenURI.



8
9
10
# File 'lib/GoogleAPI.rb', line 8

def tokenURI
  @tokenURI
end

Instance Method Details

#request(url, method = "GET", data = nil, retryCount = 0) ⇒ Object



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
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/GoogleAPI.rb', line 28

def request(url, method = "GET", data = nil, retryCount = 0)
    uri = URI(url)
    https = Net::HTTP.new(uri.host, uri.port)
    https.use_ssl = true
    
    request = Net::HTTP::Get.new(uri)
    if method.upcase == "POST"
        request = Net::HTTP::Post.new(uri)
        if !data.nil?
            request['Content-Type'] = 'application/json'
            request.body = JSON.dump(data)
        end
    end
    
    request['Authorization'] = "Bearer #{token}";
    
    response = https.request(request).read_body
    result = JSON.parse(response)

    if !result["error"].nil?
        # Quota exceeded for quota metric 'Write requests' and limit 'Write requests per minute per user
        if result["error"]["code"] == 429
            puts "[GoogleAPI] Reached Rate Limited, sleep 30 secs..."
            sleep(30)
            return request(url, method, data, 0)
        else
            if retryCount >= 10
                raise "Could not connect to #{tokenURI}, key id: #{keyID}, error message: #{response}"
            else
                @token = generateJWT()
                message = "JWT Invalid, retry. (#{retryCount + 1})"
                logger.logWarn(message)
                puts "[GoogleAPI] #{message}"
                return request(url, method, data, retryCount + 1)
            end
        end
    else
        return result
    end

end