MCPcopy Create free account
hub / github.com/IfcOpenShell/IfcOpenShell / Client

Class Client

src/bsdd/bsdd.py:509–981  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

507
508
509class Client:
510 def __init__(self):
511 self.baseurl = "https://api.bsdd.buildingsmart.org/api/"
512 self.access_token = ""
513 self.refresh_token = ""
514 self.access_token_expires_on = time.time()
515 self.refresh_token_expires_on = time.time()
516 self.auth_endpoint = "https://buildingsmartservices.b2clogin.com/tfp/buildingsmartservices.onmicrosoft.com/b2c_1_signupsignin/oauth2/v2.0/authorize"
517 self.token_endpoint = "https://buildingsmartservices.b2clogin.com/tfp/buildingsmartservices.onmicrosoft.com/b2c_1_signupsignin/oauth2/v2.0/token"
518 self.client_id = "4aba821f-d4ff-498b-a462-c2837dbbba70"
519
520 def get(self, endpoint, params=None, is_auth_required=False):
521 headers = {"User-Agent": "IfcOpenShell.bSDD.py/0.8.0"}
522 if is_auth_required:
523 headers["Authorization"] = "Bearer " + self.get_access_token()
524 return requests.get(f"{self.baseurl}{endpoint}", timeout=10, headers=headers, params=params or None).json()
525
526 def _get_deprecated(self, endpoint, params=None, is_auth_required=False):
527 headers = {"User-Agent": "IfcOpenShell.bSDD.py/0.8.0"}
528 old_baseurl = "https://bs-dd-api-prototype.azurewebsites.net/"
529 if is_auth_required:
530 headers["Authorization"] = "Bearer " + self.get_access_token()
531 return requests.get(f"{old_baseurl}{endpoint}", timeout=10, headers=headers, params=params or None).json()
532
533 def post(self):
534 pass # TODO
535
536 def get_access_token(self):
537 if self.access_token and self.access_token_expires_on > time.time():
538 return self.access_token
539 elif self.refresh_token and self.refresh_token_expires_on > time.time():
540 self.get_new_access_token()
541 else:
542 self.login()
543 return self.access_token
544
545 def login(self):
546 with http.server.HTTPServer(("", 0), OAuthReceiver) as server:
547 state = str(uuid.uuid4())
548 query = urllib.parse.urlencode(
549 {
550 "client_id": self.client_id,
551 "response_type": "code",
552 # offline_access required to get a refresh_token
553 "scope": "https://buildingsmartservices.onmicrosoft.com/api/read offline_access",
554 "state": state,
555 "redirect_uri": f"http://localhost:{server.server_address[1]}",
556 }
557 )
558 webbrowser.open(f"{self.auth_endpoint}?{query}")
559 server.timeout = 75
560 server.state = state
561 server.handle_request()
562 if server.auth_code and server.auth_state == state:
563 self.set_tokens_from_response(
564 requests.post(
565 "https://buildingsmartservices.b2clogin.com/tfp/buildingsmartservices.onmicrosoft.com/b2c_1_signupsignin/oauth2/v2.0/token",
566 params={

Callers 1

test_bsdd.pyFile · 0.90

Calls

no outgoing calls

Tested by

no test coverage detected