Skip to content
This repository has been archived by the owner on Jul 15, 2022. It is now read-only.

Commit

Permalink
refactoring of purge and purge status (#25)
Browse files Browse the repository at this point in the history
  • Loading branch information
sandromodarelli authored Nov 14, 2019
1 parent 7a153e6 commit bdd0001
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 30 deletions.
39 changes: 14 additions & 25 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,38 +76,26 @@ cdnsite = cdnsite.enable()

### Purge a cdn resource and check the purge status.
```python
purge_result1 = sp.stacks().get(stackid).purge(
url="https://example.com/resource/",
purge_request_id = sp.stacks().get(stackid).purge(
[
{
"url": "https://example.com/resource/", # required
"recursive": True,
"invalidateOnly": False,
"purgeAllDynamic": False,
"headers": [],
"purgeSelector": [],
}
]
)

## Function accepts the arguments shown below with their respective
## default values. See API Doc for more information on options:
## https://developer.stackpath.com/en/api/cdn/#operation/PurgeContent
purge_result2 = sp.stacks().get(stackid).purge(
url="https://example.com/",
recursive = True,
invalidateOnly = False,
purgeAllDynamic = False,
headers = [],
purgeSelector = []

## purge_status can be used to check the status of the requested purge.
## Progress is represented as a decimal between 0 and 1, correlating to a
## percentage.

purge_status_response1 = sd.stacks().get(stackid).purge_status(purge_result1.id)
print(purge_status_response1.progress)
##>> 1

)
## purge_status can be used to check the status of the requested purge.
## Progress is represented as a decimal between 0 and 1, correlating to a
## percentage.

purge_status_response1 = sp.stacks().get(stackid).purge_status(purge_result1.id)
print(purge_status_response1.progress)
progress = sp.stacks().get(stackid).purge_status(purge_request_id)
print(progress)
##>> 1

```

## Get metrics for a stack:
Expand All @@ -130,6 +118,7 @@ start = end - timedelta(days=7)
metrics_response2 = sp.stacks().get(stackid).metrics().get(granularity="PT1H",\
platforms = ["CDO", "CDE"], start_datetime_object = start, end_datetime_object = end)
```

## Retrieve all certificates from a stack:
```python
certificate_response = sp.stacks().get(stackid).certificates()
Expand Down
19 changes: 14 additions & 5 deletions pystackpath/stacks.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,22 +60,31 @@ def cancel(self, reason_slug, reason_text=""):
response.raise_for_status()
return self

def purge(self, items: list):
def purge(self, items: list) -> str:
"""
Purge cached content for all CDN sites on a stack
:param items: The items to purge from the CDN.
:return: The purge request's ID.
"""
data = {
"items": items
}

response = self._client.post("/cdn/v1/stacks/{}/purge".format(self.id), json=data)
response.raise_for_status()

return self.loaddict(response.json())

def purge_status(self, purge_id):
return response.json()["id"]

def purge_status(self, purge_id) -> float:
"""
Retrieve a purge request's status
:param purge_id: The ID of the purge request to check the status of
:return: The purge request's progress, ranging from 0.0 to 1.0.
"""
response = self._client.get(f"/cdn/v1/stacks/{self.id}/purge/{purge_id}")
response.raise_for_status()

return self.loaddict(response.json())
return response.json()["progress"]

def cdnsites(self):
return CdnSites(self._client, self.id)
Expand Down

0 comments on commit bdd0001

Please sign in to comment.