This repository has been archived by the owner on Sep 11, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
circleci_api.py
41 lines (34 loc) · 1.5 KB
/
circleci_api.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import requests
import json
import datetime
class CircleciApi:
def __init__(self):
with open('./config.json') as config:
data = json.load(config)
api_token = data["CIRCLECI_API"]
self.base_url = "https://circleci.com/api"
self.headers = {
"Accept": "application/json",
"Circle-Token": api_token}
def get_builds(self, project, limit, filter=None):
if filter:
filter = f"&filter={filter}"
else:
filter = ""
url = f"{self.base_url}/v1.1/project/github/silvercar/{project}?limit={limit}{filter}&shallow=true"
return requests.request("GET", url, headers=self.headers)
def get_workflows(self, project):
url = f"{self.base_url}/v2/insights/github/silvercar/{project}/workflows"
return requests.request("GET", url, headers=self.headers)
# Creates a datetime object given a CircleCI-style datetime string
def format_time(self, datetime_str):
format_string = "%Y-%m-%dT%H:%M:%S.%fZ"
return datetime.datetime.strptime(datetime_str, format_string)
# Demonstrates example JSON response data
def demo(self):
builds = self.get_builds(limit=10, project="mob-api").text
with open('demo/builds.json', 'w') as outfile:
json.dump(json.loads(builds), outfile)
workflows = self.get_workflows(project="mob-api").text
with open('demo/workflows.json', 'w') as outfile:
json.dump(json.loads(workflows), outfile)