Skip to content

Commit

Permalink
Merge pull request #29 from loadsmart/add-new-builder-and-field-to-en…
Browse files Browse the repository at this point in the history
…tity

Updates lib to have some changes into the setup and add support for account_json client
  • Loading branch information
Yanhenning authored Jan 4, 2024
2 parents 2653b8c + 30e73ba commit 27061e4
Show file tree
Hide file tree
Showing 11 changed files with 226 additions and 83 deletions.
149 changes: 113 additions & 36 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
@@ -1,64 +1,141 @@
version: 2
tag-pattern: &tag-pattern
only: /^\d+\.\d+\.\d+$/

jobs:
check:
working_directory: ~/repo
docker:
- image: themattrix/tox
version: 2.1

commands:
install-dependencies:
steps:
- checkout

- run:
name: test
command: |
make test-all
pip install tox
jobs:
tests37:
working_directory: /home/circleci/project
docker:
- image: cimg/python:3.7
steps:
- checkout
- install-dependencies
- run:
command: pip install coveralls

command: tox -e py37
- store_test_results:
path: build
tests38:
working_directory: /home/circleci/project
docker:
- image: cimg/python:3.8
steps:
- checkout
- install-dependencies
- run:
name: gen-coverage
command: |
make coverage
command: tox -e py38
- store_test_results:
path: build
tests39:
working_directory: /home/circleci/project
docker:
- image: cimg/python:3.9
steps:
- checkout
- install-dependencies
- run:
name: upload-coverage
command: |
if [ $COVERALLS_REPO_TOKEN ]; then
coveralls
fi
command: tox -e py39
- store_test_results:
path: build
tests310:
working_directory: /home/circleci/project
docker:
- image: cimg/python:3.10
steps:
- checkout
- install-dependencies
- run:
command: tox -e py310
- store_test_results:
path: build
tests311:
working_directory: /home/circleci/project
docker:
- image: cimg/python:3.11
steps:
- checkout
- install-dependencies
- run:
command: tox -e py311
- store_test_results:
path: build
tests312:
working_directory: /home/circleci/project
docker:
- image: cimg/python:3.12
steps:
- checkout
- install-dependencies
- run:
command: tox -e py312
- store_test_results:
path: build
release:
working_directory: /home/circleci/project
docker:
- image: circleci/python:3.7
- image: cimg/python:3.10
steps:
- checkout

- run:
name: release
command: |
make release
command: make release
- persist_to_workspace:
root: .
paths:
- "dist"

workflows:
version: 2
check-all:
main:
jobs:
- check
check-build-release:
jobs:
- check:
- tests37:
context: org-global
filters:
tags:
only: /^\d+\.\d+\.\d+$/
- tests38:
context: org-global
filters:
tags:
only: /^\d+\.\d+\.\d+$/
- tests39:
context: org-global
filters:
tags:
only: /^\d+\.\d+\.\d+$/
- tests310:
context: org-global
filters:
tags:
only: /^\d+\.\d+\.\d+$/
- tests311:
context: org-global
filters:
tags:
only: /^\d+\.\d+\.\d+$/
- tests312:
context: org-global
filters:
branches:
ignore: /.*/
tags:
only: /^\d+\.\d+\.\d+$/
- release:
name: release
context: org-global
requires:
- check
- tests37
- tests38
- tests39
- tests310
- tests311
- tests312
filters:
branches:
ignore: /.*/
tags:
only: /^\d+\.\d+\.\d+$/
only: /^\d+\.\d+\.\d+$/
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ clean-test:
rm -f .coverage
rm -fr htmlcov/

install:
python -m venv .venv
./.venv/bin/python -m pip install -r requirements-dev.txt

test:
python setup.py pytest

Expand Down
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
# Gmail Wrapper

[![CircleCI](https://circleci.com/gh/loadsmart/gmail-wrapper/tree/master.svg?style=svg&circle-token=110f54407b50c79865fe1f9b4352e213bc68504b)](https://circleci.com/gh/loadsmart/gmail-wrapper/tree/master)
[![Coverage Status](https://coveralls.io/repos/github/loadsmart/gmail-wrapper/badge.svg?branch=master)](https://coveralls.io/github/loadsmart/gmail-wrapper?branch=master)

Because scrapping Gmail data doesn't have to be a [pain](https://googleapis.github.io/google-api-python-client/docs/dyn/gmail_v1.html).

Expand Down
4 changes: 4 additions & 0 deletions changelogs/unreleased/add-new-builder-and-field-to-entity.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
description: Create new way to create the gmail client.
type: added
pr_number: 29
36 changes: 33 additions & 3 deletions gmail_wrapper/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from email.mime.text import MIMEText

from google.auth.transport.requests import Request
from google.oauth2 import service_account
from google.oauth2.credentials import Credentials
from googleapiclient import discovery
from googleapiclient.errors import HttpError
Expand All @@ -23,12 +24,23 @@ class GmailClient:
SCOPE_INSERT = "https://www.googleapis.com/auth/gmail.insert"
SCOPE_MODIFY = "https://www.googleapis.com/auth/gmail.modify"
SCOPE_METADATA = "https://www.googleapis.com/auth/gmail.metadata"
SECRETS_STRATEGY = "secrets_json"
ACCOUNT_JSON_STRATEGY = "account_json"

def __init__(self, email, secrets_json_string, scopes=None):
def __init__(self, email, secrets_json_string, scopes=None, client_strategy=None):
self.credentials = None
if client_strategy is None:
client_strategy = GmailClient.SECRETS_STRATEGY
self.email = email
self._client = self._make_client(secrets_json_string, scopes)
self._client = self._make_client(client_strategy)(secrets_json_string, scopes)

def _make_client(self, secrets_json_string, scopes):
def _make_client(self, client_strategy):
return {
GmailClient.SECRETS_STRATEGY: self._make_client_json_string,
GmailClient.ACCOUNT_JSON_STRATEGY: self._make_client_account_json,
}.get(client_strategy)

def _make_client_json_string(self, secrets_json_string, scopes):
google_secrets_data = json.loads(secrets_json_string)["web"]
credentials = Credentials(
None,
Expand All @@ -41,9 +53,27 @@ def _make_client(self, secrets_json_string, scopes):
credentials.refresh(Request())
return discovery.build("gmail", "v1", credentials=credentials)

def _make_client_account_json(self, account_json, scopes):
self._make_credentials(account_json, scopes)
return discovery.build(
"gmail", "v1", credentials=self.credentials, cache_discovery=False
)

def _messages_resource(self):
return self._client.users().messages()

def _make_credentials(self, account_json: dict, scopes: list) -> Credentials:
credentials = service_account.Credentials.from_service_account_info(
account_json, scopes=scopes
)
self.credentials = credentials.with_subject(self.email)
self._refresh_credentials()
return self.credentials

def _refresh_credentials(self) -> None:
if self.credentials and not self.credentials.valid:
self.credentials.refresh(Request())

def _execute(self, executable):
try:
return executable.execute()
Expand Down
4 changes: 4 additions & 0 deletions gmail_wrapper/entities.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ def id(self):
def filename(self):
return self._raw.get("filename")

@property
def mimetype(self):
return self._raw.get("mimeType")

@property
def content(self):
if not self._body.content:
Expand Down
7 changes: 7 additions & 0 deletions requirements-dev.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
-r requirements.txt
pytest==7.3.1
requests==2.31.0
pytest-cov==4.1.0
pytest-mock==3.10.0
pytest-runner==6.0.0
coverage==7.2.7
7 changes: 7 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@

requests==2.31.0
google-api-core==2.10.2
google-api-python-client==1.12.11
google-auth==1.35.0
google-auth-httplib2==0.1.0
googleapis-common-protos==1.59.0
6 changes: 5 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,12 @@
classifiers=[
"Natural Language :: English",
"Programming Language :: Python :: 3",
"Programming Language :: Python :: 3.6",
"Programming Language :: Python :: 3.7",
"Programming Language :: Python :: 3.8",
"Programming Language :: Python :: 3.9",
"Programming Language :: Python :: 3.10",
"Programming Language :: Python :: 3.11",
"Programming Language :: Python :: 3.12",
],
test_suite="tests",
tests_require=test_requirements,
Expand Down
Loading

0 comments on commit 27061e4

Please sign in to comment.