Help with using an online API?


Meisam Amini <meisamamini21@...>
 

Hello everyone.

I'm a beginner, so sorry for the ignorant question.

I'm trying to use an online api in a global plugin. It's a simple one,
just a get request and the response is a JSON object that contains all
the necessary info.

I know how to use Requests module, so tried to use it. But after
copying its many dependencies finally I encountered a problem with
http.cookies module not being there.

Nothing I did worked. I figured up that NVDA doesn't have the
cookies.py module in the http package, so I added the path to the Lib
folder of my Python installation to the beginning of sys.path so NVDA
could get the cookies module from there, but no luck.

Then tried to use urllib.request, but it gives me 403 http error for
that URL. Don't know why, it opens just fine in my browser and with
requests module.

I appreciate if someone helps me to either solve the problem with
requests module or do the job using another module.

Thanks in advance.


Noelia Ruiz
 

Hello:

I can provide an example of readFeeds add-on, available at
https://github.com/nvdaes/readFeeds
import urllib.request

...

def refresh(self): # This function gets a document from an URL
userAgent = "UniversalFeedParser/3.3 +http://feedparser.org/" # This
is the user agent, provided to improve the compatibility of feeds
headers = {'User-Agent': userAgent}
req = urllib.request.Request(self._url, None, headers) # This is the
request of the document
response = urllib.request.urlopen(req) # Response when opening the address
try:
self._document = ElementTree.parse(response) # this is used for xml
documents, you will use json, but just as an example
except Exception as e:
raise e

Here is documentation for urllib.request

https://docs.python.org/3/library/urllib.request.html

Hope this helps.

2020-12-15 3:13 GMT+01:00, Meisam Amini <meisamamini21@gmail.com>:

Hello everyone.

I'm a beginner, so sorry for the ignorant question.

I'm trying to use an online api in a global plugin. It's a simple one,
just a get request and the response is a JSON object that contains all
the necessary info.

I know how to use Requests module, so tried to use it. But after
copying its many dependencies finally I encountered a problem with
http.cookies module not being there.

Nothing I did worked. I figured up that NVDA doesn't have the
cookies.py module in the http package, so I added the path to the Lib
folder of my Python installation to the beginning of sys.path so NVDA
could get the cookies module from there, but no luck.

Then tried to use urllib.request, but it gives me 403 http error for
that URL. Don't know why, it opens just fine in my browser and with
requests module.

I appreciate if someone helps me to either solve the problem with
requests module or do the job using another module.

Thanks in advance.






Meisam Amini <meisamamini21@...>
 

Thank you. That's very helpful.


On Tue, Dec 15, 2020 at 11:24 PM Noelia Ruiz <nrm1977@...> wrote:
Hello:

I can provide an example of readFeeds add-on, available at
https://github.com/nvdaes/readFeeds
import urllib.request

...

def refresh(self): # This function gets a document from an URL
                userAgent = "UniversalFeedParser/3.3 +http://feedparser.org/" # This
is the user agent, provided to improve the compatibility of feeds
                headers = {'User-Agent': userAgent}
                req = urllib.request.Request(self._url, None, headers) # This is the
request of the document
                response = urllib.request.urlopen(req) # Response when opening the address
                try:
                        self._document = ElementTree.parse(response) # this is used for xml
documents, you will use json, but just as an example
                except Exception as e:
                        raise e

Here is documentation for urllib.request

https://docs.python.org/3/library/urllib.request.html

Hope this helps.

2020-12-15 3:13 GMT+01:00, Meisam Amini <meisamamini21@...>:
> Hello everyone.
>
> I'm a beginner, so sorry for the ignorant question.
>
> I'm trying to use an online api in a global plugin. It's a simple one,
> just a get request and the response is a JSON object that contains all
> the necessary info.
>
> I know how to use Requests module, so tried to use it. But after
> copying its many dependencies finally I encountered a problem with
> http.cookies module not being there.
>
> Nothing I did worked. I figured up that NVDA doesn't have the
> cookies.py module in the http package, so I added the path to the Lib
> folder of my Python installation to the beginning of sys.path so NVDA
> could get the cookies module from there, but no luck.
>
> Then tried to use urllib.request, but it gives me 403 http error for
> that URL. Don't know why, it opens just fine in my browser and with
> requests module.
>
> I appreciate if someone helps me to either solve the problem with
> requests module or do the job using another module.
>
> Thanks in advance.
>
>
>
>
>
>






Noelia Ruiz
 

Great, thank you.

2020-12-16 5:43 GMT+01:00, Meisam Amini <meisamamini21@gmail.com>:

Thank you. That's very helpful.

On Tue, Dec 15, 2020 at 11:24 PM Noelia Ruiz <nrm1977@gmail.com> wrote:

Hello:

I can provide an example of readFeeds add-on, available at
https://github.com/nvdaes/readFeeds
import urllib.request

...

def refresh(self): # This function gets a document from an URL
userAgent = "UniversalFeedParser/3.3 +
http://feedparser.org/" # This
is the user agent, provided to improve the compatibility of feeds
headers = {'User-Agent': userAgent}
req = urllib.request.Request(self._url, None, headers) #
This is the
request of the document
response = urllib.request.urlopen(req) # Response when
opening the address
try:
self._document = ElementTree.parse(response) #
this is used for xml
documents, you will use json, but just as an example
except Exception as e:
raise e

Here is documentation for urllib.request

https://docs.python.org/3/library/urllib.request.html

Hope this helps.

2020-12-15 3:13 GMT+01:00, Meisam Amini <meisamamini21@gmail.com>:
Hello everyone.

I'm a beginner, so sorry for the ignorant question.

I'm trying to use an online api in a global plugin. It's a simple one,
just a get request and the response is a JSON object that contains all
the necessary info.

I know how to use Requests module, so tried to use it. But after
copying its many dependencies finally I encountered a problem with
http.cookies module not being there.

Nothing I did worked. I figured up that NVDA doesn't have the
cookies.py module in the http package, so I added the path to the Lib
folder of my Python installation to the beginning of sys.path so NVDA
could get the cookies module from there, but no luck.

Then tried to use urllib.request, but it gives me 403 http error for
that URL. Don't know why, it opens just fine in my browser and with
requests module.

I appreciate if someone helps me to either solve the problem with
requests module or do the job using another module.

Thanks in advance.