Skip to content

grepper-python pull request #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 41 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
41 commits
Select commit Hold shift + click to select a range
0ddbba5
initial commit
CantCode023 Apr 3, 2023
50ea955
added Grepper to __init__ for importing
CantCode023 Mar 3, 2024
208af5d
removed line 83 which i had for debugging but forgot to remove from t…
CantCode023 Mar 3, 2024
0eb8e8c
added fetch_answer to retrieve an answer by id
CantCode023 Mar 3, 2024
6dc0461
Added update_answer to update an answer with specified id.
CantCode023 Mar 3, 2024
bf656b4
Changed class TestGrepper to Test
CantCode023 Mar 3, 2024
d3c7dbe
added unittest.main() to run tests
CantCode023 Mar 3, 2024
cf542b0
changed from grepper_python.main to from grepper_python
CantCode023 Mar 3, 2024
8bf68c1
added test for fetch_answer
CantCode023 Mar 3, 2024
fdfa092
added unittest for update_answer
CantCode023 Mar 3, 2024
edbc4ab
added pip support
CantCode023 Mar 3, 2024
8790f11
removed class VersionInfo
CantCode023 Mar 3, 2024
d79aaa4
deleted unnecessary stuffs and added __all__
CantCode023 Mar 4, 2024
6321d98
added exception base class for raising exceptions
CantCode023 Mar 4, 2024
911ab3f
changed import * to prevent errors
CantCode023 Mar 4, 2024
8c1927c
changed __api_key to _api_key since there is no need for double under…
CantCode023 Mar 4, 2024
540d09e
made the error message more customizable
CantCode023 Mar 4, 2024
5b376ec
changed str(response code) to just response code
CantCode023 Mar 4, 2024
f0004cb
changed for i to for answer for readability
CantCode023 Mar 4, 2024
b50fa04
changed msg from str to any
CantCode023 Mar 4, 2024
3d10b53
optimized raising exception
CantCode023 Mar 4, 2024
9f1966d
added pyproject.toml
CantCode023 Mar 4, 2024
854cd58
updated
CantCode023 Mar 4, 2024
48e4af4
wrote exceptions in a tuple
CantCode023 Mar 4, 2024
376cfb0
changed version to static instead of dynamic
CantCode023 Mar 4, 2024
3d5f2a2
changed exception_handler to support int instead of just string
CantCode023 Mar 4, 2024
389477b
nvm changign back to dynamic
CantCode023 Mar 4, 2024
8e02f5f
changed import * to avoid problems
CantCode023 Mar 4, 2024
e2d649e
removed comments
CantCode023 Mar 4, 2024
9be5d0f
changed from importing all exceptions to only NotFound
CantCode023 Mar 4, 2024
9412220
renamed DocDefaultException to GrepperDefaultException
CantCode023 Mar 4, 2024
8ce2625
changed relative imports to absolute imports
CantCode023 Mar 4, 2024
333203f
updated docs for 2 methods
TanvishGG Jun 21, 2024
0480e8f
added 3rd method docs
TanvishGG Jun 21, 2024
1f8ef9c
Merge pull request #1 from Bharath1910/tanvish
Bharath1910 Jun 21, 2024
10b2884
Commit the sequel.
gaghackz Jun 21, 2024
8665dc4
fixed consistency issues and added better example for search
Bharath1910 Jun 21, 2024
5c76689
fixed consistency issues
Bharath1910 Jun 21, 2024
ada403e
fixed spacing
Bharath1910 Jun 21, 2024
0f0190a
Merge pull request #1 from Bharath1910/mybranch
CantCode023 Jun 21, 2024
fd8b118
Update README.md
CantCode023 Jun 21, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License

Copyright (c) 2010-2023 Grepper, Inc. (https://www.grepper.com)

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
150 changes: 148 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,148 @@
# grepper-python
python client library
# Grepper Python Client
The Grepper Python library provides convenient access to the Grepper API from applications written in the Python language.

## Requirements
Python 3.7 and later.

## PIP
```
pip install grepper-python
```

## Manual Installation
```bash
git clone https://github.com/CantCode023/grepper-python
cd grepper-python
python setup.py install
```


# Usage

## Initialising a new Grepper Class
The following code initialises a new `Grepper` Class.

```py
from grepper_python import Grepper

grepper = Grepper("Your grepper API Key")
```
> Visit [Grepper Account Settings](https://www.grepper.com/app/settings-account.php) to get your API key.


## GrepperAnswer Type Class Reference
```py
class GrepperAnswer:
id: int
content: str
author_name: str
author_profile_url: str
title: str
upvotes: int
downvotes: int
```
Refer the above type class whenever you see `GrepperAnswer`

---
## 1. Search function

This function searches all answers based on a query.

### Required parameters

1. ``query (str, optional)``: Query to search through answer titles.
2. ``similarity (Optional[int], optional)``: How similar the query has to be to the answer title. 1-100 where 1 is really loose matching and 100 is really strict/tight match. Defaults to 60.

### Returned value

list[[GrepperAnswer](#grepperanswer-type-class-reference)]

### Example:

```py
from grepper_python import Grepper

grepper = Grepper("YOUR_API_KEY")
data = grepper.search("git abort command")

# Returns list of GrepperAnswer objects
for answer in data:
print(answer.id)
print(answer.content)
print(answer.author_name)
print(answer.author_profile_url)
print(answer.title)
print(answer.upvotes)
print(answer.downvotes)
```

### Output

```
674394
git merge --abort
git rebase --abort
# if you are still into some command on git and got back #
Magnificent Moose
https://www.grepper.com/profile/mou-biswas
abort a git command
0
0
```
---

## 2. fetch_answer
This function returns an answer specified by the id.

### Required parameters
- `id (int, required)`: The id for the specified answer. ex: 504956.
### Result
fetch_answer returns `GrepperAnswer` type class on successful search.

### Examples
```py
grepper = Grepper("YOUR_API_KEY")
answer = grepper.fetch_answer(504956)
print(answer)
```
### Output
```py
{
"id": 504956,
"content": "var arr=[1,2,3];\narr.reverse().forEach(x=> console.log(x))",
"author_name": "Yanislav Ivanov",
"author_profile_url": "https://www.grepper.com/profile/yanislav-ivanov-r2lfrl14s6xy",
"title": "js loop array back",
"upvotes": 2,
"object": "answer",
"downvotes": 2
}
```
---

## 3. update_answer
This function updates/edits the answer of specified id.
- `NOTE:` This endpoint is in progress and not yet available according to grepper API docs.

### Required parameters
- `id (int, required)`: The id for the specified answer. ex: "504956 ".
- `answer (str, required)`: The answer you want it to update to. ex "new answer content here".

### Result
returns a `Dict`

### Example
```py
grepper = Grepper("YOUR_API_KEY")
result = grepper.update_answer(id=504956,answer="The new edited answer")
print(result)
```

### Output
```py
{
id: 2,
success: "true"
}
```
---
26 changes: 26 additions & 0 deletions grepper_python/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
"""
Python Grepper API
~~~~~~~~~~~~~~~~~~~
An API wrapper for the Grepper API.
"""

__title__ = "grepper-python"
__author__ = "CodeGrepper"
__license__ = "MIT"
__copyright__ = "Copyright 2010-2023 Grepper, Inc."
__version__ = "0.0.1a"

from grepper_python.answer import GrepperAnswer
from grepper_python.main import Grepper
from grepper_python.exceptions import (
BadRequest,
Unauthorized,
Forbidden,
NotFound,
MethodNotAllowed,
TooManyRequests,
InternalServerError,
ServiceUnavailable
)

__all__ = ["GrepperAnswer", "Grepper", "BadRequest", "Unauthorized", "Forbidden", "NotFound", "MethodNotAllowed", "TooManyRequests", "InternalServerError", "ServiceUnavailable"]
12 changes: 12 additions & 0 deletions grepper_python/answer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
from dataclasses import dataclass


@dataclass
class GrepperAnswer:
id: int
content: str
author_name: str
author_profile_url: str
title: str
upvotes: int
downvotes: int
48 changes: 48 additions & 0 deletions grepper_python/exceptions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
"""
grepper-python.exceptions
~~~~~~~~~~~~~~~~~~~
This module contains the set of grepper-python's exceptions.
"""

class GrepperDefaultException(Exception):
def __init__(self, msg=None, *args, **kwargs):
super().__init__(msg or self.__doc__, *args, **kwargs)

# 400
class BadRequest(GrepperDefaultException):
"""Your request is invalid."""


# 401
class Unauthorized(GrepperDefaultException):
"""Your API key is wrong."""


# 403
class Forbidden(GrepperDefaultException):
"""You do not have access to the requested resource."""


# 404
class NotFound(GrepperDefaultException):
"""The specified enpoint could not be found."""


# 405
class MethodNotAllowed(GrepperDefaultException):
"""You tried to access an enpoint with an invalid method."""


# 429
class TooManyRequests(GrepperDefaultException):
"""You're making too many requests! Slow down!"""


# 500
class InternalServerError(GrepperDefaultException):
"""We had a problem with our server. Try again later."""


# 503
class ServiceUnavailable(GrepperDefaultException):
"""We're temporarily offline for maintenance. Please try again later."""
Loading