6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
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
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
|
# File 'lib/wrappix/templates/request.rb', line 6
def self.render(module_name, config)
oauth_token_logic = if config["auth_type"] == "oauth"
" def get_access_token\n # Try to get token from cache first\n token = \#{module_name}.cache.read(\"access_token\")\n return token if token\n\n # If not in cache, fetch new token\n response = Faraday.post(@config.token_url, {\n client_id: @config.client_id,\n client_secret: @config.client_secret,\n grant_type: \"client_credentials\"\n })\n\n if response.status == 200\n data = JSON.parse(response.body)\n token = data[\"access_token\"]\n expires_in = data[\"expires_in\"] || 3600\n\n # Cache the token\n \#{module_name}.cache.write(\"access_token\", token)\n\n # Cache expiration handling could be improved\n token\n else\n raise \#{module_name}::Error.new(\"Failed to obtain access token\", response.body, response.status)\n end\n end\n RUBY\n else\n \"\"\n end\n\n <<~RUBY\n # frozen_string_literal: true\n\n require \"faraday\"\n require \"json\"\n\n module \#{module_name}\n class Request\n def initialize(path, config = \#{module_name}.configuration)\n @path = path\n @config = config\n @base_url = config.base_url\n end\n\n def get(params: {}, headers: {})\n make_request(:get, params: params, headers: headers)\n end\n\n def post(body: {}, headers: {})\n make_request(:post, body: body, headers: headers)\n end\n\n def put(body: {}, headers: {})\n make_request(:put, body: body, headers: headers)\n end\n\n def patch(body: {}, headers: {})\n make_request(:patch, body: body, headers: headers)\n end\n\n def delete(params: {}, headers: {})\n make_request(:delete, params: params, headers: headers)\n end\n\n private\n\n def make_request(method, params: {}, body: nil, headers: {})\n response = connection.public_send(method) do |req|\n req.url @path\n req.params = params if params && !params.empty?\n req.body = body.to_json if body && !body.empty?\n req.headers.merge!(headers) if headers && !headers.empty?\n req.options.timeout = @config.timeout\n end\n\n handle_response(response)\n end\n\n def connection\n @connection ||= Faraday.new(url: @base_url) do |conn|\n conn.headers = @config.headers\n \#{connection_auth_config(config)}\n conn.response :json, content_type: /\\\\bjson$/\n conn.adapter Faraday.default_adapter\n end\n end\n\n def handle_response(response)\n return response.body if response.status.between?(200, 299)\n\n error_message = if response.body.is_a?(Hash)\n response.body[\"message\"] || response.body[\"error\"] || \"Error \\\#{response.status}\"\n else\n \"Error \\\#{response.status}\"\n end\n\n raise \#{module_name}::Error.new(error_message, response.body, response.status)\n end\n\n \#{oauth_token_logic}\n end\n end\n RUBY\nend\n"
|