Skip to content
Snippets Groups Projects
Commit 79f335c3 authored by magnus2142's avatar magnus2142
Browse files

added env data to be use din endpoints

parents 9da8a05b 9352c2e6
No related branches found
No related tags found
No related merge requests found
# IDATT2900-072 # IDATT2900-072
useful stuff
to run the tests: to run the tests:
python -m pytest tests `python -m pytest tests`
python -m bbcli --version ## To download the project and install all the requirements
\ No newline at end of file ### 1. git clone project:
**With SSH:**
`git clone git@gitlab.stud.idi.ntnu.no:mattiaae/idatt2900-072.git`
`cd idatt2900-072`
**With HTTPS:**
`git clone https://gitlab.stud.idi.ntnu.no/mattiaae/idatt2900-072.git`
`cd idatt2900-072`
### 2. Install the virtual environement inside the project folder
`python -m venv ./venv` **OR** `python3 -m venv ./venv`
`source ./venv/bin/activate`
### 3. Install all the requirements
`python -m pip install -r requirements.txt`
### 4. To chech that it is successfully installed, check the version
`python -m bbcli --version`
### 5. Run the tests
`python -m pytest test`
import requests import requests
#from requests.auth import HTTPBasicAuth #from requests.auth import HTTPBasicAuth
import json import json
import pprint import pprint
import typer import typer
#from string_builder import StringBuilder #from string_builder import StringBuilder
import click import click
...@@ -34,22 +34,39 @@ def get_user(user_name: str = typer.Argument('', help='Name of the user'))-> Non ...@@ -34,22 +34,39 @@ def get_user(user_name: str = typer.Argument('', help='Name of the user'))-> Non
cookies=cookies cookies=cookies
) )
data = x.json() data = x.json()['results'][0]
# print(data) # typer.echo(data)
print(pprint.pprint(data)) fn = data['name']['given']
sn = data['name']['family']
id = data['studentId']
typer.echo(f'Name of the student: {fn} {sn}')
typer.echo(f'The student id: {id}')
@app.command(name='get-course') @app.command(name='get-course')
def get_course(course_id: str = 'IDATT2900'): def get_course(course_id: str = typer.Argument('', help='Id of the course')):
''' '''
Get the course Get the course
''' '''
url = base_url + 'courses?courseId=%s' % course_id if course_id == '':
course_id = typer.prompt("What is the course id?")
url = f'{base_url}courses?courseId={course_id}'
x = requests.get( x = requests.get(
url, url,
cookies=cookies) cookies=cookies)
data = x.json() data = x.json()['results'][0]
print(pprint.pprint(data)) name = data['name']
course_url = data['externalAccessUrl']
typer.echo(name)
typer.echo(f'URL for the course: {course_url}')
# def open_folder(data, map):
# key = 'hasChildren'
# acc = []
# if key in data and data[key] == True:
# acc.append
@app.command(name='get-course-contents') @app.command(name='get-course-contents')
...@@ -58,35 +75,45 @@ def get_course_contents(course_id: str = '_27251_1'): ...@@ -58,35 +75,45 @@ def get_course_contents(course_id: str = '_27251_1'):
Get the course contents Get the course contents
''' '''
url = f'{base_url}courses/{course_id}/contents' url = f'{base_url}courses/{course_id}/contents'
typer.echo(url)
x = requests.get(url, cookies=cookies) x = requests.get(url, cookies=cookies)
data = x.json() data = x.json()['results']
print(pprint.pprint(data)) typer.echo('Mapper:')
map = dict()
for i in range(len(data)):
title = data[i]['title']
map[i+1] = data[i]['id']
typer.echo(f'{i+1} {title}')
# idx = typer.prompt("Open a folder by pressing a number: ")
typer.echo(map)
# for d in data:
# typer.echo(d['title'])
def get_children(d, url,acc, count: int = 0): def get_children(d, url,acc, count: int = 0):
#count = count + 1 #count = count + 1
#print(f'kommer hit: {count}') #typer.echo(f'kommer hit: {count}')
key = 'hasChildren' key = 'hasChildren'
if key not in d or d[key] == False: if key not in d or d[key] == False:
print('nei') typer.echo('nei')
return acc return acc
else: else:
print('ja') typer.echo('ja')
id = d['id'] id = d['id']
url = f'{url}/{id}/children' url = f'{url}/{id}/children'
print(url) typer.echo(url)
response = requests.get(url, cookies = cookies) response = requests.get(url, cookies = cookies)
child = response.json()['results'] child = response.json()['results']
#get_children(child, url, acc+child, count) get_children(child, url, acc+child, count)
return child # return child
def get_children(d, url): def get_children(d, url):
key = 'hasChildren' key = 'hasChildren'
while key in d and d[key] == True: while key in d and d[key] == True:
id = d['id'] id = d['id']
url = f'{url}/{id}/children' url = f'{url}/{id}/children'
print() typer.echo()
print(url) typer.echo(url)
print() typer.echo()
response = requests.get(url, cookies=cookies) response = requests.get(url, cookies=cookies)
child = response.json()['results'] child = response.json()['results']
return child return child
...@@ -103,8 +130,10 @@ def get_assignments(course_id: str = typer.Argument('_27251_1', help='The course ...@@ -103,8 +130,10 @@ def get_assignments(course_id: str = typer.Argument('_27251_1', help='The course
data = x.json()['results'] data = x.json()['results']
#res = get_children(data[2], url, []) #res = get_children(data[2], url, [])
res = get_children(data[2], url) res = get_children(data[2], url)
print(pprint.pprint(res)) #typer.echo(ptyper.echo.ptyper.echo(res))
#print(pprint.pprint(data)) for o in res:
typer.echo(o['title'])
#typer.echo(ptyper.echo.ptyper.echo(data))
#for d in data: #for d in data:
#print() #typer.echo()
#print(get_children(d, url)) #typer.echo(get_children(d, url))
from typer.testing import CliRunner from typer.testing import CliRunner
from bbcli import __app_name__, __version__, cli, getuser, getcoursecontents from bbcli import __app_name__, __version__, cli, get_user
runner = CliRunner() runner = CliRunner()
...@@ -10,12 +10,10 @@ def test_version(): ...@@ -10,12 +10,10 @@ def test_version():
assert result.exit_code == 0 assert result.exit_code == 0
assert result.output == f"{__app_name__} v{__version__}\n" assert result.output == f"{__app_name__} v{__version__}\n"
#assert f"{__app_name__} v{__version__}\n" in result.stdout assert f"{__app_name__} v{__version__}\n" in result.stdout
def test_endpoint_getuser(): def test_endpoint_getuser():
result = runner.invoke(getuser, input='hanswf') result = runner.invoke(get_user, input='hanswf')
assert not result.exception assert not result.exception
def test_endpoint_getcoursecontents():
res = runner.invoke(getcoursecontents)
assert not res.exception
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment