<commit_before>require 'net/http'

require_relative '../options_base'

module EasyUpnp
  class EventClient
    class Options < EasyUpnp::OptionsBase
      DEFAULTS = {
        timeout: 300
      }

      def initialize(options, &block)
        super(options, DEFAULTS, &block)
      end
    end

    def initialize(events_endpoint, callback_url, options = {}, &block)
      @options = Options.new(options, &block)
      @events_endpoint = URI(events_endpoint)
      @callback_url = callback_url
    end

    def subscribe
      req = SubscribeRequest.new(
        @events_endpoint,
        @callback_url,
        @options.timeout
      )
      do_request(req)['sid']
    end

    def unsubscribe(sid)
      req = UnsubscribeRequest.new(
        @events_endpoint,
        sid
      )
      do_request(req)
    end

    def resubscribe
    end

    private

    def do_request(req)
      uri = URI(@events_endpoint)
      Net::HTTP.start(uri.host, uri.port) do |http|
        puts req.inspect
        puts req.to_hash
        return http.request(req)
      end
    end

    class EventRequest < Net::HTTPRequest

      def timeout=(v)
        self['TIMEOUT'] = "Second-#{v}"
      end

      def callback=(v)
        self['CALLBACK'] = "<#{v}>"
        self['NT'] = 'upnp:event'
      end

      def sid=(v)
        self['SID'] = v
      end
    end

    class ResubscribeRequest < EventRequest
      METHOD = 'SUBSCRIBE'
      REQUEST_HAS_BODY = false
      RESPONSE_HAS_BODY = false

      def initialize(event_url, sid, timeout)
        super(URI(event_url))
        self.timeout = timeout
        self.sid = sid
      end
    end

    class SubscribeRequest < EventRequest
      METHOD = 'SUBSCRIBE'
      REQUEST_HAS_BODY = false
      RESPONSE_HAS_BODY = false

      def initialize(event_url, callback_url, timeout)
        super(URI(event_url))
        self.timeout = timeout
        self.callback = callback_url
      end
    end

    class UnsubscribeRequest < EventRequest
      METHOD = 'UNSUBSCRIBE'
      REQUEST_HAS_BODY = false
      RESPONSE_HAS_BODY = false

      def initialize(event_url, sid)<commit_msg>Add client to manage event subscriptions<commit_after>require 'net/http'

require_relative '../options_base'

module EasyUpnp
  class EventClient
    def initialize(events_endpoint, callback_url)
      @events_endpoint = URI(events_endpoint)
      @callback_url = callback_url
    end

    def subscribe(timeout: 300)
      req = SubscribeRequest.new(
        @events_endpoint,
        @callback_url,
        timeout
      )

      response = do_request(req)

      if !response['SID']
        raise RuntimeError, "SID header not present in response: #{response.to_hash}"
      end

      response['SID']
    end

    def unsubscribe(sid)
      req = UnsubscribeRequest.new(
        @events_endpoint,
        sid
      )
      do_request(req)
      true
    end

    def resubscribe(sid, timeout: 300)
      req = ResubscribeRequest.new(
        @events_endpoint,
        sid,
        timeout
      )
      do_request(req)
      true
    end

    private

    def do_request(req)
      uri = URI(@events_endpoint)
      Net::HTTP.start(uri.host, uri.port) do |http|
        response = http.request(req)

        if response.code.to_i != 200
          raise RuntimeError, "Unexpected response type (#{response.code}): #{response.body}"
        end

        return response
      end
    end

    class EventRequest < Net::HTTPRequest
      def timeout=(v)
        self['TIMEOUT'] = "Second-#{v}"
      end

      def callback=(v)
        self['CALLBACK'] = "<#{v}>"
        self['NT'] = 'upnp:event'
      end

      def sid=(v)
        self['SID'] = v
      end
    end

    class ResubscribeRequest < EventRequest
      METHOD = 'SUBSCRIBE'
      REQUEST_HAS_BODY = false
      RESPONSE_HAS_BODY = true

      def initialize(event_url, sid, timeout)
        super(URI(event_url))
        self.timeout = timeout
        self.sid = sid
      end
    end

    class SubscribeRequest < EventRequest
      METHOD = 'SUBSCRIBE'
      REQUEST_HAS_BODY = false
      RESPONSE_HAS_BODY = true

      def initialize(event_url, callback_url, timeout)
        super(URI(event_url))
        self.timeout = timeout
        self.callback = callback_url
      end
    end

    class UnsubscribeRequest < EventRequest
      METHOD = 'UNSUBSCRIBE'
      REQUEST_HAS_BODY = false
      RESPONSE_HAS_BODY = true

      def initialize(event_url, sid)
{'fraction_non_alphanumeric': 0.08847967662250168, 'fraction_numerical': 0.002694812485964518, 'mean_word_length': 2.143260409315455, 'pattern_counts': {'":': 0, '<': 14, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6798148', 'n_tokens_mistral': 1426, 'n_tokens_neox': 1343, 'n_words': 309}