httpie / httpie-http2

(DEPRECATED) Experimental HTTP/2 plugin for HTTPie
https://httpie.org
Other
68 stars 4 forks source link

Only First Attempt to Send Succeeds #10

Closed jlaskowski closed 6 years ago

jlaskowski commented 6 years ago

Below is a class I use to send notifications to my iPhone 7. I'm running this under Debian on a Raspberry Pi 2 B. The first time I call the send_notification method, it works fine. The second time, I get the lines below in my log file, but it doesn't seem to return, since I don't hit either the logging after a successful call or the logging in the exception handler.

import jwt
import time
import json
from hyper import HTTPConnection
import logging

class Apns:
  ALGORITHM = 'ES256'

  APNS_KEY_ID = '4A66263KZ6'
  APNS_AUTH_KEY = 'AuthKey_4A66263KZ6.p8'
  TEAM_ID = 'JUPE6KNM4G'

  f = open(APNS_AUTH_KEY)
  secret = f.read()

  token = jwt.encode(
    {
        'iss': TEAM_ID,
        'iat': time.time()
    },
    secret,
    algorithm= ALGORITHM,
    headers={
        'alg': ALGORITHM,
        'kid': APNS_KEY_ID,
    }
  )
  BUNDLE_ID = 'com.ccyl.alarm'

  registration_id = ['fb15ba7e9546977c403229bbda9073f8b59e670f5ebfaf1cad41931aa24db290']

  path = '/3/device/{0}'.format(registration_id[0])

  request_headers = {
    'apns-expiration': '0',
    'apns-priority': '10',
    'apns-topic': BUNDLE_ID,
    'authorization': 'bearer {0}'.format(token.decode('ascii'))
  }

  def send_notification(self, message, sound_file_name='blah.mp3', log_level = logging.INFO):
    try:
      logging.basicConfig(filename='./logfile.log',level=log_level)

      payload_data = {
        'aps': { 'alert' : message,
                 'sound' : 'www/' + sound_file_name }
      }
      payload = json.dumps(payload_data).encode('utf-8')
      conn = HTTPConnection('api.development.push.apple.com:443')
      conn.request(
        'POST',
        Apns.path,
        payload,
        headers=Apns.request_headers
      )

      resp = conn.get_response()
      conn.close()
      logging.debug(resp.status)
      logging.debug(resp.read())
    except:
      logging.debug("Unexpected error:", sys.exc_info()[0])

ERROR:

INFO:hyper.http20.connection:Received unhandled event <RemoteSettingsChanged changed_settings:{ChangedSetting(setting=SettingCodes.HEADER_TABLE_SIZE, original_value=4096, new_value=4096), ChangedSetting(setting=SettingCodes.MAX_CONCURRENT_STREAMS, original_value=None, new_value=1), ChangedSetting(setting=SettingCodes._max_frame_size, original_value=16384, new_value=16384), ChangedSetting(setting=SettingCodes._max_header_list_size, original_value=None, new_value=8000)}>
INFO:hyper.http20.connection:Received unhandled event <SettingsAcknowledged changed_settings:{ChangedSetting(setting=SettingCodes.ENABLE_PUSH, original_value=1, new_value=0)}>
INFO:hyper.http20.connection:Received unhandled event <SettingsAcknowledged changed_settings:{}>
INFO:hyper.http20.connection:Received unhandled event <RemoteSettingsChanged changed_settings:{ChangedSetting(setting=SettingCodes.HEADER_TABLE_SIZE, original_value=4096, new_value=4096), ChangedSetting(setting=SettingCodes.MAX_CONCURRENT_STREAMS, original_value=1, new_value=1000), ChangedSetting(setting=SettingCodes.INITIAL_WINDOW_SIZE, original_value=65535, new_value=65535), ChangedSetting(setting=SettingCodes._max_frame_size, original_value=16384, new_value=16384), ChangedSetting(setting=SettingCodes._max_header_list_size, original_value=8000, new_value=8000)}>
jlaskowski commented 6 years ago

Problems with caller...resolved.