跳转到主要内容

与 NearlyFreeSpeech 的 API 交互

项目描述

python-nfsn

https://travis-ci.org/ktdreyer/python-nfsn.svg?branch=master https://coveralls.io/repos/ktdreyer/python-nfsn/badge.svg?branch=master&service=github https://badge.fury.io/py/python-nfsn.svg

一个用于访问 NearlyFreeSpeech.net API 的现代 Python 库。

  • 干净的、Pythonic API

  • 支持NFSN API的100%(截至“今天”:

  • 支持Python 2.6至3.5

  • 良好的测试覆盖率(使用 httpretty)

  • 使用 random.SystemRandom() 生成加密安全的盐

安装

要快速运行,请从PyPI安装

pip install python-nfsn

这将在您的 $PATH 中放置一个 pynfsn 工具

如果您想修改代码

git clone https://github.com/ktdreyer/python-nfsn
cd python-nfsn
virtualenv venv
. venv/bin/activate
python setup.py develop

快速入门

通过控制面板提交安全支持请求以从NFSN获取API密钥,并将其存储在您家目录中的JSON文件中,如下所示

$ cat ~/.nfsn-api
{ "login": "ktdreyer",  "api-key": "aGVsbG90aGVyZWZyaWVuZA" }

使用 pynfsn 命令行工具,这是一个薄包装器,允许您探索API

$ pynfsn member ktdreyer accounts
[u'D41D-8CD98F00', u'B204-E9800998', u'ECF8-427E6D7F']

$ pynfsn member ktdreyer sites
[u'coolsite', u'anothercoolsite']

$ pynfsn dns example.com listRRs www
[{u'data': u'example.nfshost.com.', u'scope': u'system', u'type': u'CNAME', u'name': u'www', u'ttl': u'600'}]

$ pynfsn dns example.com addRR testing A 192.0.2.2

$ pynfsn dns example.com removeRR testing A 192.0.2.2

或直接在您的代码中使用API

from nfsn import Nfsn

nfsn = Nfsn(login='myusername', api_key='aGVsbG90aGVyZWZyaWVuZA')

rrs = nfsn.dns('example.com').listRRs(name='www')
for rr in rrs:
    print(rr['name']) # eg. 'www'
    print(rr['type']) # eg. 'A'
    print(rr['data']) # eg. '192.0.2.2'

身份验证

有三种方法可以将您的身份验证凭据传递给 Nfsn() 构造函数

  1. 不带参数调用构造函数

    n = Nfsn()

    默认情况下,库将查找包含您的用户名和API密钥的 $HOME/.nfsn-api JSON文件,如下所示

    $ cat ~/.nfsn-api
    { "login": "ktdreyer",  "api-key": "aGVsbG90aGVyZWZyaWVuZA" }

    (顺便说一下,这与Perl NFSN API用于身份验证的相同文件和格式相匹配。)

  2. 使用显式路径调用构造函数以指向API密钥登录文件

    n = Nfsn(login_file='/etc/nfsn-api')

    在此示例中,login_file 应该是一个类似于上面的JSON文件。

  3. 直接指定登录字符串和API密钥字符串。您可以直接跳过JSON登录文件,只需传递您需要的字符串。

    n = Nfsn(login='ktdreyer', api_key='aGVsbG90aGVyZWZyaWVuZA')

如果您没有输入正确的登录和密钥组合,每次使用此库(见下文)访问属性或方法时,NearlyFreeSpeech.net将返回HTTP 401错误,并且此库将引发一个RuntimeError

API示例

有关更多信息,请参阅https://members.nearlyfreespeech.net/wiki/API

账户API

from nfsn import Nfsn

nfsn = Nfsn(login='ktdreyer', api_key='aGVsbG90aGVyZWZyaWVuZA')

# A floating-point value, the balance on the account.
# Example: 9.04
nfsn.account('A1B2-C3D4E5F6').balance

# The friendly, human-readable name for an account.
# Example: "Personal" or "Business"
nfsn.account('A1B2-C3D4E5F6').friendlyName
nfsn.account('A1B2-C3D4E5F6').friendlyName = 'Business'

# The status details for an account.
# Example: { 'color': '#00b000', 'short': 'OK', 'status': 'Ok' }
# (Note: returns an AttrDict)
nfsn.account('A1B2-C3D4E5F6').status

# The sites associated with an account.
# Example: [ 'coolsite', 'anothercoolsite' ]
nfsn.account('A1B2-C3D4E5F6').sites

# Add a new site to an account.
nfsn.account('A1B2-C3D4E5F6').addSite(site='testing')

# Add a new warning to an account.
nfsn.account('A1B2-C3D4E5F6').addWarning(balance=1.23)

# Remove a warning from an account.
nfsn.account('A1B2-C3D4E5F6').removeWarning(balance=1.23)

DNS API

from nfsn import Nfsn

nfsn = Nfsn(login='ktdreyer', api_key='aGVsbG90aGVyZWZyaWVuZA')

# Get or set the expiration value for a DNS zone.
nfsn.dns('example.com').expire # Example: 86400
nfsn.dns('example.com').expire = 86401

# Get the minTTL value for a DNS zone.
# Example: 180
nfsn.dns('example.com').minTTL

# Get the minTTL value for a DNS zone.
# Example: 600
nfsn.dns('example.com').refresh

# Get the retry value for a DNS zone.
# Example: 180
nfsn.dns('example.com').retry

# Get the serial value for a DNS zone.
# Example: 1414129428
nfsn.dns('example.com').serial

# Add a DNS resource record. The name+type must not exist yet.
nfsn.dns('example.com').addRR(
    name = 'testing',
    type = 'A',
    data = '192.0.2.2'
)

# List all DNS resource records:
# (Note: returns an AttrDict)
# Example:
#    [{'data':  '192.0.2.1',
#      'name':  '',
#      'scope': 'member',
#      'ttl':   '3600',
#      'type':  'A'},
#     {'data':  'ns.phx2.nearlyfreespeech.net.',
#      'name':  '',
#      'scope': 'member',
#      'ttl':   '3600',
#      'type':  'NS'}]
nfsn.dns('example.com').listRRs()

# List all DNS resource records for 'www.example.com':
# (Note: returns an AttrDict)
# Example:
#    [{'data':  '192.0.2.1',
#      'name':  'www',
#      'scope': 'member',
#      'ttl':   '3600',
#      'type':  'A'}]
nfsn.dns('example.com').listRRs(name='www')

# Add a DNS resource record.
# The name+type must exist, or Nfsn will raise an an error. You must
# specify all three parameters (name, type, data).
nfsn.dns('example.com').removeRR(
    name = 'testing',
    type = 'A',
    data = '192.0.2.2'
)

电子邮件API

from nfsn import Nfsn

nfsn = Nfsn(login='ktdreyer', api_key='aGVsbG90aGVyZWZyaWVuZA')

# List all email forwarding.
# Example: { 'hello': 'customerservice@example.net'}
# (Note: returns an AttrDict)
nfsn.email('example.com').listForwards()

# Forward all 'hi@example.com' mail to 'h@example.net':
nfsn.email('example.com').setForward(forward='hi', dest_email='h@example.net')
# ... And remove the email forward:
nfsn.email('example.com').removeForward(forward='hi')

会员API

from nfsn import Nfsn

nfsn = Nfsn(login='ktdreyer', api_key='aGVsbG90aGVyZWZyaWVuZA')

# Get a list of all accounts belonging to a member.
# Example: [ 'A1B2-C3D4E5F6' ]
nfsn.member('ktdreyer').accounts

# Get a list of all sites belonging to a member.
# Example: [ 'coolsite', 'anothercoolsite' ]
nfsn.member('ktdreyer').sites

网站API

from nfsn import Nfsn

nfsn = Nfsn(login='ktdreyer', api_key='aGVsbG90aGVyZWZyaWVuZA')

# Add or remove an alias for a site:
nfsn.site('mycoolsite').addAlias(alias='mobile.example.com')
nfsn.site('mycoolsite').removeAlias(alias='mobile.example.com')

类型和错误

注意,由于我们内部使用Beanbag,当我们返回dict值时,它实际上是一个AttrDict。如果您想将值转换为普通字典,您需要使用+运算符。在值前面加上一个+符号,如下所示

rrs = nfsn.dns('example.com').listRRs()
print +rrs

如果您尝试访问一个不存在的属性或方法,NearlyFreeSpeech.net将返回一个HTTP 404 Not Found错误,并且此库将引发一个BeanBagException

项目详细信息


下载文件

下载适合您平台的文件。如果您不确定选择哪个,请了解有关安装包的更多信息。

源分发

python-nfsn-1.1.1.tar.gz (14.7 kB 查看散列)

上传时间

支持者