Skip to content

Commit

Permalink
Expose SSL verification as env variable and read version from file
Browse files Browse the repository at this point in the history
  • Loading branch information
clarencecastillo committed Jun 18, 2020
1 parent 7e11d45 commit 5858a1f
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 32 deletions.
Binary file not shown.
21 changes: 17 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,28 @@ Resulting synonyms or antonyms will be sequentially listed according to user rat

Use the `init.sh` script to install the required dependencies inside the `src` folder. When you're ready to test or export, copy the entire `src` folder to the existing workflow directory.

## Troubleshooting ##

#### SSL Errors
If you're having SSL issues, try temporarily disabling it by setting the workflow environment variable `ALFRED_PT_SSL_VERIFICATION` to `False`. This will bypass SSL verification as a workaround while waiting for the SSL certificate to be rectified. You can check Power Thesaurus's SSL certificate status [here](https://www.sslshopper.com/ssl-checker.html#hostname=api.powerthesaurus.org). Be sure to set it back to `True` when all's green.

#### Other Errors
For other errors, please open an issue describing how you got the error and together with the logs from `Alfred > Workflows > Debugging Mode` if possible. There's no proper error handling in place yet, so we'll have to troubleshoot things this way for now.


## Related Links ##

[GitHub Repository](https://github.com/clarencecastillo/alfred-powerthesaurus)
[Alfred Forums](https://www.alfredforum.com/topic/10576-powerthesaurus-search/)
[Packal](http://www.packal.org/workflow/powerthesaurus-search)

## Releases ##

There's probably a smarter way to do this, but when preparing a new release, don't forget to:

1. Bump `version` file
2. Bump `user-agent` version inside `src/api.py`
3. Bump the exported workflow following the format `Powerthesaurus-x.x.x.alfredworkflow`
4. Bump `alfredworkflow.version` field in `metadata.json` or just re-export the entire `metadata.json`

2. Bump the exported workflow following the format `Powerthesaurus-x.x.x.alfredworkflow`
3. Bump `alfredworkflow.version` field in `metadata.json` or just re-export the entire `metadata.json` from `Alfred > Workflows`

## Licensing ##

Expand Down
2 changes: 1 addition & 1 deletion metadata.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"readme" : "Search Powerthesaurus synonyms and antonyms from Alfred",
"createdby" : "Clarence Castillo",
"downloadurl" : "",
"version" : "2.1.1",
"version" : "2.1.2",
"bundleid" : "me.clarencecastillo.alfred-powerthesaurus",
"description" : "Search for synonyms and antonyms on powerthesaurus.org",
"name" : "Powerthesaurus",
Expand Down
53 changes: 27 additions & 26 deletions src/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@

class PowerThesaurus:

USER_AGENT = "Alfred-Powerthesaurus/2.1.1"
GQL_THESAURUS_QUERY = "thesaurus_query"
GQL_SEARCH_QUERY = "search_query"
VERSION = open('./version').readline()
GQL_THESAURUS_QUERY = 'thesaurus_query'
GQL_SEARCH_QUERY = 'search_query'
VERIFY_SSL = bool(os.getenv('ALFRED_PT_SSL_VERIFICATION'))

def __init__(self, api_url, web_url, gql_queries_dir="./gql_queries/", pos_file_path="./pos.json", logger=logging):
def __init__(self, api_url, web_url, gql_queries_dir='./gql_queries/', pos_file_path='./pos.json', logger=logging):
self.api_url = api_url
self.web_url = web_url
self.logger = logger
Expand All @@ -22,8 +23,8 @@ def build_url(self, slug, query_type):

def build_request_headers(self):
return {
"user-agent": PowerThesaurus.USER_AGENT,
"content-type": "application/json"
'user-agent': 'Alfred-PowerThesaurus/' + PowerThesaurus.VERSION,
'content-type': 'application/json'
}

def read_pos_mapping(self, file_path):
Expand All @@ -47,31 +48,31 @@ def read_gql_queries(self, dir):

def build_search_query_params(self, query):
return {
"operationName": "SEARCH_QUERY",
"variables": {
"query": query
'operationName': 'SEARCH_QUERY',
'variables': {
'query': query
},
"query": self.gql_queries[PowerThesaurus.GQL_SEARCH_QUERY]
'query': self.gql_queries[PowerThesaurus.GQL_SEARCH_QUERY]
}

def build_thesaurus_query_params(self, term_id, query_type):
return {
"operationName": "THESAURUSES_QUERY",
"variables": {
"list": query_type.upper(),
"termID": term_id,
"sort": {
"field": "RATING",
"direction": "DESC"
'operationName': 'THESAURUSES_QUERY',
'variables': {
'list': query_type.upper(),
'termID': term_id,
'sort': {
'field': 'RATING',
'direction': 'DESC'
},
"limit": 50,
"syllables": None,
"query": None,
"posID": None,
"first": 50,
"after": ""
'limit': 50,
'syllables': None,
'query': None,
'posID': None,
'first': 50,
'after': ''
},
"query": self.gql_queries[PowerThesaurus.GQL_THESAURUS_QUERY]
'query': self.gql_queries[PowerThesaurus.GQL_THESAURUS_QUERY]
}

def parse_thesaurus_query_response(self, response):
Expand All @@ -94,7 +95,7 @@ def thesaurus_query(self, term_id, query_type):
if not term_id:
return []
params = self.build_thesaurus_query_params(term_id, query_type)
r = requests.post(self.api_url, json=params, headers=self.request_headers, verify=False)
r = requests.post(self.api_url, json=params, headers=self.request_headers, verify=PowerThesaurus.VERIFY_SSL)
self.logger.debug('thesaurus_query: {} {}'.format(r.status_code, r.url))
r.raise_for_status()
return self.parse_thesaurus_query_response(r.json())
Expand All @@ -108,7 +109,7 @@ def parse_search_query_response(self, response):

def search_query(self, query):
params = self.build_search_query_params(query)
r = requests.post(self.api_url, json=params, headers=self.request_headers, verify=False)
r = requests.post(self.api_url, json=params, headers=self.request_headers, verify=PowerThesaurus.VERIFY_SSL)
self.logger.debug('search_query: {} {}'.format(r.status_code, r.url))
r.raise_for_status()
return self.parse_search_query_response(r.json())
Expand Down
2 changes: 1 addition & 1 deletion src/version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2.1.1
2.1.2

0 comments on commit 5858a1f

Please sign in to comment.