aga-request 0.0.2 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +67 -8
- data/lib/request/base.rb +0 -2
- data/lib/request/resources/auth.rb +52 -0
- data/lib/request/resources/client.rb +67 -0
- data/lib/request/resources/error.rb +13 -0
- data/lib/request/resources/response.rb +27 -0
- data/lib/request/version.rb +1 -1
- metadata +7 -4
- data/lib/request/resources/http.rb +0 -55
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: c6e6796f389f50f10fc0f901b1d656cf8d552849ea9a97ae0de64b9230c03cf4
|
4
|
+
data.tar.gz: 99f2fe222281866fd0c1d2d5d96fa3df67055d9386e1872d6954c7a3e01c5f39
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 7d40c5dd68cf777893fb81067568a77154ae5581169801c4ca5122f149cf8b3e3be617c2208d53eb951c11d06f8bad87c4b73eca6989d31af80e0e7207e04864
|
7
|
+
data.tar.gz: bdba30dbec6dd437e9c56693a86bf6bf41ff20c219dc5c428a9ce6e4a7e47230cab83b55e4c4a50a117275b7d1b79a4945a4f1fa0dab0e8448088821ef5ad3dc
|
data/README.md
CHANGED
@@ -1,8 +1,13 @@
|
|
1
|
-
# Aga
|
1
|
+
# Aga-request
|
2
2
|
|
3
|
-
|
3
|
+
Aga request is a Ruby gem that provides a convenient way to make HTTP requests, including passing parameters, headers, and more. It simplifies the process of interacting with APIs and web services and handles the complexities of handling HTTP requests and responses.
|
4
4
|
|
5
|
-
|
5
|
+
## Features
|
6
|
+
|
7
|
+
- Make GET, POST, PUT, DELETE, and other HTTP requests easily.
|
8
|
+
- Pass parameters and headers with the requests.
|
9
|
+
- Handle response parsing automatically, returning OpenStruct responses.
|
10
|
+
- Get access to the response status, headers, and body.
|
6
11
|
|
7
12
|
## Installation
|
8
13
|
|
@@ -16,14 +21,68 @@ If bundler is not being used to manage dependencies, install the gem by executin
|
|
16
21
|
|
17
22
|
## Usage
|
18
23
|
|
19
|
-
|
24
|
+
To use Aga-request in your application, require the gem and start making requests. Here's a basic example:
|
25
|
+
|
26
|
+
```
|
27
|
+
require 'request'
|
28
|
+
require 'ostruct'
|
29
|
+
|
30
|
+
options = OpenStruct.new(
|
31
|
+
http_method: 'get', // get, post, put, patch, delete
|
32
|
+
url: 'https://api.example.com/resource',
|
33
|
+
headers: {}, // All headers you want, without authentication headers
|
34
|
+
data: {}, // Body params
|
35
|
+
params: nil, // Hash if you want to pass any query params
|
36
|
+
auth: (*),
|
37
|
+
proxy: nil,
|
38
|
+
options: {}
|
39
|
+
)
|
40
|
+
|
41
|
+
response = Request::Client.request(options)
|
42
|
+
|
43
|
+
puts response # Output: #<Request::Response:0x00007fdac286dd38 @status=200, @body=#`<OpenStruct>`>
|
44
|
+
puts response.status # Output: 200
|
20
45
|
|
21
|
-
|
46
|
+
puts response.body # Output: #<OpenStruct userId=1, id=1, title="sunt aut facere repellat provident occaecati excepturi optio reprehenderit", body="quia et suscipit\nsuscipit recusandae consequuntur expedita et cum\nreprehenderit molestiae ut ut quas totam\nnostrum rerum est autem sunt rem eveniet architecto">
|
22
47
|
|
23
|
-
|
48
|
+
OpenStruct of auth possible (*):
|
24
49
|
|
25
|
-
|
50
|
+
Basic: OpenStruct.new(type::basic, user:'username', pass:'password')
|
51
|
+
Bearer: OpenStruct.new(type::bearer, token:'token_value')
|
52
|
+
Token: OpenStruct.new(type::token, token:'token_value')
|
53
|
+
|
54
|
+
```
|
26
55
|
|
27
56
|
## Contributing
|
28
57
|
|
29
|
-
|
58
|
+
Contributions to Aga-request are welcome! If you encounter any issues or have suggestions for improvements, please submit an issue on the repository. Pull requests are also encouraged.
|
59
|
+
|
60
|
+
## Private License
|
61
|
+
|
62
|
+
Version 0.0.2, 01/06/2023
|
63
|
+
|
64
|
+
By obtaining a copy of this software and associated documentation files (the "Software"), you agree that the Software is proprietary to AIGreenAnt and is protected under intellectual property laws. This license grants you limited rights to use the Software solely for internal purposes within your organization.
|
65
|
+
|
66
|
+
1. License Grant
|
67
|
+
|
68
|
+
1.1 You are granted a non-exclusive, non-transferable license to use the Software for internal purposes only.
|
69
|
+
|
70
|
+
1.2 You may not distribute, sublicense, sell, or transfer the Software to any third party without prior written permission from AIGreenAnt.
|
71
|
+
2. Intellectual Property
|
72
|
+
|
73
|
+
2.1 The Software and any associated intellectual property rights, including but not limited to copyrights, patents, trademarks, trade secrets, and any other proprietary rights, are and shall remain the exclusive property of AIGreenAnt.
|
74
|
+
|
75
|
+
2.2 You may not remove, alter, or obscure any proprietary notices or labels on the Software.
|
76
|
+
3. Limitation of Liability
|
77
|
+
|
78
|
+
3.1 In no event shall AIGreenAnt be liable for any direct, indirect, incidental, special, or consequential damages arising out of the use or inability to use the Software, even if AIGreenAnt has been advised of the possibility of such damages.
|
79
|
+
4. Termination
|
80
|
+
|
81
|
+
4.1 This license is effective until terminated. AIGreenAnt may terminate this license at any time if you fail to comply with the terms and conditions of this agreement.
|
82
|
+
|
83
|
+
4.2 Upon termination, you must immediately cease all use of the Software and destroy all copies in your possession or control.
|
84
|
+
5. Governing Law
|
85
|
+
|
86
|
+
5.1 This license shall be governed by and construed in accordance with the laws of Italy, without regard to its conflict of laws principles.
|
87
|
+
|
88
|
+
By using the Software, you acknowledge that you have read and understood this license agreement and agree to be bound by its terms and conditions.
|
data/lib/request/base.rb
CHANGED
@@ -0,0 +1,52 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Request
|
4
|
+
module Auth
|
5
|
+
class Authentication
|
6
|
+
AUTH_CLASSES = {
|
7
|
+
basic: 'Basic',
|
8
|
+
bearer: 'Bearer',
|
9
|
+
token: 'Token'
|
10
|
+
}.freeze
|
11
|
+
|
12
|
+
def self.apply(headers, *args)
|
13
|
+
auth_class = AUTH_CLASSES[args.first.type]
|
14
|
+
raise ArgumentError, "Type of authentication: #{args.first.type} doesn't support" unless auth_class
|
15
|
+
|
16
|
+
auth_instance = Object.const_get("Request::Auth::#{auth_class}").new(headers, *args)
|
17
|
+
auth_instance.apply
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
class BaseAuth
|
22
|
+
def initialize(headers, args)
|
23
|
+
@headers = headers
|
24
|
+
@token = args.token
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
class Basic
|
29
|
+
def initialize(headers, args)
|
30
|
+
@headers = headers
|
31
|
+
@user = args.user
|
32
|
+
@pass = args.password
|
33
|
+
end
|
34
|
+
|
35
|
+
def apply
|
36
|
+
@headers['Authorization'] = 'Basic ' + ["#{@user}:#{@pass}"].pack('m0')
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
class Bearer < BaseAuth
|
41
|
+
def apply
|
42
|
+
@headers['Authorization'] = "Bearer #{@token}"
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
class Token < BaseAuth
|
47
|
+
def apply
|
48
|
+
@headers['Authorization'] = "Token #{@token}"
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
@@ -0,0 +1,67 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Request
|
4
|
+
class Client < Base
|
5
|
+
class << self
|
6
|
+
def request(args)
|
7
|
+
new.request(args)
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
def request(args) # rubocop: disable Metrics/AbcSize, Style/CommentedKeyword
|
12
|
+
uri = URI.parse(args.url)
|
13
|
+
uri.query = encode_www_form(args.params) if args.params
|
14
|
+
|
15
|
+
body = process_params(headers: args.headers, data: args.data) if args.data
|
16
|
+
Request::Auth::Authentication.apply(args.headers, args.auth) if args.auth
|
17
|
+
|
18
|
+
response = send_request(uri, args, body)
|
19
|
+
response.is_a?(Net::HTTPSuccess) ? build_response(response) : raise_error(response)
|
20
|
+
end
|
21
|
+
|
22
|
+
private
|
23
|
+
|
24
|
+
def build_response(response)
|
25
|
+
Response.new(response.code, response.body)
|
26
|
+
end
|
27
|
+
|
28
|
+
def encode_www_form(params)
|
29
|
+
URI.encode_www_form(params)
|
30
|
+
end
|
31
|
+
|
32
|
+
def opts(uri, options)
|
33
|
+
return unless uri.scheme == 'https'
|
34
|
+
|
35
|
+
{
|
36
|
+
use_ssl: true,
|
37
|
+
verify_mode: OpenSSL::SSL::VERIFY_PEER,
|
38
|
+
read_timeout: options[:read_timeout],
|
39
|
+
open_timeout: options[:open_timeout]
|
40
|
+
}
|
41
|
+
end
|
42
|
+
|
43
|
+
def process_params(headers: nil, data: nil)
|
44
|
+
if !data.is_a?(Enumerable)
|
45
|
+
data
|
46
|
+
else
|
47
|
+
headers['content-type'] = 'application/x-www-form-urlencoded'
|
48
|
+
encode_www_form(data)
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
def proxy(args)
|
53
|
+
proxy = args.proxy.to_h
|
54
|
+
[proxy[:host], proxy[:port], proxy[:user], proxy[:password]]
|
55
|
+
end
|
56
|
+
|
57
|
+
def raise_error(response)
|
58
|
+
raise Error, response
|
59
|
+
end
|
60
|
+
|
61
|
+
def send_request(uri, args, body)
|
62
|
+
Net::HTTP.start(uri.host, uri.port, *proxy(args), opts(uri, args.options)) do |http|
|
63
|
+
http.send_request(args.http_method.to_s.upcase, uri, body, args.headers)
|
64
|
+
end
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Request
|
4
|
+
class Response
|
5
|
+
attr_reader :status, :body
|
6
|
+
|
7
|
+
def initialize(status, body)
|
8
|
+
@status = Integer(status)
|
9
|
+
@body = body
|
10
|
+
@body = struct(json) unless @body.nil? || @body.empty?
|
11
|
+
end
|
12
|
+
|
13
|
+
def json
|
14
|
+
JSON.parse(@body)
|
15
|
+
end
|
16
|
+
|
17
|
+
def struct(value = json)
|
18
|
+
if value.is_a?(Hash)
|
19
|
+
OpenStruct.new(value.transform_values { |v| struct(v) })
|
20
|
+
elsif value.is_a?(Array)
|
21
|
+
value.map { |v| struct(v) }
|
22
|
+
else
|
23
|
+
value
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
data/lib/request/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: aga-request
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Stefano Baldazzi
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2023-
|
11
|
+
date: 2023-08-25 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: zeitwerk
|
@@ -176,7 +176,10 @@ files:
|
|
176
176
|
- lib/request.rb
|
177
177
|
- lib/request/autoloader.rb
|
178
178
|
- lib/request/base.rb
|
179
|
-
- lib/request/resources/
|
179
|
+
- lib/request/resources/auth.rb
|
180
|
+
- lib/request/resources/client.rb
|
181
|
+
- lib/request/resources/error.rb
|
182
|
+
- lib/request/resources/response.rb
|
180
183
|
- lib/request/version.rb
|
181
184
|
homepage: https://github.com/Baldaz02/aga-request-ruby
|
182
185
|
licenses:
|
@@ -200,5 +203,5 @@ requirements: []
|
|
200
203
|
rubygems_version: 3.3.24
|
201
204
|
signing_key:
|
202
205
|
specification_version: 4
|
203
|
-
summary: request0.0.
|
206
|
+
summary: request0.0.3
|
204
207
|
test_files: []
|
@@ -1,55 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
module Request
|
4
|
-
class Http < Base
|
5
|
-
def initialize(api_key: nil, api_secret: nil, cache_options: {})
|
6
|
-
super()
|
7
|
-
@api_key = api_key
|
8
|
-
@api_secret = api_secret
|
9
|
-
@cache = ActiveSupport::Cache.lookup_store(:memory_store, cache_options || default_cache)
|
10
|
-
end
|
11
|
-
|
12
|
-
%i[get post put delete].each do |method|
|
13
|
-
define_method(method) do |endpoint, params = {}, headers = {}|
|
14
|
-
cached_request(method, endpoint, params, headers)
|
15
|
-
end
|
16
|
-
end
|
17
|
-
|
18
|
-
private
|
19
|
-
|
20
|
-
def cached_request(method, endpoint, params = {}, headers = {})
|
21
|
-
cache_key = [method, endpoint, params, headers]
|
22
|
-
unless @cache.exist?(cache_key)
|
23
|
-
response = make_request(method, endpoint, params, headers)
|
24
|
-
@cache.write(cache_key, data(response))
|
25
|
-
end
|
26
|
-
|
27
|
-
@cache.read(cache_key)
|
28
|
-
end
|
29
|
-
|
30
|
-
def data(response)
|
31
|
-
{ response: JSON.parse(response.body), status: response.success? }
|
32
|
-
end
|
33
|
-
|
34
|
-
def default_cache
|
35
|
-
{ size: 64.megabytes, expires_in: 1.minute }
|
36
|
-
end
|
37
|
-
|
38
|
-
def make_request(method, endpoint, params, headers)
|
39
|
-
connection = prepare_connection(method, endpoint, params, headers)
|
40
|
-
|
41
|
-
connection.public_send(method) do |request|
|
42
|
-
request.headers.merge!(headers)
|
43
|
-
request.url(endpoint)
|
44
|
-
request.body = params.to_json if %i[post put].include?(method)
|
45
|
-
end
|
46
|
-
end
|
47
|
-
|
48
|
-
def prepare_connection(_method, endpoint, _params, _headers)
|
49
|
-
Faraday.new(endpoint) do |conn|
|
50
|
-
conn.request :url_encoded
|
51
|
-
conn.adapter Faraday.default_adapter
|
52
|
-
end
|
53
|
-
end
|
54
|
-
end
|
55
|
-
end
|