68 lines
2.5 KiB
Python
68 lines
2.5 KiB
Python
#===============================================================================
|
|
# Copyright (C) 2014 Ryan Holmes
|
|
#
|
|
# This file is part of pyfa.
|
|
#
|
|
# pyfa is free software: you can redistribute it and/or modify
|
|
# it under the terms of the GNU General Public License as published by
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
# (at your option) any later version.
|
|
#
|
|
# pyfa is distributed in the hope that it will be useful,
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
# GNU General Public License for more details.
|
|
#
|
|
# You should have received a copy of the GNU General Public License
|
|
# along with pyfa. If not, see <http://www.gnu.org/licenses/>.
|
|
#===============================================================================
|
|
|
|
from service.settings import NetworkSettings
|
|
import urllib2
|
|
import urllib
|
|
import config
|
|
|
|
class Network():
|
|
# Request constants - every request must supply this, as it is checked if
|
|
# enabled or not via settings
|
|
ENABLED = 1
|
|
EVE = 2 # Mostly API, but also covers CREST requests
|
|
PRICES = 4
|
|
UPDATE = 8
|
|
|
|
_instance = None
|
|
@classmethod
|
|
def getInstance(cls):
|
|
if cls._instance == None:
|
|
cls._instance = Network()
|
|
|
|
return cls._instance
|
|
|
|
def request(self, url, type=None, postData=None):
|
|
|
|
# URL is required to be https as of right now
|
|
print "Starting request: %s\n\tType: %s\n\tPost Data: %s"%(url,type,postData)
|
|
|
|
# Make sure request is enabled
|
|
access = NetworkSettings.getInstance().getAccess()
|
|
|
|
if not type or not type & access: # @todo: check if enabled
|
|
print "\tType not enabled"
|
|
return # @todo: throw exception
|
|
|
|
# Set up some things for the request
|
|
versionString = "{0} {1} - {2} {3}".format(config.version, config.tag, config.expansionName, config.expansionVersion)
|
|
headers={"User-Agent" : "pyfa {0} (Python-urllib2)".format(versionString)}
|
|
|
|
proxy = NetworkSettings.getInstance().getProxySettings()
|
|
if proxy is not None:
|
|
print "\tUsing a proxy"
|
|
proxy = urllib2.ProxyHandler({'https': "{0}:{1}".format(*proxy)})
|
|
opener = urllib2.build_opener(proxy)
|
|
urllib2.install_opener(opener)
|
|
|
|
request = urllib2.Request(url, headers=headers, data=urllib.urlencode(postData) if postData else None)
|
|
data = urllib2.urlopen(request)
|
|
print "\tReturning data"
|
|
return data
|