Skip to content

Commit 65954a7

Browse files
authored
Merge pull request #121 from ns1/PENG-3135/add_redirect
Add new redirect APIs
2 parents 30587f8 + 4d8e2ee commit 65954a7

File tree

5 files changed

+777
-1
lines changed

5 files changed

+777
-1
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
## 0.20.0 (June 19th, 2024)
2+
3+
ENHANCEMENTS:
4+
* Adds support for Redirects
5+
16
## 0.19.1 (May 15th, 2024)
27

38
BUG FIXES:

ns1/__init__.py

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
#
66
from .config import Config
77

8-
version = "0.19.1"
8+
version = "0.20.0"
99

1010

1111
class NS1:
@@ -302,6 +302,26 @@ def datasets(self):
302302

303303
return ns1.rest.datasets.Datasets(self.config)
304304

305+
def redirects(self):
306+
"""
307+
Return a new raw REST interface to Redirect resources
308+
309+
:rtype: :py:class:`ns1.rest.redirect.Redirects`
310+
"""
311+
import ns1.rest.redirect
312+
313+
return ns1.rest.redirect.Redirects(self.config)
314+
315+
def redirect_certificates(self):
316+
"""
317+
Return a new raw REST interface to RedirectCertificate resources
318+
319+
:rtype: :py:class:`ns1.rest.redirect.RedirectCertificates`
320+
"""
321+
import ns1.rest.redirect
322+
323+
return ns1.rest.redirect.RedirectCertificates(self.config)
324+
305325
# HIGH LEVEL INTERFACE
306326
def loadZone(self, zone, callback=None, errback=None):
307327
"""

ns1/redirect.py

Lines changed: 270 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,270 @@
1+
from ns1.rest.redirect import Redirects
2+
from ns1.rest.redirect import RedirectCertificates
3+
4+
5+
class RedirectException(Exception):
6+
pass
7+
8+
9+
class Redirect(object):
10+
"""
11+
High level object representing a redirect.
12+
"""
13+
14+
def __init__(self, config):
15+
"""
16+
Create a new high level Redirect object
17+
18+
:param ns1.config.Config config: config object
19+
"""
20+
self._rest = Redirects(config)
21+
self.config = config
22+
self.data = None
23+
24+
def __repr__(self):
25+
return "<Redirect [%s:%s]=%s>" % (
26+
self.__getitem__("domain"),
27+
self.__getitem__("path"),
28+
self.__getitem__("target"),
29+
)
30+
31+
def __getitem__(self, item):
32+
if not self.data:
33+
raise RedirectException("redirect not loaded")
34+
return self.data.get(item, None)
35+
36+
def reload(self, callback=None, errback=None):
37+
"""
38+
Reload redirect data from the API.
39+
"""
40+
return self.load(reload=True, callback=callback, errback=errback)
41+
42+
def load(self, id=None, callback=None, errback=None, reload=False):
43+
"""
44+
Load redirect data from the API.
45+
:param str id: redirect id to load
46+
"""
47+
if not reload and self.data:
48+
raise RedirectException("redirect already loaded")
49+
if id is None and self.data:
50+
id = self.__getitem__("id")
51+
if id is None:
52+
raise RedirectException("no redirect id: did you mean to create?")
53+
54+
def success(result, *args):
55+
self.data = result
56+
if callback:
57+
return callback(self)
58+
else:
59+
return self
60+
61+
return self._rest.retrieve(id, callback=success, errback=errback)
62+
63+
def loadFromDict(self, cfg):
64+
"""
65+
Load redirect data from a dictionary.
66+
:param dict cfg: dictionary containing *at least* either an id or domain/path/target
67+
"""
68+
if "id" in cfg or (
69+
"domain" in cfg and "path" in cfg and "target" in cfg
70+
):
71+
self.data = cfg
72+
return self
73+
else:
74+
raise RedirectException("insufficient configuration")
75+
76+
def delete(self, callback=None, errback=None):
77+
"""
78+
Delete the redirect.
79+
"""
80+
id = self.__getitem__("id")
81+
return self._rest.delete(id, callback=callback, errback=errback)
82+
83+
def update(self, callback=None, errback=None, **kwargs):
84+
"""
85+
Update redirect configuration. Pass a list of keywords and their values to
86+
update. For the list of keywords available for zone configuration, see
87+
:attr:`ns1.rest.redirect.Redirects.INT_FIELDS` and
88+
:attr:`ns1.rest.redirect.Redirects.PASSTHRU_FIELDS`
89+
"""
90+
if not self.data:
91+
raise RedirectException("redirect not loaded")
92+
93+
def success(result, *args):
94+
self.data = result
95+
if callback:
96+
return callback(self)
97+
else:
98+
return self
99+
100+
return self._rest.update(
101+
self.data, callback=success, errback=errback, **kwargs
102+
)
103+
104+
def create(
105+
self, domain, path, target, callback=None, errback=None, **kwargs
106+
):
107+
"""
108+
Create a new redirect. Pass a list of keywords and their values to
109+
configure. For the list of keywords available for zone configuration,
110+
see :attr:`ns1.rest.redirect.Redirects.INT_FIELDS` and
111+
:attr:`ns1.rest.redirect.Redirects.PASSTHRU_FIELDS`
112+
:param str domain: the domain to redirect from
113+
:param str path: the path on the domain to redirect from
114+
:param str target: the url to redirect to
115+
"""
116+
if self.data:
117+
raise RedirectException("redirect already loaded")
118+
119+
def success(result, *args):
120+
self.data = result
121+
if callback:
122+
return callback(self)
123+
else:
124+
return self
125+
126+
return self._rest.create(
127+
domain, path, target, callback=success, errback=errback, **kwargs
128+
)
129+
130+
def retrieveCertificate(self, callback=None, errback=None):
131+
"""
132+
Retrieve the certificate associated to this redirect.
133+
:return: the RedirectCertificate object
134+
"""
135+
return RedirectCertificate(self.config).load(
136+
self.__getitem__("certificate_id")
137+
)
138+
139+
140+
def listRedirects(config, callback=None, errback=None):
141+
"""
142+
Lists all redirects currently configured.
143+
:return: a list of Redirect objects
144+
"""
145+
146+
def success(result, *args):
147+
ret = []
148+
cfgs = result.get("results", None)
149+
for cfg in cfgs:
150+
ret.append(Redirect(config).loadFromDict(cfg))
151+
if callback:
152+
return callback(ret)
153+
else:
154+
return ret
155+
156+
return Redirects(config).list(callback=success, errback=errback)
157+
158+
159+
class RedirectCertificate(object):
160+
"""
161+
High level object representing a redirect certificate.
162+
"""
163+
164+
def __init__(self, config):
165+
"""
166+
Create a new high level RedirectCertificate object
167+
168+
:param ns1.config.Config config: config object
169+
"""
170+
self._rest = RedirectCertificates(config)
171+
self.config = config
172+
self.data = None
173+
174+
def __repr__(self):
175+
return "<RedirectCertificate %s>" % self.__getitem__("domain")
176+
177+
def __getitem__(self, item):
178+
if not self.data:
179+
raise RedirectException("redirect certificate not loaded")
180+
return self.data.get(item, None)
181+
182+
def reload(self, callback=None, errback=None):
183+
"""
184+
Reload redirect certificate data from the API.
185+
"""
186+
return self.load(reload=True, callback=callback, errback=errback)
187+
188+
def load(self, id=None, callback=None, errback=None, reload=False):
189+
"""
190+
Load redirect certificate data from the API.
191+
:param str id: redirect certificate id to load
192+
"""
193+
if not reload and self.data:
194+
raise RedirectException("redirect certificate already loaded")
195+
if id is None and self.data:
196+
id = self.__getitem__("id")
197+
if id is None:
198+
raise RedirectException(
199+
"no redirect certificate id: did you mean to create?"
200+
)
201+
202+
def success(result, *args):
203+
self.data = result
204+
if callback:
205+
return callback(self)
206+
else:
207+
return self
208+
209+
return self._rest.retrieve(id, callback=success, errback=errback)
210+
211+
def loadFromDict(self, cfg):
212+
"""
213+
Load redirect data from a dictionary.
214+
:param dict cfg: dictionary containing *at least* either an id or a domain
215+
"""
216+
if "id" in cfg or "domain" in cfg:
217+
self.data = cfg
218+
return self
219+
else:
220+
raise RedirectException("insufficient configuration")
221+
222+
def delete(self, callback=None, errback=None):
223+
"""
224+
Requests to revoke the redirect certificate.
225+
"""
226+
id = self.__getitem__("id")
227+
return self._rest.delete(id, callback=callback, errback=errback)
228+
229+
def update(self, callback=None, errback=None):
230+
"""
231+
Requests to renew the redirect certificate.
232+
"""
233+
id = self.__getitem__("id")
234+
return self._rest.update(id, callback=callback, errback=errback)
235+
236+
def create(self, domain, callback=None, errback=None):
237+
"""
238+
Request a new redirect certificate.
239+
:param str domain: the domain to issue the certificate for
240+
"""
241+
if self.data:
242+
raise RedirectException("redirect certificate already loaded")
243+
244+
def success(result, *args):
245+
self.data = result
246+
if callback:
247+
return callback(self)
248+
else:
249+
return self
250+
251+
return self._rest.create(domain, callback=success, errback=errback)
252+
253+
254+
def listRedirectCertificates(config, callback=None, errback=None):
255+
"""
256+
Lists all redirects certificates currently configured.
257+
:return: a list of RedirectCertificate objects
258+
"""
259+
260+
def success(result, *args):
261+
ret = []
262+
cfgs = result.get("results", None)
263+
for cfg in cfgs:
264+
ret.append(RedirectCertificate(config).loadFromDict(cfg))
265+
if callback:
266+
return callback(ret)
267+
else:
268+
return ret
269+
270+
return RedirectCertificates(config).list(callback=success, errback=errback)

0 commit comments

Comments
 (0)