=====================================================
Working with private channels using third-party tools
=====================================================

In order to access private channels via third-party tools (such as ``pip``, ``Twine``, or ``R``), you *must* have a resource token. For more information, see :doc:`Tokens <auth_token>`.

Python packages
===============

Twine is a popular tool for uploading Python packages to repositories. For information on setting your username and password using the configuration file or environment variables, see the official `Twine package documentation <https://twine.readthedocs.io/en/latest/#configuration>`_.

.. note:: 
    
    Package Security Manager supports user authentication using *Basic Authentication*. For information on setting your index URL using the configuration file or environment variables, see the official `pip documentation <https://pip.pypa.io/en/stable/user_guide/#configuration>`_. 

.. tab-set:: 

    .. tab-item:: Uploading

        To upload your Python packages using ``Twine``, run the following command:

        .. code-block:: none

            # Replace <FQDN> with your Package Security Manager fully qualified domain name
            # Replace <CHANNEL> with the name of the channel you want to upload your package to
            # Replace <PACKAGE> with the name of the package you are uploading
            # Replace <TOKEN> with a resource token that provides access to the channel 
            twine upload --repository-url http://<FQDN>/api/repo/<CHANNEL> <PACKAGE> -u <TOKEN> -p x-auth-token


        **Example**

        .. code-block:: none

            twine upload --repository-url http://demo-repo.com/api/repo/my-pip-channel six-1.13.0-py2.py3-none-any.whl -u 46e98d3dbf2378bb26e2107867652ee734eb098ea5b96f35 -p x-auth-token

        .. note:: 
            
            If you are receiving a 401 error message, your token is incorrect. 

    .. tab-item:: Installing
        
        To install a ``pip`` package, run the following command:

            .. code-block:: none
                
                # Replace <FQDN> with your Package Security Manager fully qualified domain name
                # Replace <CHANNEL> with the name of the channel that contains your package
                # Replace <PACKAGE> with the name of the package you are installing
                # Replace <TOKEN> with a resource token that provides access to the channel  
                pip install --trusted-host <FQDN> --index-url http://<TOKEN>:x-auth-token@<FQDN>/api/repo/<CHANNEL>/simple <PACKAGE>

        **Example**

            .. code-block:: none

                pip install --trusted-host demo-repo.com --index-url http://46e98d3dbf2378bb26e2107867652ee734eb098ea5b96f35:x-auth-token@demo-repo.com/api/repo/alex-pip/simple six

R packages
==========

There is no standard tool for uploading R packages. Upload your R packages using either the UI or API directly.

.. tab-set:: 

    .. tab-item:: Uploading

        To upload R packages, run the following command:

        .. code-block:: none

            # Replace <FQDN> with your Package Security Manager fully qualified domain name
            # Replace <CHANNEL> with the name of the channel you want to upload your package to
            # Replace <PACKAGE> with the name of the package you are uploading
            # Replace <TOKEN> with a resource token that provides access to the channel
            curl -v -F filetype=cran_source_package -F content=@<PACKAGE> http://<FQDN>/api/repo/<CHANNEL> -H"X-Auth: <TOKEN>"

        **Example**

            .. code-block:: none

                curl -v -F filetype=cran_source_package -F content=@abc_2.1.tar.gz http://demo-repo.com/api/repo/alex-pip -H"X-Auth: 46e98d3dbf2378bb26e2107867652ee734eb098ea5b96f35"

        .. note:: 
    
            Use ``cran_source_package`` for source packages and ``cran_binary_package`` for binary R packages.

    .. tab-item:: Installing

        You can use your domain as a repository URL in the ``install.packages`` function by injecting a token into the path. 
        
        To install an ``R`` package, run the following command:

        .. code-block:: none

            # Replace <FQDN> with your Package Security Manager fully qualified domain name
            # Replace <CHANNEL> with the name of the channel that contains your package
            # Replace <PACKAGE> with the name of the package you are installing
            # Replace <TOKEN> with a resource token that provides access to the channel
            install.packages("<PACKAGE>", repos="http://<FQDN>/api/repo/t/<TOKEN>/<CHANNEL>/cran")

        **Example**

            .. code-block:: none

                install.packages("leaflet", repos="http://demo-repo.com/api/t/46e98d3dbf2378bb26e2107867652ee734eb098ea5b96f35/repo/alex-pip/cran")