o
    mi                     @  s  d Z ddlmZ ddlZddlZddlZddlmZ ddlm	Z	m
Z
mZ ddlZddlmZ ddlmZmZmZmZmZmZmZmZmZmZmZ ddlmZmZmZmZm Z m!Z!m"Z" dd	l#m$Z$m%Z%m&Z& dd
l'm(Z( ddl)m*Z*m+Z+m,Z, ej-e,df Z.ej-e,df Z/ej-e,e+df Z0eddG dd dZ1G dd dZ2G dd dZ3eddG dd dZ4G dd dZ5G dd dZ6G dd dZ7G dd dZ8dS )zRDF4J client module.    )annotationsN)	dataclass)AnyBinaryIOIterable)BNode)RDF4JUnsupportedProtocolErrorRDFLibParserErrorRepositoryAlreadyExistsErrorRepositoryErrorRepositoryNotFoundErrorRepositoryNotHealthyErrorRepositoryResponseFormatErrorTransactionClosedErrorTransactionCommitErrorTransactionPingErrorTransactionRollbackError)build_context_parambuild_infer_param build_sparql_query_accept_headerbuild_spo_paramrdf_payload_to_streamvalidate_graph_namevalidate_no_bnodes)DATASET_DEFAULT_GRAPH_IDDatasetGraph)Result)IdentifiedNodeLiteralURIRefT)frozenc                   @  s"   e Zd ZU dZded< ded< dS )NamespaceListingResultz'RDF4J namespace and prefix name result.strprefix	namespaceN)__name__
__module____qualname____doc____annotations__ r+   r+   U/home/kim/smarthome/.venv/lib/python3.10/site-packages/rdflib/contrib/rdf4j/client.pyr"   -   s   
 r"   c                   @  sb   e Zd ZdZdddZedd	 Zed
d ZdddZdd Z	dddZ
dddZd ddZdS )!RDF4JNamespaceManagerzA namespace manager for RDF4J repositories.

    Parameters:
        identifier: The identifier of the repository.
        http_client: The httpx.Client instance.
    
identifierr#   http_clienthttpx.Clientc                 C  s   || _ || _d S N)_identifier_http_clientselfr.   r/   r+   r+   r,   __init__=   s   
zRDF4JNamespaceManager.__init__c                 C     | j S r1   r3   r5   r+   r+   r,   r/   A      z!RDF4JNamespaceManager.http_clientc                 C  r7   zRepository identifier.r2   r9   r+   r+   r,   r.   E      z RDF4JNamespaceManager.identifierreturnlist[NamespaceListingResult]c              
   C  sz   ddi}| j jd| j d|d}|  z| }|d d }dd	 |D W S  ttfy< } ztd
| d}~ww )a  List all namespace declarations in the repository.

        Returns:
            list[NamespaceListingResult]: List of namespace and prefix name results.

        Raises:
            RepositoryResponseFormatError: If the response format is unrecognized.
        Acceptapplication/sparql-results+json/repositories//namespacesheadersresultsbindingsc                 S  s(   g | ]}t |d  d |d d dqS )r$   valuer%   )r$   r%   )r"   ).0rowr+   r+   r,   
<listcomp>^   s    

z.RDF4JNamespaceManager.list.<locals>.<listcomp>Unrecognised response format: N)r/   getr.   raise_for_statusjsonKeyError
ValueErrorr   r5   rE   responsedatarF   errr+   r+   r,   listJ   s    
zRDF4JNamespaceManager.listc                 C  s.   ddi}| j jd| j d|d}|  dS )z3Clear all namespace declarations in the repository.r@   rA   rB   rC   rD   N)r/   deleter.   rN   )r5   rE   rS   r+   r+   r,   clearh   s   zRDF4JNamespaceManager.clearr$   
str | Nonec              
   C  sz   |st dddi}z| jjd| j d| |d}|  |jW S  tjy< } z|jj	dkr7W Y d}~dS  d}~ww )	zGet the namespace URI for a given prefix.

        Parameters:
            prefix: The prefix to lookup.

        Returns:
            The namespace URI or `None` if not found.
        Prefix cannot be empty.r@   
text/plainrB   /namespaces/rD     N)
rQ   r/   rM   r.   rN   texthttpxHTTPStatusErrorrS   status_code)r5   r$   rE   rS   rU   r+   r+   r,   rM   r   s    	zRDF4JNamespaceManager.getr%   c                 C  sL   |st d|st dddi}| jjd| j d| ||d}|  dS )	a!  Set the namespace URI for a given prefix.

        !!! note
            If the prefix was previously mapped to a different namespace, this will be
            overwritten.

        Parameters:
            prefix: The prefix to set.
            namespace: The namespace URI to set.
        rZ   zNamespace cannot be empty.Content-Typer[   rB   r\   rE   contentN)rQ   r/   putr.   rN   )r5   r$   r%   rE   rS   r+   r+   r,   set   s   zRDF4JNamespaceManager.setc                 C  s2   |st d| jd| j d| }|  dS )z|Remove the namespace declaration for a given prefix.

        Parameters:
            prefix: The prefix to remove.
        rZ   rB   r\   N)rQ   r/   rW   r.   rN   )r5   r$   rS   r+   r+   r,   remove   s   zRDF4JNamespaceManager.removeNr.   r#   r/   r0   )r>   r?   )r$   r#   r>   rY   )r$   r#   r%   r#   )r$   r#   )r&   r'   r(   r)   r6   propertyr/   r.   rV   rX   rM   rf   rg   r+   r+   r+   r,   r-   5   s    






r-   c                   @  sr   e Zd ZdZdddZedd	 Zed
d Zed ddZ	d ddZ
d!ddZd"ddZd"ddZd ddZdS )#GraphStoreManagerzAn RDF4J Graph Store Protocol Client.

    Parameters:
        identifier: The identifier of the repository.
        http_client: The httpx.Client instance.
    r.   r#   r/   r0   c                 C     || _ || _d| _d S )Napplication/n-triples)r2   r3   _content_typer4   r+   r+   r,   r6         
zGraphStoreManager.__init__c                 C  r7   r1   r8   r9   r+   r+   r,   r/      r:   zGraphStoreManager.http_clientc                 C  r7   r;   r<   r9   r+   r+   r,   r.      r=   zGraphStoreManager.identifier
graph_nameURIRef | strc                 C  sB   i }t | tr| tkst | tr| ttkr	 |S t| |d< |S )Ngraph)
isinstancer    r   r#   )ro   paramsr+   r+   r,   _build_graph_name_params   s   	z*GraphStoreManager._build_graph_name_paramsc                 C  s,   d| j  d}t|tr|tkr|d7 }|S )NrB   z/rdf-graphs/servicez?default)r.   rr   r    r   )r5   ro   urlr+   r+   r,   
_build_url   s   zGraphStoreManager._build_urlr>   r   c                 C  sd   |st dt| d| ji}| |pd}| jj| |||d}|  t|dj	|j
| jdS )a}  Fetch all statements in the specified graph.

        Parameters:
            graph_name: The graph name of the graph.

                For the default graph, use
                [`DATASET_DEFAULT_GRAPH_ID`][rdflib.graph.DATASET_DEFAULT_GRAPH_ID].

        Returns:
            A [`Graph`][rdflib.graph.Graph] object containing all statements in the
                graph.
        Graph name must be provided.r@   NrE   rs   )r.   rT   format)rQ   r   rm   rt   r/   rM   rv   rN   r   parser^   )r5   ro   rE   rs   rS   r+   r+   r,   rM      s   
zGraphStoreManager.getrT   str | bytes | BinaryIO | Graphc                 C     |st dt| t|\}}d| ji}| |pd}z| jj| ||||d}|  W |r7|	  dS dS |r@|	  w w )a"  Add statements to the specified graph.

        Parameters:
            graph_name: The graph name of the graph.

                For the default graph, use
                [`DATASET_DEFAULT_GRAPH_ID`][rdflib.graph.DATASET_DEFAULT_GRAPH_ID].

            data: The RDF data to add.
        rw   rb   NrE   rs   rd   )
rQ   r   r   rm   rt   r/   postrv   rN   closer5   ro   rT   streamshould_closerE   rs   rS   r+   r+   r,   add  (   

zGraphStoreManager.addc                 C  r}   )a3  Overwrite statements in the specified graph.

        Parameters:
            graph_name: The graph name of the graph.

                For the default graph, use
                [`DATASET_DEFAULT_GRAPH_ID`][rdflib.graph.DATASET_DEFAULT_GRAPH_ID].

            data: The RDF data to overwrite with.
        rw   rb   Nr~   )
rQ   r   r   rm   rt   r/   re   rv   rN   r   r   r+   r+   r,   	overwrite"  r   zGraphStoreManager.overwritec                 C  sD   |st dt| | |pd}| jj| ||d}|  dS )a   Clear all statements in the specified graph.

        Parameters:
            graph_name: The graph name of the graph.

                For the default graph, use
                [`DATASET_DEFAULT_GRAPH_ID`][rdflib.graph.DATASET_DEFAULT_GRAPH_ID].
        rw   Nrs   )rQ   r   rt   r/   rW   rv   rN   r5   ro   rs   rS   r+   r+   r,   rX   A  s   	zGraphStoreManager.clearNrh   )ro   rp   )ro   rp   r>   r   )ro   rp   rT   r|   )r&   r'   r(   r)   r6   ri   r/   r.   staticmethodrt   rv   rM   r   r   rX   r+   r+   r+   r,   rj      s    





 
rj   c                   @  s>   e Zd ZU dZded< ded< ded< ded< dZd	ed
< dS )RepositoryListingResulta'  RDF4J repository listing result.

    Parameters:
        identifier: Repository identifier.
        uri: Repository URI.
        readable: Whether the repository is readable by the client.
        writable: Whether the repository is writable by the client.
        title: Repository title.
    r#   r.   uriboolreadablewritableNrY   title)r&   r'   r(   r)   r*   r   r+   r+   r+   r,   r   R  s   
 
r   c                   @  s   e Zd ZdZd@ddZedd	 Zed
d ZedAddZedBddZ	dCddZ
dDdEddZedFddZdGd d!ZdGd"d#ZdHd%d&Z					'	dIdJd2d3Z		dKdLd7d8Z			dMdNd9d:Z				dOdPd<d=Zejd>d? ZdS )Q
RepositoryzRDF4J repository client.

    Parameters:
        identifier: The identifier of the repository.
        http_client: The httpx.Client instance.
    r.   r#   r/   r0   c                 C  s   || _ || _d | _d | _d S r1   )r2   r3   _namespace_manager_graph_store_managerr4   r+   r+   r,   r6   m  s   
zRepository.__init__c                 C  r7   r1   r8   r9   r+   r+   r,   r/   s  r:   zRepository.http_clientc                 C  r7   r;   r<   r9   r+   r+   r,   r.   w  r=   zRepository.identifierr>   r-   c                 C      | j du rt| j| j| _ | j S )z%Namespace manager for the repository.N)r   r-   r.   r/   r9   r+   r+   r,   
namespaces|  
   
zRepository.namespacesrj   c                 C  r   )z'Graph store manager for the repository.N)r   rj   r.   r/   r9   r+   r+   r,   graphs  r   zRepository.graphsr   c              
   C  s   ddd}z| j jd| j |dd}|  W dS  tjyG } z!|jjdkr1td	| j d
t	d	| j d|jj d|jj
 d}~ww )a%  Repository health check.

        Returns:
            bool: True if the repository is healthy, otherwise an error is raised.

        Raises:
            RepositoryNotFoundError: If the repository is not found.
            RepositoryNotHealthyError: If the repository is not healthy.
        application/sparql-queryrA   )rb   r@   rB   zASK {}rc   Tr]   Repository  not found.z is not healthy.  - N)r/   r   r2   rN   r_   r`   rS   ra   r   r   r^   )r5   rE   rS   rU   r+   r+   r,   health  s&   zRepository.healthNro   &URIRef | Iterable[URIRef] | str | Noneintc                 C  sD   t | i }t|| | jjd| j d|d}|  | |jS )  The number of statements in the repository or in the specified graph name.

        Parameters:
            graph_name: Graph name(s) to restrict to.

                The default value `None` queries all graphs.

                To query just the default graph, use
                [`DATASET_DEFAULT_GRAPH_ID`][rdflib.graph.DATASET_DEFAULT_GRAPH_ID].

        Returns:
            The number of statements.

        Raises:
            RepositoryResponseFormatError: Fails to parse the repository size.
        rB   z/sizer   )r   r   r/   rM   r.   rN   _to_sizer^   r   r+   r+   r,   size  s   
zRepository.sizer   c              
   C  sN   zt | }|dkr|W S td|  ty& } ztd| |d }~ww )Nr   zInvalid repository size: z!Failed to parse repository size: )r   rQ   r   )r   rH   rU   r+   r+   r,   r     s   zRepository._to_sizequeryc              
   K  s   ddi}t || |s| jjd| j ||d}n| jjd| j |d|i|d}|  ztjt	|j
|jd dd d	W S  ty] } ztd
|jd d| |d}~ww )a  Execute a SPARQL query against the repository.

        !!! note
            A POST request is used by default. If any keyword arguments are provided,
            a GET request is used instead, and the arguments are passed as query parameters.

        Parameters:
            query: The SPARQL query to execute.
            **kwargs: Additional keyword arguments to include as query parameters
                in the request. See
                [RDF4J REST API - Execute SPARQL query](https://rdf4j.org/documentation/reference/rest-api/#tag/SPARQL/paths/~1repositories~1%7BrepositoryID%7D/get)
                for the list of supported query parameters.
        rb   r   rB   rc   r   rx   ;r   content_type$Failed to parse SPARQL query result : N)r   r/   r   r.   rM   rN   r   r{   ioBytesIOrd   rE   splitrP   r	   )r5   r   kwargsrE   rS   rU   r+   r+   r,   r     s2   



zRepository.queryc                 C  s0   ddi}| j jd| j d||d}|  dS )zExecute a SPARQL update operation on the repository.

        Parameters:
            query: The SPARQL update query to execute.
        rb   zapplication/sparql-updaterB   /statementsrc   N)r/   r   r.   rN   )r5   r   rE   rS   r+   r+   r,   update  s   zRepository.updatelist[IdentifiedNode]c              
   C  s   ddi}| j jd| j d|d}|  z:g }| d d D ],}|d d	 }|d d
 }|dkr:|t| q |dkrF|t| q td| |W S  t	yc } zt
d| |d}~ww )zGet a list of all graph names in the repository.

        Returns:
            A list of graph names.

        Raises:
            RepositoryResponseFormatError: Fails to parse the repository graph names.
        r@   rA   rB   z	/contextsrD   rF   rG   Z	contextIDrH   typer   ZbnodezInvalid graph name type: z(Failed to parse repository graph names: N)r/   rM   r.   rN   rO   appendr    r   rQ   	Exceptionr   )r5   rE   rS   valuesrJ   rH   
value_typerU   r+   r+   r,   graph_names  s2   
zRepository.graph_namesTsubjSubjectTypepredPredicateTypeobj
ObjectTypeinferr   rY   Graph | Datasetc              
   C  s   t |||| |du rd}d|i}i }t|| t|||| t||d | jjd| j d||d}	|	  g d}
z,||
v rJt j	|	j
|d	}n	t j	|	j
|d	}| j D ]}|j|j|jd
d qX|W S  ty{ } ztd| |d}~ww )  Get RDF statements from the repository matching the filtering parameters.

        !!! Note
            The terms for `subj`, `pred`, `obj` or `graph_name` cannot be
            [`BNodes`][rdflib.term.BNode].

        Parameters:
            subj: Subject of the statement to filter by, or `None` to match all.
            pred: Predicate of the statement to filter by, or `None` to match all.
            obj: Object of the statement to filter by, or `None` to match all.
            graph_name: Graph name(s) to restrict to.

                The default value `None` queries all graphs.

                To query just the default graph, use
                [`DATASET_DEFAULT_GRAPH_ID`][rdflib.graph.DATASET_DEFAULT_GRAPH_ID].

            infer: Specifies whether inferred statements should be included in the
                result.
            content_type: The content type of the response.
                A triple-based format returns a [Graph][rdflib.graph.Graph], while a
                quad-based format returns a [`Dataset`][rdflib.graph.Dataset].

        Returns:
            A [`Graph`][rdflib.graph.Graph] or [`Dataset`][rdflib.graph.Dataset] object
                with the repository namespace prefixes bound to it.
        Napplication/n-quadsr@   r   rB   r   rx   rl   text/turtlezapplication/rdf+xmlry   TreplaceError parsing RDF: )r   r   r   r   r/   rM   r.   rN   r   r{   r^   r   r   rV   bindr$   r%   r   r	   r5   r   r   r   ro   r   r   rE   rs   rS   Ztriple_formatsretvalresultrU   r+   r+   r,   rM   &  s4   $
zRepository.getrT   (str | bytes | BinaryIO | Graph | Datasetbase_uric           	      C  sz   t |\}}z.d|pdi}i }|dur||d< | jjd| j d|||d}|  W |r3|  dS dS |r<|  w w )T  Upload and append statements to the repository.

        Parameters:
            data: The RDF data to upload.
            base_uri: The base URI to resolve against for any relative URIs in the data.
            content_type: The content type of the data. Defaults to
                `application/n-quads` when the value is `None`.
        rb   r   NbaseURIrB   r   r~   )r   r/   r   r.   rN   r   	r5   rT   r   r   r   r   rE   rs   rS   r+   r+   r,   uploadi  s$   

zRepository.uploadc           
      C  s   t |\}}t| z3d|pdi}i }t|| |dur ||d< | jjd| j d|||d}	|	  W |r<|  dS dS |rE|  w w )a]  Upload and overwrite statements in the repository.

        Parameters:
            data: The RDF data to upload.
            graph_name: Graph name(s) to restrict to.

                The default value `None` applies to all graphs.

                To apply to just the default graph, use
                [`DATASET_DEFAULT_GRAPH_ID`][rdflib.graph.DATASET_DEFAULT_GRAPH_ID].

            base_uri: The base URI to resolve against for any relative URIs in the data.
            content_type: The content type of the data. Defaults to
                `application/n-quads` when the value is `None`.
        rb   r   Nr   rB   r   r~   )r   r   r   r/   re   r.   rN   r   )
r5   rT   ro   r   r   r   r   rE   rs   rS   r+   r+   r,   r     s(   


zRepository.overwriteNonec                 C  sP   t |||| i }t|| t|||| | jjd| j d|d}|  dS )a  Deletes statements from the repository matching the filtering parameters.

        !!! Note
            The terms for `subj`, `pred`, `obj` or `graph_name` cannot be
            [`BNodes`][rdflib.term.BNode].

        Parameters:
            subj: Subject of the statement to filter by, or `None` to match all.
            pred: Predicate of the statement to filter by, or `None` to match all.
            obj: Object of the statement to filter by, or `None` to match all.
            graph_name: Graph name(s) to restrict to.

                The default value `None` queries all graphs.

                To query just the default graph, use
                [`DATASET_DEFAULT_GRAPH_ID`][rdflib.graph.DATASET_DEFAULT_GRAPH_ID].
        rB   r   r   N)r   r   r   r/   rW   r.   rN   )r5   r   r   r   ro   rs   rS   r+   r+   r,   rW     s   
zRepository.deletec                 c  s8    t | }|V  W d   dS 1 sw   Y  dS )aD  Create a new transaction for the repository.

        !!! warning

        Transaction instances are not thread-safe. Do not share a single
        Transaction instance across multiple threads. Each thread should create
        its own transaction, or use appropriate synchronization if sharing is
        required.
        N)Transactioncreate)r5   Ztxnr+   r+   r,   transaction  s   "zRepository.transactionrh   )r>   r-   )r>   rj   r>   r   r1   )ro   r   r>   r   )r   r#   r   r#   )r>   r   NNNNTNr   r   r   r   r   r   ro   r   r   r   r   rY   r>   r   NNrT   r   r   rY   r   rY   )NNN)rT   r   ro   r   r   rY   r   rY   )NNNN)
r   r   r   r   r   r   ro   r   r>   r   )r&   r'   r(   r)   r6   ri   r/   r.   r   r   r   r   r   r   r   r   r   rM   r   r   rW   
contextlibcontextmanagerr   r+   r+   r+   r,   r   e  sN    





&
#F"+#r   c                   @  s   e Zd ZdZd=ddZdd	 Zd
d Zedd Zedd Z	ed>ddZ
dd Zed?ddZdd Zdd Zdd Zd@dAd!d"ZdBd$d%ZdBd&d'Z		dCdDd-d.Z					/	dEdFd8d9Z		dCdGd;d<ZdS )Hr   a]  An RDF4J transaction.

    !!! warning

        Transaction instances are not thread-safe. Do not share a single
        Transaction instance across multiple threads. Each thread should create
        its own transaction, or use appropriate synchronization if sharing is
        required.

    Parameters:
        repo: The repository instance.
    repor   ru   r#   c                 C  rk   NF)_repo_url_closed)r5   r   ru   r+   r+   r,   r6     rn   zTransaction.__init__c                 C     | S r1   r+   r9   r+   r+   r,   	__enter__     zTransaction.__enter__c                 C  sB   | j s|d u r|   dS z|   W dS  ty   Y dS w dS r   )	is_closedcommitrollbackr   r5   exc_typeexc_valexc_tbr+   r+   r,   __exit__  s   
zTransaction.__exit__c                 C  r7   )zThe repository instance.)r   r9   r+   r+   r,   r     r=   zTransaction.repoc                 C  r7   )zThe transaction URL.)r   r9   r+   r+   r,   ru     r=   zTransaction.urlr>   r   c                 C  r7   )z"Whether the transaction is closed.)r   r9   r+   r+   r,   r     r=   zTransaction.is_closedc                 C  s   | j rtdd S )Nz The transaction has been closed.)r   r   r9   r+   r+   r,   _raise_for_closed  s   zTransaction._raise_for_closedc                 C  s2   |j d|j d}|  |jd }| ||S )zCreate a new transaction for the repository.

        Parameters:
            repo: The repository instance.

        Returns:
            A new Transaction instance.
        rB   z/transactionsZLocation)r/   r   r.   rN   rE   )clsr   rS   ru   r+   r+   r,   r     s   


zTransaction.createc                 C  sP   |    ddi}| jjj| j|d}|jdkr#td|j d|j d| _dS )	zCommit the transaction.

        Raises:
            TransactionCommitError: If the transaction commit fails.
            TransactionClosedError: If the transaction is closed.
        actionZCOMMITr      zTransaction commit failed: r   TN)	r   r   r/   re   ru   ra   r   r^   r   r5   rs   rS   r+   r+   r,   r   *  s   

zTransaction.commitc                 C  sD   |    | jj| j}|jdkrtd|j d|j d| _dS )zRoll back the transaction.

        Raises:
            TransactionRollbackError: If the transaction rollback fails.
            TransactionClosedError: If the transaction is closed.
           zTransaction rollback failed: r   TN)	r   r   r/   rW   ru   ra   r   r^   r   r5   rS   r+   r+   r,   r   :  s   

zTransaction.rollbackc                 C  sJ   |    ddi}| jjj| j|d}|jdkr#td|j d|j dS )zPing the transaction.

        Raises:
            RepositoryTransactionPingError: If the transaction ping fails.
            TransactionClosedError: If the transaction is closed.
        r   ZPINGr   r   zTransaction ping failed: r   N)r   r   r/   re   ru   ra   r   r^   r   r+   r+   r,   pingI  s   
zTransaction.pingNro   r   c                 C  sL   |    t| ddi}t|| | jjj| j|d}|  | j|j	S )r   r   ZSIZEr   )
r   r   r   r   r/   re   ru   rN   r   r^   r   r+   r+   r,   r   X  s   
zTransaction.sizer   c              
   K  s   |    i }t|| d|d}| jjj| j|i ||d}|  ztjt	
|j|jd dd dW S  tyR } ztd|jd d	| |d
}~ww )a  Execute a SPARQL query against the repository.

        Parameters:
            query: The SPARQL query to execute.
            **kwargs: Additional keyword arguments to include as query parameters
                in the request. See
                [RDF4J REST API - Execute SPARQL query](https://rdf4j.org/documentation/reference/rest-api/#tag/SPARQL/paths/~1repositories~1%7BrepositoryID%7D/get)
                for the list of supported query parameters.
        ZQUERY)r   r   rx   rb   r   r   r   r   r   N)r   r   r   r/   re   ru   rN   r   r{   r   r   rd   rE   r   rP   r	   rM   )r5   r   r   rE   rs   rS   rU   r+   r+   r,   r   q  s*   



zTransaction.queryc                 K  s:   |    d|d}| jjj| ji ||d}|  dS )a  Execute a SPARQL update operation on the repository.

        Parameters:
            query: The SPARQL update query to execute.
            **kwargs: Additional keyword arguments to include as query parameters
                See [RDF4J REST API - Execute a transaction action](https://rdf4j.org/documentation/reference/rest-api/#tag/Transactions/paths/~1repositories~1%7BrepositoryID%7D~1transactions~1%7BtransactionID%7D/put)
                for the list of supported query parameters.
        ZUPDATE)r   r   r   N)r   r   r/   re   ru   rN   )r5   r   r   rs   rS   r+   r+   r,   r     s   	

zTransaction.updaterT   r   r   rY   r   c           	      C  s   |    t|\}}d|pdi}ddi}|dur||d< z| jjj| j|||d}|  W |r6|  dS dS |r?|  w w )r   rb   r   r   ZADDNr   r~   r   r   r   r/   re   ru   rN   r   r   r+   r+   r,   r     s&   

zTransaction.uploadTr   r   r   r   r   r   r   r   c              
   C  s   |    t|||| |du rd}d|i}ddi}t|| t|||| t||d | jjj| j||d}	|		  g d}
z-||
v rMt
 j|	j|d	}n	t j|	j|d	}| jj D ]}|j|j|jd
d q\|W S  ty } ztd| |d}~ww )r   Nr   r@   r   GETr   rx   r   ry   Tr   r   )r   r   r   r   r   r   r/   re   ru   rN   r   r{   r^   r   r   rV   r   r$   r%   r   r	   r   r+   r+   r,   rM     s6   $
zTransaction.getr   c           	      C  s   |    ddi}t|\}}d|pdi}|dur||d< z| jjj| j|||d}|  W |r6|  dS dS |r?|  w w )a  Delete statements from the repository.

        !!! Note
            This function operates differently to
            [`Repository.delete`][rdflib.contrib.rdf4j.client.Repository.delete]
            as it does not use filter parameters. Instead, it expects a data payload.
            See the notes from
            [graphdb.js#Deleting](https://github.com/Ontotext-AD/graphdb.js?tab=readme-ov-file#deleting-1)
            for more information.

        Parameters:
            data: The RDF data to upload.
            base_uri: The base URI to resolve against for any relative URIs in the data.
            content_type: The content type of the data. Defaults to
                `application/n-quads` when the value is `None`.
        r   DELETErb   r   Nr   r~   r   )	r5   rT   r   r   rs   r   r   rE   rS   r+   r+   r,   rW     s&   

zTransaction.delete)r   r   ru   r#   r   )r   r   r>   r   r1   )ro   r   r   r   r   r   r   )rT   r   r   rY   r   rY   r>   r   )r&   r'   r(   r)   r6   r   r   ri   r   ru   r   r   classmethodr   r   r   r   r   r   r   r   rM   rW   r+   r+   r+   r,   r     sB    




"Gr   c                   @  sR   e Zd ZdZdddZedd Zdd
dZdddZ	ddddZ	dddZ
dS ) RepositoryManagerz|A client to manage server-level repository operations.

    Parameters:
        http_client: The httpx.Client instance.
    r/   r0   c                 C  s
   || _ d S r1   r8   )r5   r/   r+   r+   r,   r6   2  s   
zRepositoryManager.__init__c                 C  r7   r1   r8   r9   r+   r+   r,   r/   5  r:   zRepositoryManager.http_clientr>   list[RepositoryListingResult]c              
   C  sp   ddi}| j jd|d}|  z| }|d d }dd |D W S  ttfy7 } ztd	| d
}~ww )zList all available repositories.

        Returns:
            list[RepositoryListingResult]: List of repository results.

        Raises:
            RepositoryResponseFormatError: If the response format is unrecognized.
        r@   rA   z/repositoriesrD   rF   rG   c                 S  sL   g | ]"}t |d  d |d d |d d |d d |di ddqS )idrH   r   r   r   r   )r.   r   r   r   r   )r   rM   )rI   r   r+   r+   r,   rK   K  s    



z*RepositoryManager.list.<locals>.<listcomp>rL   N)r/   rM   rN   rO   rP   rQ   r   rR   r+   r+   r,   rV   9  s   

zRepositoryManager.listrepository_idr#   r   c                 C  s   t || j}|  |S )a  Get a repository by ID.

        !!! Note
            This performs a health check before returning the repository object.

        Parameters:
            repository_id: The identifier of the repository.

        Returns:
            Repository: The repository instance.

        Raises:
            RepositoryNotFoundError: If the repository is not found.
            RepositoryNotHealthyError: If the repository is not healthy.
        )r   r/   r   )r5   r   r   r+   r+   r,   rM   X  s   zRepositoryManager.getr   rT   r   c              
   C  sn   zd|i}| j jd| ||d}|  | |W S  tjy6 } z|jjdkr1td| d d}~ww )a  Create a new repository.

        Parameters:
            repository_id: The identifier of the repository.
            data: The repository configuration in RDF.
            content_type: The repository configuration content type.

        Raises:
            RepositoryAlreadyExistsError: If the repository already exists.
            RepositoryNotHealthyError: If the repository is not healthy.
        rb   rB   rc   i  r   z already exists.N)	r/   re   rN   rM   r_   r`   rS   ra   r
   )r5   r   rT   r   rE   rS   rU   r+   r+   r,   r   l  s   
zRepositoryManager.creater   c              
   C  s   z&| j d| }|  |jdkr$td| d|j d|j  W d	S  tjyB } z|j	jdkr=t
d| d d	}~ww )
a  Delete a repository.

        Parameters:
            repository_id: The identifier of the repository.

        Raises:
            RepositoryNotFoundError: If the repository is not found.
            RepositoryError: If the repository is not deleted successfully.
        rB   r   z9Unexpected response status code when deleting repository r   r   r]   r   r   N)r/   rW   rN   ra   r   r^   stripr_   r`   rS   r   )r5   r   rS   rU   r+   r+   r,   rW     s   

zRepositoryManager.deleteN)r/   r0   )r>   r   )r   r#   r>   r   )r   )r   r#   rT   r#   r   r#   r>   r   )r   r#   r>   r   )r&   r'   r(   r)   r6   ri   r/   rV   rM   r   rW   r+   r+   r+   r,   r   +  s    



r   c                   @  s`   e Zd ZdZ		ddddZdd Zdd Zedd ZedddZ	ed ddZ
dd ZdS )!RDF4JClienta  RDF4J client.

    This client and its inner management objects perform HTTP requests via
    httpx and may raise httpx-specific exceptions. Errors documented by RDF4J
    in its protocol specification are mapped to specific exceptions in this
    library where applicable. Error mappings are documented on each management
    method. The underlying httpx client is reused across requests, and
    connection pooling is handled automatically by httpx.

    Parameters:
        base_url: The base URL of the RDF4J server.
        auth: Authentication credentials. Can be a tuple (username, password) for
            basic auth, or a string for token-based auth (e.g., "GDB <token>")
            which is added as the Authorization header.
        timeout: Request timeout in seconds or an httpx.Timeout for fine-grained control (default: 30.0).
        kwargs: Additional keyword arguments to pass to the httpx.Client.
    N      >@base_urlr#   authtuple[str, str] | str | Nonetimeoutfloat | httpx.Timeoutr   r   c           	   
   K  s   | ds	|d7 }d }t|tr|}nt|tr&|di }||d< ||d< tjd	|||d|| _t| j	| _
z| j}W n tjyW } z|   td| |d }~ww |dk rh|   td| dd S )
N/rE   Authorization)r   r   r   z"Failed to check protocol version:    zRDF4J server protocol version z2 is not supported. Minimum required version is 12.r+   )endswithrr   tupler#   rM   r_   ZClientr3   r   r/   _repository_managerprotocolRequestErrorr   r   )	r5   r   r   r   r   Z
httpx_authrE   Zprotocol_versionrU   r+   r+   r,   r6     s@   




zRDF4JClient.__init__c                 C  r   r1   r+   r9   r+   r+   r,   r     r   zRDF4JClient.__enter__c                 C  s   |    d S r1   )r   r   r+   r+   r,   r     s   zRDF4JClient.__exit__c                 C  r7   r1   r8   r9   r+   r+   r,   r/     r:   zRDF4JClient.http_clientr>   r   c                 C  r7   )z.Server-level repository management operations.)r  r9   r+   r+   r,   repositories  r=   zRDF4JClient.repositoriesfloatc                 C  s*   | j jdddid}|  t|j S )zhThe RDF4J REST API protocol version.

        Returns:
            The protocol version number.
        z	/protocolr@   r[   rD   )r/   rM   rN   r  r^   r   r   r+   r+   r,   r    s   zRDF4JClient.protocolc                 C  s   | j   dS )z"Close the underlying httpx.Client.N)r/   r   r9   r+   r+   r,   r     s   zRDF4JClient.close)Nr   )r   r#   r   r   r   r  r   r   )r>   r   )r>   r  )r&   r'   r(   r)   r6   r   r   ri   r/   r
  r  r   r+   r+   r+   r,   r     s    #

r   )9r)   
__future__r   r   r   typingtdataclassesr   r   r   r   r_   Zrdflibr   Zrdflib.contrib.rdf4j.exceptionsr   r	   r
   r   r   r   r   r   r   r   r   Zrdflib.contrib.rdf4j.utilr   r   r   r   r   r   r   Zrdflib.graphr   r   r   Zrdflib.queryr   Zrdflib.termr   r   r    Unionr   r   r   r"   r-   rj   r   r   r   r   r   r+   r+   r+   r,   <module>   sB    4$	} !     Jt