prest 0.1.6 → 0.1.8
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile.lock +1 -1
- data/README.md +48 -3
- data/lib/prest/client.rb +31 -8
- data/lib/prest/version.rb +1 -1
- data/lib/prest.rb +12 -0
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: accb2149cb0dacc0ad3fee732fb7bff9fb552d1bbea197118b29525fed9e24fe
|
4
|
+
data.tar.gz: b6cf166232aca1d97372acc0efdb8b8e952d4025159a638d55f779ea608e2276
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a313c848c5bd26fd4210cdccfc0e6f79f07927dc59cca7417966ccac53c518b61f0d80c403cc6371c99c9453e21f8b08d1696af2774bbaba91945a7826c6ce48
|
7
|
+
data.tar.gz: 16364768bf1190a5bb884c44379873ef37841ae610d9d832b7fdc9742a13ca79601fd9442513342c8ceca4a92bfa1df27a61409488f3c6cdfe16a683a2815279
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -54,6 +54,48 @@ Prest::Client.new('https://example.com/api', { headers: { 'Authorization' => 'Be
|
|
54
54
|
.post(body: { username: 'juan-apa' })
|
55
55
|
```
|
56
56
|
|
57
|
+
### Using raw/custom/special query parameters
|
58
|
+
|
59
|
+
In ruby, duplicate keyword arguments on method calls are not accepted, so you **can not** do the following:
|
60
|
+
|
61
|
+
```ruby
|
62
|
+
Prest::Client.new('https://example.com/api').example(key: 1, key: 2).get
|
63
|
+
# `key: 1` is overriden by `key: 2`;
|
64
|
+
# produces: GET https://example.com/api/one/two?key=2
|
65
|
+
```
|
66
|
+
|
67
|
+
Because of this and other cases where formatting is very strict/unusual, you can pass a string which will not be formatted to the query parameters. To do this, use the following:
|
68
|
+
|
69
|
+
```ruby
|
70
|
+
# GET https://example.com/api/example?key=1&key2&other=value
|
71
|
+
Prest::Client.new('https://example.com/api')
|
72
|
+
.example(__query_params: 'key=1&key=2', other: 'value')
|
73
|
+
.get
|
74
|
+
```
|
75
|
+
|
76
|
+
The string passed to the keyword argument `__query_params` will not be formatted, and passed as is.
|
77
|
+
|
78
|
+
> **Warning**
|
79
|
+
> `__query_params` is the only keyword argument that can be repeated across method calls:
|
80
|
+
|
81
|
+
```ruby
|
82
|
+
Prest::Client.new('https://example.com/api')
|
83
|
+
.one(key: '1')
|
84
|
+
.two(key: '2')
|
85
|
+
.get
|
86
|
+
# Produces: GET https://example.com/api/one/two?key=2
|
87
|
+
```
|
88
|
+
|
89
|
+
However using `__query_params`:
|
90
|
+
|
91
|
+
```ruby
|
92
|
+
Prest::Client.new('https://example.com/api')
|
93
|
+
.one(__query_params: 'key=1')
|
94
|
+
.two(__query_params: 'key=2')
|
95
|
+
.get
|
96
|
+
#Produces: GET https://example.com/api/one/two?key=1&key=2
|
97
|
+
```
|
98
|
+
|
57
99
|
### Automatically adding the json headers
|
58
100
|
|
59
101
|
Because some API's need a `Content-Type: application/json` and/or `Accept: application/json` headers, there's a built in option that can be passed to the client to add those for you:
|
@@ -70,18 +112,21 @@ Note: The option will merge any other header you pass to the initializer.
|
|
70
112
|
### Raising exceptions on failed HTTP requests
|
71
113
|
|
72
114
|
An HTTP request is considered as failed when the status code is not between `100` and `299`.
|
73
|
-
To automatically raise a `Prest::
|
115
|
+
To automatically raise a `Prest::RequestError` when the HTTP request is not successful, use the bang methods (`get!`, `post!`, `put!`, `patch!` and `delete!`).
|
74
116
|
|
75
117
|
```ruby
|
76
118
|
# If for example the authorization headers are invalid, it will return an 401 status code.
|
77
|
-
# This call will raise a ::Prest::
|
119
|
+
# This call will raise a ::Prest::RequestError with the response as a json in the message.
|
78
120
|
|
79
121
|
begin
|
80
122
|
Prest::Client.new('https://example.com/api', { headers: { 'Authorization' => 'Bearer Token xxxyyyzzz' } })
|
81
123
|
.users
|
82
124
|
.get!
|
83
|
-
rescue Prest::
|
125
|
+
rescue Prest::RequestError => e
|
84
126
|
puts e.message # "{ error: \"Invalid auth credentials\" }"
|
127
|
+
puts e.status # 403
|
128
|
+
puts e.body # "{ error: \"Invalid auth credentials\" }"
|
129
|
+
puts e.headers # { 'ContentType' => 'application/json' }
|
85
130
|
end
|
86
131
|
```
|
87
132
|
|
data/lib/prest/client.rb
CHANGED
@@ -36,12 +36,19 @@ module Prest
|
|
36
36
|
res = ::HTTParty.send(http_method, build_url, headers: headers, body: body)
|
37
37
|
::Prest::Response.new(res.code, res.parsed_response, res.headers)
|
38
38
|
rescue ::HTTParty::ResponseError => e
|
39
|
-
::Kernel.raise
|
39
|
+
::Kernel.raise(::Prest::RequestError.new(status: res.status,
|
40
|
+
body: res.parsed_response,
|
41
|
+
headers: res.headers), e.message)
|
40
42
|
end
|
41
43
|
|
42
44
|
def execute_query!(*args, **kwargs)
|
43
45
|
res = execute_query(*args, **kwargs)
|
44
|
-
|
46
|
+
|
47
|
+
unless res.successful?
|
48
|
+
::Kernel.raise(::Prest::RequestError.new(status: res.status,
|
49
|
+
body: res.body.to_json,
|
50
|
+
headers: res.headers), res.body.to_json)
|
51
|
+
end
|
45
52
|
|
46
53
|
res
|
47
54
|
end
|
@@ -49,7 +56,10 @@ module Prest
|
|
49
56
|
def chain_fragment(fragment_name, *args, **kwargs)
|
50
57
|
arguments = args.join('/')
|
51
58
|
parsed_args = arguments.empty? ? '' : "/#{arguments}"
|
52
|
-
|
59
|
+
extract_raw_query_params!(kwargs)
|
60
|
+
dup_kwargs = kwargs.dup
|
61
|
+
dup_kwargs.delete(:__query_params)
|
62
|
+
@query_params.merge!(dup_kwargs)
|
53
63
|
@fragments << "#{fragment_name.gsub("__", "-")}#{parsed_args}"
|
54
64
|
end
|
55
65
|
|
@@ -62,13 +72,26 @@ module Prest
|
|
62
72
|
def build_url
|
63
73
|
path = @fragments.join('/')
|
64
74
|
|
65
|
-
stringified_params =
|
66
|
-
|
67
|
-
|
68
|
-
end
|
75
|
+
stringified_params = @query_params.to_a.map do |key_val|
|
76
|
+
key_val[0] == :__query_params ? key_val[1].join('&') : "#{key_val[0]}=#{key_val[1]}"
|
77
|
+
end.join('&')
|
69
78
|
|
70
|
-
stringified_params = stringified_params.empty? ? '' : "?#{stringified_params
|
79
|
+
stringified_params = stringified_params.empty? ? '' : "?#{stringified_params}"
|
71
80
|
"#{@base_uri}/#{path}#{stringified_params}"
|
72
81
|
end
|
82
|
+
|
83
|
+
def array_wrap(obj)
|
84
|
+
obj.is_a?(::Array) ? obj : [obj]
|
85
|
+
end
|
86
|
+
|
87
|
+
def extract_raw_query_params!(kwargs)
|
88
|
+
return unless kwargs.key?(:__query_params)
|
89
|
+
|
90
|
+
@query_params[:__query_params] = if @query_params[:__query_params].is_a?(::Array)
|
91
|
+
@query_params[:__query_params] + array_wrap(kwargs[:__query_params])
|
92
|
+
else
|
93
|
+
array_wrap(kwargs[:__query_params])
|
94
|
+
end
|
95
|
+
end
|
73
96
|
end
|
74
97
|
end
|
data/lib/prest/version.rb
CHANGED
data/lib/prest.rb
CHANGED
@@ -8,4 +8,16 @@ require_relative 'prest/response'
|
|
8
8
|
|
9
9
|
module Prest
|
10
10
|
class Error < StandardError; end
|
11
|
+
|
12
|
+
# Error for when a request is unsuccessful.
|
13
|
+
class RequestError < StandardError
|
14
|
+
attr_reader :status, :body, :headers
|
15
|
+
|
16
|
+
def initialize(status:, body: '', headers: {})
|
17
|
+
super()
|
18
|
+
@status = status
|
19
|
+
@body = body
|
20
|
+
@headers = headers
|
21
|
+
end
|
22
|
+
end
|
11
23
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: prest
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.8
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Juan Aparicio
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2023-04-05 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: httparty
|