Skip to main content

NIC.RU API Python library

Project description

NIC.RU API Python library

This library provides interfaces for managing DNS zones and records for the DNS service NIC.RU (Ru-Center).

Installation

Using pip:

pip install nic-api

If you want to use this module in your project, add nic-api to its dependencies.

Usage

Initialization

To start using the API, you should get an OAuth application login and a password from NIC.RU. Here is the registration page: https://www.nic.ru/manager/oauth.cgi?step=oauth.app_register

Create an instance of nic_api.DnsApi and provide the obtained credentials:

from nic_api import DnsApi
app_login = "your_application_login"
app_password = "your_application_secret"
api = DnsApi(app_login, app_password)

Authentication

Call the DnsApi.get_token() method with the username and the password for your NIC.RU account:

api.get_token(
    username="Your_account/NIC-D",
    password="Your_password",
)

Now you are ready to use the API.

Till the token is valid, you don't need to provide neither client username or password to access the API – just create an instance of the DnsApi class with the same OAuth config, and pass the cached token as token parameter:

api = DnsApi(app_login, app_password, token)

If you have a valid refresh token, you can get a new access token with it:

api.refresh_token(refresh_token)

You can add a callback method to save the token somewhere outside of the DnsApi object. Use the token_updater_clb parameter for that.

Viewing services and DNS zones

In the NIC.RU, DNS zones are located in “services”:

api.services()

Usually there is only one service per account. To view available zones in the service MY_SERVICE call DnsApi.zones():

api.zones("MY_SERVICE")

Always check if there are any uncommitted changes in the zone before making any modifications – your commit would apply all unsaved changes!

Getting DNS records

For viewing or modifying records, you need to specify both service and DNS zone name:

api.records("MY_SERIVCE", "example.com")

Creating a record

To add a record, create an instance of one of the nic_api.models.DNSRecord subclasses, i.e. ARecord:

from nic_api.models import ARecord
record_www = ARecord(name="www", a="8.8.8.8", ttl=3600)

Add this record to the zone and commit the changes:

api.add_record(record_www, "MY_SERVICE", "example.com")
api.commit("MY_SERVICE", "example.com")

Deleting a record

Every record in the zone has an unique ID, and it's accessible via DNSRecord.id property. When you got the ID, pass it to the DnsApi.delete_record() method:

api.delete_record(100000, "MY_SERVICE", "example.com")
api.commit("MY_SERVICE", "example.com")

Do not forget to commit the changes afterwards.

Default service and zone

The service and zone parameters can be optional in all DnsApi methods, if you set default_service and default_zone properties:

api.default_service = "MY_SERVICE"
api.default_zone = "example.com"

api.delete_record(100000)  # service or zone are not required now
api.commit()               # the same for commit() method

Internationalized domain names

For using IDNs, you need to encode parameters with Punycode before passing them to the classes or methods, with the only exception of idn_name argument. In Python 3.7+ this is done via calling .encode("idna").decode() methods on the str object:

record_idn = ARecord(a="192.168.0.1", name="тест".encode("idna").decode())
api.add_record(record_idn, "MY_SERVICE", "example.com")
api.commit("MY_SERVICE", "example.com")

For working with top-level IDNs, use the same approach:

api.records("MY_SERIVCE", "мой-домен.рф".encode("idna").decode())

# or:
api.default_service = "MY_SERVICE"
api.default_zone = "мой-домен.рф".encode("idna").decode()
api.records()

Note for Python old versions' users

Python 2.7 was EOLed on 2020-01-01, and Python 3.7 is going to be EOLed on 2023-06-27, so the 1.x.x release branch of nic-api is going to only support active Python releases. If you still need old Python version support, please use 0.x.x releases of nic-api. These versions would still receive bugfixes, but no enhancements.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

nic_api-1.1.0.tar.gz (27.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

nic_api-1.1.0-py3-none-any.whl (24.2 kB view details)

Uploaded Python 3

File details

Details for the file nic_api-1.1.0.tar.gz.

File metadata

  • Download URL: nic_api-1.1.0.tar.gz
  • Upload date:
  • Size: 27.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.9

File hashes

Hashes for nic_api-1.1.0.tar.gz
Algorithm Hash digest
SHA256 5922616a912fed1e3f7e645a90d6da6465b2846e40e7c4c016ad6f9a1813aeaa
MD5 05787f3dec6203eb04ebcd0cd278a6f9
BLAKE2b-256 8cba98642f54e88dd9c971330b4d7444f638a594b3d8e52b29a68caf1791d0a1

See more details on using hashes here.

File details

Details for the file nic_api-1.1.0-py3-none-any.whl.

File metadata

  • Download URL: nic_api-1.1.0-py3-none-any.whl
  • Upload date:
  • Size: 24.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.9

File hashes

Hashes for nic_api-1.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 64be4d480b58bbba55c1c0b149a850eba092d06bc6df9bb70d68094862846a1b
MD5 e054995ef7864334a7c1cc30db118b36
BLAKE2b-256 99c184a41cfa23502cc4e6b56685b6ff7958083809d74acb11326a621829dbe4

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page