1

I need to do some HTTP request based on feature in PyQGIS.

Based on http://zetcode.com/pyqt/qnetworkaccessmanager/ I created this class:

from typing import Callable

from PyQt5.QtNetwork import  QNetworkAccessManager, QNetworkRequest
from PyQt5.QtCore import QCoreApplication, QUrl
from PyQt5.QtGui import QIcon, QPixmap
from qgis.core import QgsMessageLog


class HttpGetPhoto:
    photo_id: str = None
    callback: Callable = None

    def __init__(self, photo_id: str, handleResponseCallback: Callable):
        self.photo_id = photo_id
        self.callback = handleResponseCallback

    def doRequest(self):
        url = f'https://example.net/photos/{self.photo_id}.jpg'
        QgsMessageLog.logMessage(f'Making a request to {url}')
        req = QNetworkRequest(QUrl(url))

        self.nam = QNetworkAccessManager()
        self.nam.finished.connect(self.handleResponse)
        self.nam.get(req)

    def handleResponse(self, reply) -> None:
        #er = reply.error() -> treat error :
        QgsMessageLog.logMessage(f'Err ? {reply.error()}. Reply message : {reply}')
        pixmap = QPixmap()
        pixmap.loadFromData(reply.readAll()) 
        self.callback(img) 

I get the "Making a request to https://example.net/photos/XXX.jpg but I never get the response. am I missing something ?

2

I figured it out. When calling HttpGetPhoto().doRequest() I was not doing anythign with the object after word, so the the Gc deleted it. Simple :-/

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.