fritzbox-smarthome 0.6.0 → 0.7.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 87ca99bd2b9c7f0ab8cc157255593d9211b16e168f838e3a7b643dd273b541e2
4
- data.tar.gz: c9d862aa7dfae2b02cb16694e2caf8595397fbdc990e84fb0539159b1a5dfee6
3
+ metadata.gz: c4ed289626e1d8c8cacccb9f89eed39178c5b11c781c99a649b3872521a3d0d4
4
+ data.tar.gz: 02b35061da61c6f1084b87192eefe55cc83113526addf7caff584240256997f7
5
5
  SHA512:
6
- metadata.gz: de120146d37f15a50a43c0ba89c19cee46f91131a4765a01d518cedf897ecaa053269c6cae7bb5a6ccbdd35bd48904571722c6bafcd475d28e92ce4ff636d63c
7
- data.tar.gz: 62629678cefbef554aa83ff465a3086661c0100075375723fdefe0ab655816b441da4c17aabba5c8d9462172cd559d6d64461ca584ed496c4c4769f5e815f9ea
6
+ metadata.gz: d2840a781e1717f8e9b11fa1a18929b316c930e57ee3b5e57a24574ad2f065c1b44fa3837e85dc0e88367ff1f402826d5210d8bbf2b9a9079b48dbe655a9d1e8
7
+ data.tar.gz: 02f972de29e194f5c42f7671c3caddfdfe046951e0fd91095f4614412c5afb76a1861ed71211f89d0e44b52ff909dd79df4f7e93d558d50e9903d217ab0e8be4
data/.ruby-version CHANGED
@@ -1 +1 @@
1
- 3.1.3
1
+ 3.2.0
data/CHANGELOG.md CHANGED
@@ -1,5 +1,12 @@
1
1
  # Changelog
2
2
 
3
+ ## v0.7.0
4
+
5
+ * Measure HTTP request duration
6
+ * Print the request time in debug log-level
7
+ * Cache authentication between requests
8
+ * Keep the returned SessionID for 60 minutes in memory
9
+
3
10
  ## v0.6.0
4
11
 
5
12
  * Add support for `Actor.find_by!(ain:)` and `Actor#reload`
@@ -1,6 +1,10 @@
1
1
  module Fritzbox
2
2
  module Smarthome
3
3
  class Resource
4
+ cattr_accessor :session
5
+
6
+ AuthenticationError = Class.new(RuntimeError)
7
+
4
8
  class << self
5
9
  # @param params [Hash] key/value pairs that will be appended to the switchcmd query string
6
10
  def get(command:, ain: nil, param: nil, **params)
@@ -12,9 +16,16 @@ module Fritzbox
12
16
  url = "#{url}&#{key}=#{value}"
13
17
  end
14
18
 
15
- config.logger.debug(url)
19
+ response = measure(url) { HTTParty.get(url, **httparty_options) }
20
+
21
+ raise AuthenticationError if response.code == 403
16
22
 
17
- HTTParty.get(url, **httparty_options)
23
+ response
24
+ rescue AuthenticationError
25
+ raise if session.nil?
26
+
27
+ self.session = nil
28
+ retry
18
29
  end
19
30
 
20
31
  def parse(response)
@@ -26,19 +37,28 @@ module Fritzbox
26
37
  delegate :config, to: Smarthome
27
38
 
28
39
  def authenticate
29
- response = HTTParty.get(login_endpoint, **httparty_options)
30
- xml = nori.parse(response.body)
31
- challenge = xml.dig('SessionInfo', 'Challenge')
40
+ return session.id if session.present? && session.valid?
41
+
42
+ session_id = measure("authentication") do
43
+ response = HTTParty.get(login_endpoint, **httparty_options)
44
+ xml = nori.parse(response.body)
45
+ challenge = xml.dig('SessionInfo', 'Challenge')
46
+
47
+ md5 = Digest::MD5.hexdigest("#{challenge}-#{config.password}".encode('UTF-16LE'))
32
48
 
33
- md5 = Digest::MD5.hexdigest("#{challenge}-#{config.password}".encode('UTF-16LE'))
49
+ url = "#{login_endpoint}?response=#{challenge}-#{md5}"
50
+ url = "#{url}&username=#{config.username}" if config.username.present?
34
51
 
35
- url = "#{login_endpoint}?response=#{challenge}-#{md5}"
36
- url = "#{url}&username=#{config.username}" if config.username.present?
52
+ response = HTTParty.get(url, **httparty_options)
37
53
 
38
- response = HTTParty.get(url, **httparty_options)
54
+ xml = nori.parse(response.body)
39
55
 
40
- xml = nori.parse(response.body)
41
- xml.dig('SessionInfo', 'SID')
56
+ xml.dig('SessionInfo', 'SID')
57
+ end
58
+
59
+ self.session = Session.new(session_id)
60
+
61
+ session_id
42
62
  end
43
63
 
44
64
  def login_endpoint
@@ -55,6 +75,14 @@ module Fritzbox
55
75
  def nori
56
76
  @nori ||= Nori.new
57
77
  end
78
+
79
+ def measure(identifier, &block)
80
+ time_start = Time.now
81
+ result = block.call
82
+ time_elapsed = (Time.now - time_start).to_f.round(3)
83
+ config.logger.debug("Request `#{identifier}` took #{time_elapsed} seconds")
84
+ result
85
+ end
58
86
  end
59
87
 
60
88
  delegate :get, :parse, to: :class
@@ -0,0 +1,22 @@
1
+ module Fritzbox
2
+ module Smarthome
3
+ class Session
4
+ TIMEOUT_MINUTES = 60
5
+
6
+ def initialize(id)
7
+ self.id = id
8
+ self.valid_until = Time.now + TIMEOUT_MINUTES.minutes
9
+ end
10
+
11
+ def valid?
12
+ self.valid_until > Time.now
13
+ end
14
+
15
+ attr_reader :id, :valid_until
16
+
17
+ private
18
+
19
+ attr_writer :id, :valid_until
20
+ end
21
+ end
22
+ end
@@ -1,5 +1,5 @@
1
1
  module Fritzbox
2
2
  module Smarthome
3
- VERSION = '0.6.0'.freeze
3
+ VERSION = '0.7.0'.freeze
4
4
  end
5
5
  end
@@ -6,6 +6,7 @@ require 'nori'
6
6
  require 'fritzbox/smarthome/version'
7
7
  require 'fritzbox/smarthome/null_logger'
8
8
  require 'fritzbox/smarthome/properties'
9
+ require 'fritzbox/smarthome/session'
9
10
  require 'fritzbox/smarthome/resource'
10
11
  require 'fritzbox/smarthome/actor'
11
12
  require 'fritzbox/smarthome/heater'
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fritzbox-smarthome
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.0
4
+ version: 0.7.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Klaus Meyer
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2022-12-06 00:00:00.000000000 Z
11
+ date: 2022-12-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -176,6 +176,7 @@ files:
176
176
  - lib/fritzbox/smarthome/properties.rb
177
177
  - lib/fritzbox/smarthome/properties/simple_on_off.rb
178
178
  - lib/fritzbox/smarthome/resource.rb
179
+ - lib/fritzbox/smarthome/session.rb
179
180
  - lib/fritzbox/smarthome/smoke_detector.rb
180
181
  - lib/fritzbox/smarthome/switch.rb
181
182
  - lib/fritzbox/smarthome/version.rb
@@ -198,7 +199,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
198
199
  - !ruby/object:Gem::Version
199
200
  version: '0'
200
201
  requirements: []
201
- rubygems_version: 3.3.7
202
+ rubygems_version: 3.4.1
202
203
  signing_key:
203
204
  specification_version: 4
204
205
  summary: Client library to interface with Smarthome features of your FritzBox