# __BEGIN_LICENSE__
# Copyright (c) 2015, United States Government, as represented by the
# Administrator of the National Aeronautics and Space Administration.
# All rights reserved.
#
# The xGDS platform is licensed under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# http://www.apache.org/licenses/LICENSE-2.0.
#
# Unless required by applicable law or agreed to in writing, software distributed
# under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
# CONDITIONS OF ANY KIND, either express or implied. See the License for the
# specific language governing permissions and limitations under the License.
# __END_LICENSE__

from dateutil.relativedelta import relativedelta
from django.utils import timezone
import urlparse
import requests


def get100Years():
    theNow = timezone.now() + relativedelta(years=100)
    return theNow

def addPort(url, port, http=True):
    ''' Add a port to a url '''
    if port:
        parsed = urlparse.urlparse(url)
        replaced = parsed._replace(netloc="{}:{}".format(parsed.hostname, port))
        if http:
            replaced = replaced._replace(scheme='http')
        return replaced.geturl()
    return url

def callUrl(url, username, password, method='GET'):
    if not username:
        if method == 'POST':
            return requests.post(url)
        elif method == 'GET':
            return requests.get(url)
    else:
        s = requests.Session()
        s.auth = (username, password)
        if method == 'POST':
            return s.post(url)
        elif method == 'GET':
            return s.get(url)
    
def deletePostKey(post, theKey):
    mutable = post._mutable
    post._mutable = True
    del post[theKey]
    post._mutable = mutable
    return post
# __BEGIN_LICENSE__
# Copyright (c) 2015, United States Government, as represented by the
# Administrator of the National Aeronautics and Space Administration.
# All rights reserved.
#
# The xGDS platform is licensed under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# http://www.apache.org/licenses/LICENSE-2.0.
#
# Unless required by applicable law or agreed to in writing, software distributed
# under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
# CONDITIONS OF ANY KIND, either express or implied. See the License for the
# specific language governing permissions and limitations under the License.
# __END_LICENSE__

from dateutil.relativedelta import relativedelta
from django.utils import timezone
import urlparse
import requests


def get100Years():
    theNow = timezone.now() + relativedelta(years=100)
    return theNow

def addPort(url, port, http=True):
    ''' Add a port to a url '''
    if port:
        parsed = urlparse.urlparse(url)
        replaced = parsed._replace(netloc="{}:{}".format(parsed.hostname, port))
        if http:
            replaced = replaced._replace(scheme='http')
        return replaced.geturl()
    return url

def callUrl(url, username, password, method='GET'):
    if not username:
        if method == 'POST':
            return requests.post(url)
        elif method == 'GET':
            return requests.get(url)
    else:
        s = requests.Session()
        s.auth = (username, password)
        if method == 'POST':
            return s.post(url)
        elif method == 'GET':
            return s.get(url)
    
def deletePostKey(post, theKey):
    mutable = post._mutable
    if theKey in post:
        post._mutable = True
        del post[theKey]
        post._mutable = mutable
    return post

check for key before removing from dict