reso_transport 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +15 -0
- data/.travis.yml +7 -0
- data/CODE_OF_CONDUCT.md +74 -0
- data/Gemfile +6 -0
- data/Gemfile.lock +33 -0
- data/LICENSE.txt +21 -0
- data/README.md +193 -0
- data/Rakefile +10 -0
- data/bin/console +30 -0
- data/bin/setup +8 -0
- data/lib/reso_transport/authentication/access.rb +23 -0
- data/lib/reso_transport/authentication/auth_strategy.rb +26 -0
- data/lib/reso_transport/authentication/fetch_token_auth.rb +49 -0
- data/lib/reso_transport/authentication/middleware.rb +46 -0
- data/lib/reso_transport/authentication/static_token_auth.rb +24 -0
- data/lib/reso_transport/authentication.rb +5 -0
- data/lib/reso_transport/client.rb +53 -0
- data/lib/reso_transport/configuration.rb +6 -0
- data/lib/reso_transport/entity_set.rb +11 -0
- data/lib/reso_transport/entity_type.rb +47 -0
- data/lib/reso_transport/enum.rb +37 -0
- data/lib/reso_transport/metadata.rb +48 -0
- data/lib/reso_transport/metadata_parser.rb +104 -0
- data/lib/reso_transport/property.rb +101 -0
- data/lib/reso_transport/query.rb +147 -0
- data/lib/reso_transport/resource.rb +55 -0
- data/lib/reso_transport/schema.rb +21 -0
- data/lib/reso_transport/version.rb +3 -0
- data/lib/reso_transport.rb +57 -0
- data/reso_transport.gemspec +35 -0
- data/sample_secrets.yml +26 -0
- metadata +172 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: b1de660c3d25c774dbbb7165850c147879631167e4e22b9797d87b8e58649d8d
|
4
|
+
data.tar.gz: 9050fc9ed9d8f01d7649565c76951d968965f66135c4c8bef98a2892afa11390
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: d4415d6ab3b57518b46badbd31a1d55b1ba47450b2eab8063f595c28c94ea9a7088e4aebb87e8d4217fb75c33f538e43256b9f42f90ed760f5c4a5e12b460b4e
|
7
|
+
data.tar.gz: 8aec8759ac045f56e7a1e17d473875bfb8b081fb833a3cbe638606a141c702e768d82b3966df24a0f47408720b662a2968f1b240673acff6b545001c82ce0737
|
data/.gitignore
ADDED
data/.travis.yml
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
2
|
+
|
3
|
+
## Our Pledge
|
4
|
+
|
5
|
+
In the interest of fostering an open and welcoming environment, we as
|
6
|
+
contributors and maintainers pledge to making participation in our project and
|
7
|
+
our community a harassment-free experience for everyone, regardless of age, body
|
8
|
+
size, disability, ethnicity, gender identity and expression, level of experience,
|
9
|
+
nationality, personal appearance, race, religion, or sexual identity and
|
10
|
+
orientation.
|
11
|
+
|
12
|
+
## Our Standards
|
13
|
+
|
14
|
+
Examples of behavior that contributes to creating a positive environment
|
15
|
+
include:
|
16
|
+
|
17
|
+
* Using welcoming and inclusive language
|
18
|
+
* Being respectful of differing viewpoints and experiences
|
19
|
+
* Gracefully accepting constructive criticism
|
20
|
+
* Focusing on what is best for the community
|
21
|
+
* Showing empathy towards other community members
|
22
|
+
|
23
|
+
Examples of unacceptable behavior by participants include:
|
24
|
+
|
25
|
+
* The use of sexualized language or imagery and unwelcome sexual attention or
|
26
|
+
advances
|
27
|
+
* Trolling, insulting/derogatory comments, and personal or political attacks
|
28
|
+
* Public or private harassment
|
29
|
+
* Publishing others' private information, such as a physical or electronic
|
30
|
+
address, without explicit permission
|
31
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
32
|
+
professional setting
|
33
|
+
|
34
|
+
## Our Responsibilities
|
35
|
+
|
36
|
+
Project maintainers are responsible for clarifying the standards of acceptable
|
37
|
+
behavior and are expected to take appropriate and fair corrective action in
|
38
|
+
response to any instances of unacceptable behavior.
|
39
|
+
|
40
|
+
Project maintainers have the right and responsibility to remove, edit, or
|
41
|
+
reject comments, commits, code, wiki edits, issues, and other contributions
|
42
|
+
that are not aligned to this Code of Conduct, or to ban temporarily or
|
43
|
+
permanently any contributor for other behaviors that they deem inappropriate,
|
44
|
+
threatening, offensive, or harmful.
|
45
|
+
|
46
|
+
## Scope
|
47
|
+
|
48
|
+
This Code of Conduct applies both within project spaces and in public spaces
|
49
|
+
when an individual is representing the project or its community. Examples of
|
50
|
+
representing a project or community include using an official project e-mail
|
51
|
+
address, posting via an official social media account, or acting as an appointed
|
52
|
+
representative at an online or offline event. Representation of a project may be
|
53
|
+
further defined and clarified by project maintainers.
|
54
|
+
|
55
|
+
## Enforcement
|
56
|
+
|
57
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
58
|
+
reported by contacting the project team at jon@jondruse.com. All
|
59
|
+
complaints will be reviewed and investigated and will result in a response that
|
60
|
+
is deemed necessary and appropriate to the circumstances. The project team is
|
61
|
+
obligated to maintain confidentiality with regard to the reporter of an incident.
|
62
|
+
Further details of specific enforcement policies may be posted separately.
|
63
|
+
|
64
|
+
Project maintainers who do not follow or enforce the Code of Conduct in good
|
65
|
+
faith may face temporary or permanent repercussions as determined by other
|
66
|
+
members of the project's leadership.
|
67
|
+
|
68
|
+
## Attribution
|
69
|
+
|
70
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
|
71
|
+
available at [http://contributor-covenant.org/version/1/4][version]
|
72
|
+
|
73
|
+
[homepage]: http://contributor-covenant.org
|
74
|
+
[version]: http://contributor-covenant.org/version/1/4/
|
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,33 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
reso_transport (1.0.0)
|
5
|
+
faraday (~> 0.17.0)
|
6
|
+
|
7
|
+
GEM
|
8
|
+
remote: https://rubygems.org/
|
9
|
+
specs:
|
10
|
+
byebug (11.0.1)
|
11
|
+
faraday (0.17.0)
|
12
|
+
multipart-post (>= 1.2, < 3)
|
13
|
+
minitest (5.13.0)
|
14
|
+
minitest-rg (5.2.0)
|
15
|
+
minitest (~> 5.0)
|
16
|
+
multipart-post (2.1.1)
|
17
|
+
rake (10.5.0)
|
18
|
+
vcr (5.0.0)
|
19
|
+
|
20
|
+
PLATFORMS
|
21
|
+
ruby
|
22
|
+
|
23
|
+
DEPENDENCIES
|
24
|
+
bundler (~> 1.17)
|
25
|
+
byebug
|
26
|
+
minitest (~> 5.0)
|
27
|
+
minitest-rg (~> 5.0)
|
28
|
+
rake (~> 10.0)
|
29
|
+
reso_transport!
|
30
|
+
vcr (~> 5.0)
|
31
|
+
|
32
|
+
BUNDLED WITH
|
33
|
+
1.17.2
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2019 Jon Druse
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,193 @@
|
|
1
|
+
# ResoTransport
|
2
|
+
|
3
|
+
A Ruby gem for connecting to and interacting with RESO WebAPI services. Learn more about what that is by checking out the [RESO WebAPI](https://www.reso.org/reso-web-api/) Documentation.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'reso_transport'
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install reso_transport
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
|
24
|
+
### Getting Connected
|
25
|
+
|
26
|
+
There are 2 strategies for authentication.
|
27
|
+
|
28
|
+
**Bearer Token**
|
29
|
+
|
30
|
+
It's simple to use a static access token if your token never expires:
|
31
|
+
|
32
|
+
```ruby
|
33
|
+
@client = ResoTransport::Client.new({
|
34
|
+
md_file: METADATA_CACHE,
|
35
|
+
endpoint: ENDPOINT_URL
|
36
|
+
authentication: {
|
37
|
+
access_token: TOKEN,
|
38
|
+
token_type: "Bearer" # this is the default and can be ommitted
|
39
|
+
}
|
40
|
+
})
|
41
|
+
```
|
42
|
+
|
43
|
+
|
44
|
+
**Authorization Endpoint**
|
45
|
+
|
46
|
+
If the connection requires requesting a new token periodically, it's easy to provide that information:
|
47
|
+
|
48
|
+
```ruby
|
49
|
+
@client = ResoTransport::Client.new({
|
50
|
+
md_file: METADATA_CACHE,
|
51
|
+
endpoint: ENDPOINT_URL
|
52
|
+
authentication: {
|
53
|
+
endpoint: AUTH_ENDPOINT,
|
54
|
+
client_id: CLIENT_ID,
|
55
|
+
client_secret: CLIENT_SECRET,
|
56
|
+
grant_type: "client_credentials", # these are the default and can be ommitted
|
57
|
+
scope: "api" #
|
58
|
+
}
|
59
|
+
})
|
60
|
+
```
|
61
|
+
|
62
|
+
This will pre-fetch a token from the provided endpoint when the current token is either non-existent or has expired.
|
63
|
+
|
64
|
+
|
65
|
+
### Resources
|
66
|
+
|
67
|
+
Once you have a successful connection you can explore what resources are available from the API:
|
68
|
+
|
69
|
+
|
70
|
+
```ruby
|
71
|
+
@client.resources
|
72
|
+
#=> {"Property"=>#<ResoTransport::Resource entity_set="Property", schema="ODataService">, "Office"=>#<ResoTransport::Resource entity_set="Office", schema="ODataService">, "Member"=>#<ResoTransport::Resource entity_set="Member", schema="ODataService">}
|
73
|
+
|
74
|
+
@client.resources["Property"]
|
75
|
+
#=> #<ResoTransport::Resource entity_set="Property", schema="ODataService">
|
76
|
+
|
77
|
+
@client.resources["Property"].query.limit(1).results
|
78
|
+
#=> Results Array
|
79
|
+
```
|
80
|
+
|
81
|
+
#### Querying
|
82
|
+
|
83
|
+
ResoTransport provides powerful querying capabilities:
|
84
|
+
|
85
|
+
To get 10 listings in Los Angeles between 900K and 1M and at least 5 bedrooms:
|
86
|
+
```ruby
|
87
|
+
@resource.query.
|
88
|
+
eq(City: "Los Angeles").
|
89
|
+
le(ListPrice: 1_000_000).
|
90
|
+
ge(ListPrice: 900_000, Bedrooms: 5).
|
91
|
+
limit(10).
|
92
|
+
results
|
93
|
+
```
|
94
|
+
|
95
|
+
To get 10 listings in Los Angeles OR Hollywood between 900K and 1M and at least 5 bedrooms:
|
96
|
+
```ruby
|
97
|
+
@resource.query.
|
98
|
+
any {
|
99
|
+
eq(City: "Los Angeles").eq(City: "Hollywood")
|
100
|
+
}.
|
101
|
+
le(ListPrice: 1_000_000).
|
102
|
+
ge(ListPrice: 900_000, Bedrooms: 5).
|
103
|
+
limit(10).
|
104
|
+
results
|
105
|
+
```
|
106
|
+
|
107
|
+
#### Expanding Child Records
|
108
|
+
|
109
|
+
To see what child records can be expanded look at `expandable`:
|
110
|
+
|
111
|
+
```ruby
|
112
|
+
@resource.expandable
|
113
|
+
#=> [#<struct ResoTransport::Property name="Media", data_type="Collection(RESO.Media)", attrs={"Name"=>"Media", "Type"=>"Collection(RESO.Media)"}, multi=true, enum=nil, complex_type=nil, entity_type=#<struct ResoTransport::EntityType name="Media", base_type=nil, primary_key="MediaKey", schema="CoreLogic.DataStandard.RESO.DD">> ...]
|
114
|
+
```
|
115
|
+
|
116
|
+
Use `expand` to expand child records with the top level results.
|
117
|
+
|
118
|
+
```ruby
|
119
|
+
@resource.query.expand("Media").limit(10).results
|
120
|
+
#=> Results Array
|
121
|
+
```
|
122
|
+
|
123
|
+
You have several options to expand multiple child record sets. Each of these will have the same result.
|
124
|
+
|
125
|
+
```ruby
|
126
|
+
@resource.query.expand("Media", "Office").limit(10).results
|
127
|
+
|
128
|
+
@resource.query.expand(["Media", "Office"]).limit(10).results
|
129
|
+
|
130
|
+
@resource.query.expand("Media").expand("Office").limit(10).results
|
131
|
+
```
|
132
|
+
|
133
|
+
### Results Array
|
134
|
+
|
135
|
+
The results are parsed according to the metadata with some things worth mentioning:
|
136
|
+
|
137
|
+
* Date fields are parsed into ruby `DateTime` objects
|
138
|
+
* Enumeration fields are parsed into either the `Name` or `Annotation -> String` of the member that is represented.
|
139
|
+
* Collections or Enumerations with `is_flags=true` will also be parsed into an `Array`.
|
140
|
+
|
141
|
+
### Enumerations
|
142
|
+
|
143
|
+
Enumerations are essentially a mapping of system values and display values. To see a mapping:
|
144
|
+
|
145
|
+
```ruby
|
146
|
+
@resource.property("StandardStatus").enum.mapping
|
147
|
+
|
148
|
+
=> {
|
149
|
+
"Active"=>"Active",
|
150
|
+
"ActiveUnderContract"=>"Active Under Contract",
|
151
|
+
"Canceled"=>"Canceled",
|
152
|
+
"Closed"=>"Closed",
|
153
|
+
"ComingSoon"=>"Coming Soon",
|
154
|
+
"Delete"=>"Delete",
|
155
|
+
"Expired"=>"Expired",
|
156
|
+
"Hold"=>"Hold",
|
157
|
+
"Incomplete"=>"Incomplete",
|
158
|
+
"Pending"=>"Pending",
|
159
|
+
"Withdrawn"=>"Withdrawn"
|
160
|
+
}
|
161
|
+
```
|
162
|
+
|
163
|
+
Most Enumerations will ultimately be used to fill a dropdown with options to select from. Like so:
|
164
|
+
|
165
|
+
```ruby
|
166
|
+
@resource.property("StandardStatus").enum.mapping.values
|
167
|
+
#=> ["Active", "Active Under Contract", "Canceled", "Closed", "Coming Soon", "Delete", "Expired", "Hold", "Incomplete", "Pending", "Withdrawn"]
|
168
|
+
```
|
169
|
+
|
170
|
+
When querying for an enumeration value, you can provide either the system name, or the display name and it will be converted to the correct value. This allows your programs to not worry too much about the system values.
|
171
|
+
|
172
|
+
```ruby
|
173
|
+
@resource.query.eq(StandardStatus: "Active Under Contract").limit(1).compile_params
|
174
|
+
#=> {"$top"=>1, "$filter"=>"StandardStatus eq 'ActiveUnderContract'"}
|
175
|
+
```
|
176
|
+
|
177
|
+
## Development
|
178
|
+
|
179
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
180
|
+
|
181
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
182
|
+
|
183
|
+
## Contributing
|
184
|
+
|
185
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/reso_transport. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
|
186
|
+
|
187
|
+
## License
|
188
|
+
|
189
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
190
|
+
|
191
|
+
## Code of Conduct
|
192
|
+
|
193
|
+
Everyone interacting in the ResoTransport project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/reso_transport/blob/master/CODE_OF_CONDUCT.md).
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,30 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "reso_transport"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
#
|
13
|
+
|
14
|
+
ResoTransport.configure do |c|
|
15
|
+
c.logger = Logger.new("log/console.log")
|
16
|
+
end
|
17
|
+
|
18
|
+
|
19
|
+
require "irb"
|
20
|
+
require 'yaml'
|
21
|
+
require 'byebug'
|
22
|
+
|
23
|
+
SECRETS = YAML.load_file("secrets.yml")
|
24
|
+
|
25
|
+
@trestle = ResoTransport::Client.new(SECRETS[:trestle])
|
26
|
+
@bridge = ResoTransport::Client.new(SECRETS[:bridge])
|
27
|
+
@spark = ResoTransport::Client.new(SECRETS[:spark])
|
28
|
+
@crmls = ResoTransport::Client.new(SECRETS[:crmls])
|
29
|
+
|
30
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
module ResoTransport
|
2
|
+
module Authentication
|
3
|
+
# Session class for TokenAuth. This stores the access token, the token type
|
4
|
+
# (usually `Bearer`), and the expiration date of the token.
|
5
|
+
class Access
|
6
|
+
attr_accessor :access_token, :expires, :token_type
|
7
|
+
|
8
|
+
def initialize(options = {})
|
9
|
+
@access_token = options[:access_token]
|
10
|
+
@expires = Time.now + options[:expires_in]
|
11
|
+
@token_type = options[:token_type]
|
12
|
+
end
|
13
|
+
|
14
|
+
def expired?
|
15
|
+
Time.now > expires
|
16
|
+
end
|
17
|
+
|
18
|
+
def valid?
|
19
|
+
!!access_token && !expired?
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
module ResoTransport
|
2
|
+
module Authentication
|
3
|
+
# This base class defines the basic interface support by all client authentication implementations.
|
4
|
+
class AuthStrategy
|
5
|
+
attr_reader :access
|
6
|
+
|
7
|
+
# @abstract Perform requests to authenticate the client with the API
|
8
|
+
# @return [Access] The access token object
|
9
|
+
def authenticate(*)
|
10
|
+
raise NotImplementedError, 'Implement me!'
|
11
|
+
end
|
12
|
+
|
13
|
+
# Ensure that a valid access token is present or raise an exception
|
14
|
+
# @raise [ResoWebApi::Errors::AccessDenied] If authentication fails
|
15
|
+
def ensure_valid_access!
|
16
|
+
@access = authenticate unless access && access.valid?
|
17
|
+
access
|
18
|
+
end
|
19
|
+
|
20
|
+
# Resets access
|
21
|
+
def reset
|
22
|
+
@access = nil
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
@@ -0,0 +1,49 @@
|
|
1
|
+
module ResoTransport
|
2
|
+
module Authentication
|
3
|
+
class FetchTokenAuth < AuthStrategy
|
4
|
+
attr_reader :connection, :endpoint, :client_id, :client_secret, :grant_type, :scope
|
5
|
+
|
6
|
+
def initialize(options)
|
7
|
+
@grant_type = options.fetch(:grant_type, "client_credentials")
|
8
|
+
@scope = options.fetch(:scope, "api")
|
9
|
+
@client_id = options.fetch(:client_id)
|
10
|
+
@client_secret = options.fetch(:client_secret)
|
11
|
+
@endpoint = options.fetch(:endpoint)
|
12
|
+
|
13
|
+
@connection = Faraday.new(@endpoint) do |faraday|
|
14
|
+
faraday.request :url_encoded
|
15
|
+
faraday.response :logger, ResoTransport.configuration.logger if ResoTransport.configuration.logger
|
16
|
+
faraday.adapter Faraday.default_adapter
|
17
|
+
faraday.basic_auth @client_id, @client_secret
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
def authenticate
|
22
|
+
response = connection.post nil, auth_params
|
23
|
+
json = JSON.parse response.body
|
24
|
+
|
25
|
+
unless response.success?
|
26
|
+
message = "#{response.reason_phrase}: #{json['error'] || response.body}"
|
27
|
+
raise ResoTransport::AccessDenied, response: response, message: message
|
28
|
+
end
|
29
|
+
|
30
|
+
Access.new({
|
31
|
+
access_token: json.fetch('access_token'),
|
32
|
+
expires_in: json.fetch('expires_in', 1 << (1.size * 8 - 2) - 1),
|
33
|
+
token_type: json.fetch('token_type', "Bearer")
|
34
|
+
})
|
35
|
+
end
|
36
|
+
|
37
|
+
private
|
38
|
+
|
39
|
+
def auth_params
|
40
|
+
{
|
41
|
+
client_id: client_id,
|
42
|
+
client_secret: client_secret,
|
43
|
+
grant_type: grant_type,
|
44
|
+
scope: scope
|
45
|
+
}
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
@@ -0,0 +1,46 @@
|
|
1
|
+
module ResoTransport
|
2
|
+
module Authentication
|
3
|
+
# Authentication middleware
|
4
|
+
# Ensures that each request is made with proper `Authorization` header set
|
5
|
+
# and raises an exception if a request yields a `401 Access Denied` response.
|
6
|
+
class Middleware < Faraday::Middleware
|
7
|
+
AUTH_HEADER = 'Authorization'.freeze
|
8
|
+
|
9
|
+
def initialize(app, auth)
|
10
|
+
super(app)
|
11
|
+
@auth = auth
|
12
|
+
end
|
13
|
+
|
14
|
+
def call(request_env)
|
15
|
+
retries = 1
|
16
|
+
|
17
|
+
begin
|
18
|
+
authorize_request(request_env)
|
19
|
+
|
20
|
+
@app.call(request_env).on_complete do |response_env|
|
21
|
+
raise_if_unauthorized(response_env)
|
22
|
+
end
|
23
|
+
rescue ResoTransport::AccessDenied
|
24
|
+
raise if retries == 0
|
25
|
+
@auth.reset
|
26
|
+
retries -= 1
|
27
|
+
retry
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
private
|
32
|
+
|
33
|
+
def authorize_request(request_env)
|
34
|
+
@auth.ensure_valid_access!
|
35
|
+
|
36
|
+
request_env[:request_headers].merge!(
|
37
|
+
AUTH_HEADER => "#{@auth.access.token_type} #{@auth.access.access_token}"
|
38
|
+
)
|
39
|
+
end
|
40
|
+
|
41
|
+
def raise_if_unauthorized(response_env)
|
42
|
+
raise ResoTransport::AccessDenied if response_env[:status] == 401
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
module ResoTransport
|
2
|
+
module Authentication
|
3
|
+
# A simple auth strategy that uses a static, non-expiring token.
|
4
|
+
class StaticTokenAuth < AuthStrategy
|
5
|
+
attr_reader :access_token
|
6
|
+
attr_reader :token_type
|
7
|
+
|
8
|
+
def initialize(options)
|
9
|
+
@access_token = options.fetch(:access_token)
|
10
|
+
@token_type = options.fetch(:token_type, "Bearer")
|
11
|
+
end
|
12
|
+
|
13
|
+
# Simply returns a static, never expiring access token
|
14
|
+
# @return [Access] The access token object
|
15
|
+
def authenticate
|
16
|
+
Access.new(
|
17
|
+
access_token: access_token,
|
18
|
+
token_type: token_type,
|
19
|
+
expires_in: 1 << (1.size * 8 - 2) - 1 # Max int value
|
20
|
+
)
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
@@ -0,0 +1,53 @@
|
|
1
|
+
module ResoTransport
|
2
|
+
class Client
|
3
|
+
attr_reader :connection, :uid, :vendor, :endpoint, :auth, :md_file
|
4
|
+
|
5
|
+
def initialize(options)
|
6
|
+
@endpoint = options.fetch(:endpoint)
|
7
|
+
@md_file = options.fetch(:md_file, nil)
|
8
|
+
@authentication = ensure_valid_auth_strategy(options.fetch(:authentication))
|
9
|
+
@vendor = options.fetch(:vendor, {})
|
10
|
+
@faraday_options = options.fetch(:faraday_options, {})
|
11
|
+
|
12
|
+
@connection = Faraday.new(@endpoint, @faraday_options) do |faraday|
|
13
|
+
faraday.request :url_encoded
|
14
|
+
faraday.response :logger, ResoTransport.configuration.logger if ResoTransport.configuration.logger
|
15
|
+
#yield faraday if block_given?
|
16
|
+
faraday.use Authentication::Middleware, @authentication
|
17
|
+
faraday.adapter Faraday.default_adapter #unless faraday.builder.send(:adapter_set?)
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
def resources
|
22
|
+
@resources ||= metadata.entity_sets.map {|es| {es.name => Resource.new(self, es)} }.reduce(:merge!)
|
23
|
+
end
|
24
|
+
|
25
|
+
def metadata
|
26
|
+
@metadata ||= Metadata.new(self)
|
27
|
+
end
|
28
|
+
|
29
|
+
def to_s
|
30
|
+
%(#<ResoTransport::Client endpoint="#{endpoint}", md_file="#{md_file}">)
|
31
|
+
end
|
32
|
+
|
33
|
+
def inspect
|
34
|
+
to_s
|
35
|
+
end
|
36
|
+
|
37
|
+
private
|
38
|
+
|
39
|
+
def ensure_valid_auth_strategy(options)
|
40
|
+
case options
|
41
|
+
when Hash
|
42
|
+
if options.has_key?(:endpoint)
|
43
|
+
Authentication::FetchTokenAuth.new(options)
|
44
|
+
else
|
45
|
+
Authentication::StaticTokenAuth.new(options)
|
46
|
+
end
|
47
|
+
else
|
48
|
+
raise ArgumentError, "#{options.inspect} invalid: cannot determine strategy"
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
end
|
53
|
+
end
|
@@ -0,0 +1,11 @@
|
|
1
|
+
module ResoTransport
|
2
|
+
EntitySet = Struct.new(:name, :schema, :entity_type) do
|
3
|
+
|
4
|
+
def self.from_stream(args)
|
5
|
+
schema, entity_type = ResoTransport.split_schema_and_class_name(args["EntityType"])
|
6
|
+
|
7
|
+
new(args["Name"], schema, entity_type)
|
8
|
+
end
|
9
|
+
|
10
|
+
end
|
11
|
+
end
|