Jan
Jan

Reputation: 205

How to reset Requests?

Im trying to write a REST Client for a closed API. I call a specific function twice but it only works the first time. It's the same command. If i run the script twice with batch It works. Im suspecting Requests keeps the connection alive or caches data. How do i "reset" Requests ?

base_headers = {"Connection":"keep-alive",
            "User-Agent":user_agent,
            "Accept-Encoding":"gzip",
            "Host":"xxxxxxxxxxx",
            "Content-Type":"application/json; charset=UTF-8"}
global auth
auth = 'xxxxxx'


def create_header(doAuth = True):
    headers = base_headers
    if doAuth:
       headers['Authorization'] = auth
    return headers
def do_get(url):
    return requests.get(url, headers=create_header());


def do_put(url, payload):
    if payload is None:
        return requests.put(url, headers=create_header())

    return requests.put(url, data=payload, headers=create_header())


def upvote(postid):
    return do_put("xxxxxxxxxxxxx" % postid, None).content


def set_pos(longtitude, latitude, place, country="DE"):
    payload = {'xxxxx':'xxxx'}

    json_payload = json.dumps(payload)

    return do_put("xxxxxxxxxxxxxxx",json_payload).content


def do_post(url, payload, doAuth=True):
    return requests.post(url, data=payload, headers=create_header(doAuth=doAuth))

def new_acc(place, latitude, longtitude):
    access = get_access_token(place, latitude, longtitude)
    print access
    global auth
    auth = "Bearer %s" % access['access_token']
    set_pos(longtitude, latitude, place) # Does a POST

for i in range(1,30):
    posts = new_acc(uni['city'], uni['latitude'], uni['longtitude'])
    upvote('xxxxxxxxxxxxx')

Basically the first upvote() goes through every time but every succeding does not.

Upvotes: 0

Views: 2251

Answers (1)

Bobby Russell
Bobby Russell

Reputation: 475

I'm almost positive that keep-alive is not doing this. I would suggest writing some handlers to debug the response if you don't get the expected response code after the request.

Also, might I suggest a slightly different design that avoids global?

class RESTClient(object):

    BASE_HEADERS = {"Connection":"keep-alive",
                    "Accept-Encoding":"gzip",
                    "Host":"xxxxxxxxxxx",
                    "Content-Type":"application/json; charset=UTF-8"}

    def __init__(self, user_agent, auth = None):
        self.headers = dict(self.BASE_HEADERS)
        self.headers['User-Agent'] = user_agent
        self.auth = auth

    def create_header(self):
        headers = dict(self.headers)
        if auth:
            headers['Authorization'] = auth
        return headers

    def do_get(self, url):
        return requests.get(url, headers=self.create_header());


    def do_put(self, url, payload = None):
        if not payload:
            return requests.put(url, headers=self.create_header())
        return requests.put(url, data=payload, headers=self.create_header())


    def upvote(self, postid):
        return do_put("xxxxxxxxxxxxx" % postid).content

    def set_pos(self, longtitude, latitude, place, country="DE"):
        payload = {'xxxxx':'xxxx'}
        json_payload = json.dumps(payload)
        return do_put("xxxxxxxxxxxxxxx",json_payload).content

    def do_post(self, url, payload, do_auth = None):
        return requests.post(url, data=payload, headers=self.create_header())

    def new_acc(self, place, latitude, longtitude):
        access = get_access_token(place, latitude, longtitude)
        print access
        self.auth = "Bearer %s" % access['access_token']
        set_pos(longtitude, latitude, place)

rc = RESTClient('Mozilla/5.0 ... Firefox/38.0', 'my-auth-token')
rc.upvote(post_id)
etc...

It could be that your use of globals is breaking things. Having not run your code, I can't check, but I would avoid using globals and favor tighter control of your state via objects.

EDIT: Given that this answer was accepted by the OP, I am assuming the defect was caused by mutation of globals.

Upvotes: 1

Related Questions