79500253

Date: 2025-03-11 09:46:54
Score: 0.5
Natty:
Report link

So I found a solution to this problem. It might not be pretty but it works.

What I did is, I created a class named "JWTAuth" which uses the AuthBase class. When I call the class I pass the token. This way the auth parameter of pysolr will receive a object and not a string, thus it is happy.

class JWTAuth(AuthBase):
    def __init__(self, jwt_token):
        self.jwt_token = jwt_token

    def __call__(self, r):
        r.headers['Authorization'] = f'Bearer {self.jwt_token}'
        return r

async def search(
    skip: int = 0,
    limit: int = 100,
    params: SearchQueryParams = Depends(),
) -> Any:
    """
    Search query.
    """

    zookeeper = pysolr.ZooKeeper("search-zoo1,search-zoo2,search-zoo3")

    solr = pysolr.SolrCloud(zookeeper, "tag", auth=JWTAuth(add-token-here))

    res = solr.search(q=params.query, start=skip, rows=limit)

    return SearchResults(data=res, count=res.hits)
Reasons:
  • Long answer (-0.5):
  • Has code block (-0.5):
  • Self-answer (0.5):
  • Low reputation (1):
Posted by: anvbis