kylebebak / Requester

Powerful, modern HTTP/REST client built on top of the Requests library
https://kylebebak.github.io/Requester/
MIT License
307 stars 10 forks source link

Env variables no longer loaded #20

Closed alexvanacker closed 5 years ago

alexvanacker commented 5 years ago

OS:

 ~/  uname -srm
Linux 4.18.0-14-generic x86_64
 ~/  cat /etc/os-release
NAME="Ubuntu"
VERSION="18.10 (Cosmic Cuttlefish)"
ID=ubuntu
ID_LIKE=debian

Sublime text version:3.1.1, build 3176 Request version: v2.35.0

Ever since the update to 2.35, my env variables are not loaded. This is for both file and local env variables (i.e. even if the variables are in my .pyr file). Example:

###env
auth_token = 'some_token'
host = 'http://localhost:8080/'
default_headers = {'auth': auth_token, 'Content-Type': 'application/json'}
rest_path = host + 'some/path'
###env

get(
    rest_path + 'organization/40/folders',
    headers = default_headers,
    json = {
        "name": "A",
        "parentId": 58
    }
)

Error shown in console:

'NoneType' object has no attribute 'path'
error: PrepareRequest Error: name 'rest_path' is not defined
requests.get(
    rest_path + 'organization/40/folders',
    headers = default_headers,
    json = {
        "name": "A",
        "parentId": 58
    }…
cesuarez commented 5 years ago

I have the same issue. I'm using version v2.36.0 and is not fixed yet. Any news about this?

kylebebak commented 5 years ago

Hey all, sorry I didn't see this until now.

I made some big improvements to how env vars were parsed and the combined env string (from env block + env file) is computed in 2.35.0 and 2.36.0.

Improved merging of env block and env file. Concatenation order of strings parsed from env block and env file determined by relative order of env block and env_file line in requester file.

Also, request history now persists combined env string to history file, which means any request can be re-sent even if original requester file no longer present.

@alexvanacker I broke env var parsing in some cases in 2.35.0, but as of 2.36.0 everything should work. I improved testing of env block parsing in 2.36.0 to make sure this doesn't happen again.

For example, the following works for me:

###env
auth_token = 'some_token'
host = 'https://httpbin.org'
default_headers = {'auth': auth_token, 'Content-Type': 'application/json'}
rest_path = host + '/get'
###env

get(
    rest_path,
    headers = default_headers,
    json = {
        "name": "A",
        "parentId": 58
    }
)

It's basically identical to your example, but it points at httpbin instead of localhost. This works if it's run from an unsaved view or a file that's saved. It also works if it's run from the requester history, even if the original requester file has been deleted.

Please give this a try and let me know if it works.

@cesuarez Again, I don't think this is a problem in 2.36.0. Could you show me your requester file, the request you're trying to run, and the error that's printed to the ST console?

alexvanacker commented 5 years ago

No worries. I tested it with the latest version and it seems to work now :)

Great job!

kylebebak commented 5 years ago

@alexvanacker I'm glad it's working =)

Also, if anyone wants to know how Requester parses env vars and persists them so they can be used from requests being resent from response tabs or from the requester history, read here.

cesuarez commented 5 years ago

@kylebebak Thanks for answering. I'm still having this issue, even in the current 2.37 version. I tried reinstalling and it's still happening.

My code used to work before, even when I didn't change it. I even tried using alexvanacker code that you said it should work well (this is a saved file):

###env
auth_token = 'some_token'
host = 'https://httpbin.org'
default_headers = {'auth': auth_token, 'Content-Type': 'application/json'}
rest_path = host + '/get'
###env

get(
    rest_path,
    headers = default_headers,
    json = {
        "name": "A",
        "parentId": 58
    }
)

When I execute the request, it prompts me this error:

Screenshot from 2019-03-18 11-39-49

Followed by this one:

Screenshot from 2019-03-18 11-43-10

This is my console log output:

error: EnvBlock Error:
'NoneType' object has no attribute 'path'

Open the console to see the full environment string

EnvString:
``
auth_token = 'some_token'
host = 'https://httpbin.org'
default_headers = {'auth': auth_token, 'Content-Type': 'application/json'}
rest_path = host + '/get'
``
error: PrepareRequest Error: name 'rest_path' is not defined
requests.get(
    rest_path,
    headers = default_headers,
    json = {
        "name": "A",
        "parentId": 58
    }
)
kylebebak commented 5 years ago

Hmmm, it looks like the exception is being raised in add_path.py, by this context manager:

import sys
import os

class add_path():
    def __init__(self, *parts):
        self.path = os.path.normpath(os.path.join(*parts))

    def __enter__(self):
        sys.path.insert(0, self.path)

    def __exit__(self, exc_type, exc_value, traceback):
        while True:
            try:
                sys.path.remove(self.path)
            except ValueError:
                break

My guess is self is None in either the enter or exit method of this context manager, but I can't reproduce the error on my platform (OSX 10.12, ST 3197).

What platform are you on? Sublime Text version? OS? This might be a bug in Python3.3 that I can work around, it would just be good to have more info.

kylebebak commented 5 years ago

@cesuarez

I pushed a fix (2.37.1) that should hopefully deal with this problem. Can you check if this works now?

cesuarez commented 5 years ago

@kylebebak it's working now! thanks for the fix

kylebebak commented 5 years ago

Cool Cesar, thanks for bringing this to my attention. Just out of curiosity, if you don't mind, could you share you platform details?

This seems like it's a bug in Python 3.3.6.

cesuarez commented 5 years ago

I'm using Python 3.3.6 indeed. My OS is Ubuntu 18.04

kylebebak commented 5 years ago

Thanks!

YayC commented 5 years ago

Thanks for fixing this!

I was seeing this on MacOS with Requester v2.38.0, Sublime Text build 3207, Python 3.3.6. All I had to do was restart Sublime and it started working again.