From 02072f944e9ca2e09ffe8b01c8c36e4c09e086ad Mon Sep 17 00:00:00 2001 From: Ygor Lemos Date: Mon, 7 Sep 2020 22:04:29 -0300 Subject: [PATCH] Support for providing the key as a bytes (#2) * Support for providing the key as a bytes closes #1 * update docs with description of the new raw key bytes mode for _key --- README.md | 10 +++++----- docs/index.md | 10 +++++----- mapsnap/__init__.py | 11 +++++++++-- 3 files changed, 19 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index 21fe72e..b3550e9 100644 --- a/README.md +++ b/README.md @@ -104,11 +104,11 @@ Every time you create a new instance of this class, a signed URL is automaticall ##### Default Parameters and Attributes -| Parameter | Type | Description | -| ---------- | ---- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | -| **\_key** | str | The path for the .p8 or .pem file containing your MapKit JS Key downloaded from Apple Developer Portal. **(ex: /opt/myapplekeys/AuthKey_XXXXXXXXXX.p8)** | -| **teamId** | str | Your Apple Developer Team ID (you can view it on https://developer.apple.com after logging in near your company/developer name or by accessing the **Membership** option on the menu) | -| **keyId** | str | This ID is shown to you when you create your key and is generally part of the key file name, for instance if the downloaded key file is AuthKey_XYZ712387.p8 your keyId is XYZ712387. (You can double check it by going to your details on the Apple Developer portal) | +| Parameter | Type | Description | +| ---------- | ------------ | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | +| **\_key** | str or bytes | The path for the .p8 or .pem file containing your MapKit JS Key downloaded from Apple Developer Portal. **(ex: /opt/myapplekeys/AuthKey_XXXXXXXXXX.p8)**
This can also be a **bytes** object containing the raw contents of the key file. | +| **teamId** | str | Your Apple Developer Team ID (you can view it on https://developer.apple.com after logging in near your company/developer name or by accessing the **Membership** option on the menu) | +| **keyId** | str | This ID is shown to you when you create your key and is generally part of the key file name, for instance if the downloaded key file is AuthKey_XYZ712387.p8 your keyId is XYZ712387. (You can double check it by going to your details on the Apple Developer portal) | The rest of the parameters are mapped directly from [Apple Maps Web Snapshots](https://developer.apple.com/documentation/snapshots) documentation and can be used as guided there. diff --git a/docs/index.md b/docs/index.md index 6af40d1..02d80ee 100644 --- a/docs/index.md +++ b/docs/index.md @@ -104,11 +104,11 @@ Every time you create a new instance of this class, a signed URL is automaticall ##### Default Parameters and Attributes -| Parameter | Type | Description | -| ---------- | ---- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | -| **\_key** | str | The path for the .p8 or .pem file containing your MapKit JS Key downloaded from Apple Developer Portal. **(ex: /opt/myapplekeys/AuthKey_XXXXXXXXXX.p8)** | -| **teamId** | str | Your Apple Developer Team ID (you can view it on https://developer.apple.com after logging in near your company/developer name or by accessing the **Membership** option on the menu) | -| **keyId** | str | This ID is shown to you when you create your key and is generally part of the key file name, for instance if the downloaded key file is AuthKey_XYZ712387.p8 your keyId is XYZ712387. (You can double check it by going to your details on the Apple Developer portal) | +| Parameter | Type | Description | +| ---------- | ------------ | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | +| **\_key** | str or bytes | The path for the .p8 or .pem file containing your MapKit JS Key downloaded from Apple Developer Portal. **(ex: /opt/myapplekeys/AuthKey_XXXXXXXXXX.p8)**
This can also be a **bytes** object containing the raw contents of the key file. | +| **teamId** | str | Your Apple Developer Team ID (you can view it on https://developer.apple.com after logging in near your company/developer name or by accessing the **Membership** option on the menu) | +| **keyId** | str | This ID is shown to you when you create your key and is generally part of the key file name, for instance if the downloaded key file is AuthKey_XYZ712387.p8 your keyId is XYZ712387. (You can double check it by going to your details on the Apple Developer portal) | The rest of the parameters are mapped directly from [Apple Maps Web Snapshots](https://developer.apple.com/documentation/snapshots) documentation and can be used as guided there. diff --git a/mapsnap/__init__.py b/mapsnap/__init__.py index 4ec137b..1a39278 100644 --- a/mapsnap/__init__.py +++ b/mapsnap/__init__.py @@ -74,8 +74,15 @@ def __str__(self): def __post_init__(self): """Initialize, Serialize and Sign""" - with open(self._key, "rb") as fp: - self._sk = SigningKey.from_pem(fp.read()) + if isinstance(self._key, bytes): + self._sk = SigningKey.from_pem(self._key) + elif isinstance(self._key, str): + with open(self._key, "rb") as fp: + self._sk = SigningKey.from_pem(fp.read()) + else: + raise ValueError( + "_key must be a str with the path or a bytes object with the raw key" + ) self._qs = self.serialize() self._sig = self.sign()