Skip to content

Commit

Permalink
feat(notes): added note_upload_service
Browse files Browse the repository at this point in the history
  • Loading branch information
thaddeauslow committed Feb 22, 2024
1 parent 05e654f commit 06dd47b
Show file tree
Hide file tree
Showing 4,101 changed files with 567,168 additions and 52 deletions.
The diff you're trying to view is too large. We only load the first 3000 changed files.
3 changes: 3 additions & 0 deletions notes/.aws/credentials
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[default]
aws_access_key_id = "AKIAXBWYNHCVA3KMXP4F"
aws_secret_access_key = "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"
Binary file not shown.
Binary file added notes/__pycache__/notes_pb2.cpython-39.pyc
Binary file not shown.
Binary file added notes/__pycache__/notes_pb2_grpc.cpython-39.pyc
Binary file not shown.
38 changes: 38 additions & 0 deletions notes/note_client.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import grpc
import notes_pb2
import notes_pb2_grpc

def upload_note(stub, user_id, filename, file_content):
# Create a request message
request = notes_pb2.UploadNoteRequest(userId=user_id, filename=filename, fileContent=file_content)
# Make the call to the server
response = stub.UploadNote(request)
return response

def retrieve_note(stub, note_id):
# Create a request message
request = notes_pb2.RetrieveNoteRequest(noteId=note_id)
# Make the call to the server
response = stub.RetrieveNote(request)
return response

def run():
# Update with the appropriate server address and port
with grpc.insecure_channel('localhost:50051') as channel:
stub = notes_pb2_grpc.NoteServiceStub(channel) # Updated to NoteServiceStub

# Example usage: upload a note
user_id = '123'
filename = 'example_note.txt'
file_content = b'This is the content of the note.'
upload_response = upload_note(stub, user_id, filename, file_content)
print(f"Uploaded Note ID: {upload_response.noteId}")

# Example usage: retrieve a note
# Note: You need to implement this part on the server side for it to work
note_id = upload_response.noteId
retrieve_response = retrieve_note(stub, note_id)
print(f"Retrieved Note Content: {retrieve_response.fileContent}")

if __name__ == '__main__':
run()
45 changes: 0 additions & 45 deletions notes/note_retrieval_service.py

This file was deleted.

94 changes: 89 additions & 5 deletions notes/note_upload_service.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,100 @@
from concurrent import futures
import uuid
import grpc
import boto3
import mysql.connector
from mysql.connector import Error
import notes_pb2
import notes_pb2_grpc
from concurrent import futures

# Directly specify your AWS credentials (Not recommended for production use)
s3_client = boto3.client(
's3',
aws_access_key_id='AKIAXBWYNHCVA3KMXP4F',
aws_secret_access_key='l2fqR/CFwofX3MhTC79cWjfuvikGDZg47lPRhZv6'
)
bucket_name = 'eduhelper-notes-bucket'

class NoteUploadServiceServicer(notes_pb2_grpc.NoteUploadServiceServicer):
# Database connection
try:
db_conn = mysql.connector.connect(
host="localhost",
user="root",
password="root",
database="my_notes_app"
)
db_cursor = db_conn.cursor()

print("Successfully connected to the database")
except Error as e:
print(f"Error connecting to MySQL database: {e}")

class NoteServiceServicer(notes_pb2_grpc.NoteServiceServicer):
def UploadNote(self, request, context):
# Implement logic to handle the note upload, including saving the file to S3
return notes_pb2.UploadNoteResponse(noteId='generatedNoteId')
# Generate a unique note ID
note_id = str(uuid.uuid4())

# Upload file to S3
s3_object_key = f"{note_id}/{request.filename}"
try:
s3_client.put_object(Bucket=bucket_name, Key=s3_object_key, Body=request.fileContent)
except Exception as e:
context.set_details(f'Error uploading note to S3: {e}')
context.set_code(grpc.StatusCode.INTERNAL)
return notes_pb2.UploadNoteResponse()

# Save note metadata in the database
try:
db_cursor.execute(
"INSERT INTO notes (user_id, filename, note_id) VALUES (%s, %s, %s)",
(request.userId, request.filename, note_id)
)
db_conn.commit()
except Exception as e:
context.set_details(f'Error saving note metadata to the database: {e}')
context.set_code(grpc.StatusCode.INTERNAL)
return notes_pb2.UploadNoteResponse()

# Return the note ID
return notes_pb2.UploadNoteResponse(noteId=note_id)

def RetrieveNote(self, request, context):
# Retrieve note metadata from the database
try:
db_cursor.execute(
"SELECT user_id, filename FROM notes WHERE note_id = %s",
(request.noteId,)
)
note_metadata = db_cursor.fetchone()
if note_metadata:
user_id, filename = note_metadata
else:
context.set_details('Note not found.')
context.set_code(grpc.StatusCode.NOT_FOUND)
return notes_pb2.RetrieveNoteResponse()
except Exception as e:
context.set_details(f'Error querying note metadata from the database: {e}')
context.set_code(grpc.StatusCode.INTERNAL)
return notes_pb2.RetrieveNoteResponse()

# Retrieve note content from S3
s3_object_key = f"{request.noteId}/{filename}"
try:
s3_response = s3_client.get_object(Bucket=bucket_name, Key=s3_object_key)
file_content = s3_response['Body'].read()
except Exception as e:
context.set_details(f'Error retrieving note from S3: {e}')
context.set_code(grpc.StatusCode.INTERNAL)
return notes_pb2.RetrieveNoteResponse()

# Return the retrieved note content
return notes_pb2.RetrieveNoteResponse(userId=user_id, filename=filename, fileContent=file_content)


# Function to add the servicer to the gRPC server
def serve():
server = grpc.server(futures.ThreadPoolExecutor(max_workers=10))
notes_pb2_grpc.add_NoteUploadServiceServicer_to_server(NoteUploadServiceServicer(), server)
notes_pb2_grpc.add_NoteServiceServicer_to_server(NoteServiceServicer(), server)
server.add_insecure_port('[::]:50051')
server.start()
server.wait_for_termination()
Expand Down
35 changes: 34 additions & 1 deletion notes/notes.proto
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,37 @@ message FileMetadata {
int64 pagecount = 2;
int64 filesize = 3;
string locale = 4;
}
}

// Define a service with RPC methods
service NoteService {
// Define an RPC method to upload a note
rpc UploadNote (UploadNoteRequest) returns (UploadNoteResponse);

// Define an RPC method to retrieve a note
rpc RetrieveNote (RetrieveNoteRequest) returns (RetrieveNoteResponse);
}

// Request message for uploading a note
message UploadNoteRequest {
string userId = 1;
string filename = 2;
bytes fileContent = 3;
}

// Response message for uploading a note
message UploadNoteResponse {
string noteId = 1;
}

// Request message for retrieving a note
message RetrieveNoteRequest {
string noteId = 1;
}

// Response message for retrieving a note
message RetrieveNoteResponse {
string userId = 1;
string filename = 2;
bytes fileContent = 3;
}
12 changes: 11 additions & 1 deletion notes/notes_pb2.py

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

100 changes: 100 additions & 0 deletions notes/notes_pb2_grpc.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,103 @@
"""Client and server classes corresponding to protobuf-defined services."""
import grpc

import notes_pb2 as notes__pb2


class NoteServiceStub(object):
"""Define a service with RPC methods
"""

def __init__(self, channel):
"""Constructor.
Args:
channel: A grpc.Channel.
"""
self.UploadNote = channel.unary_unary(
'/NoteService/UploadNote',
request_serializer=notes__pb2.UploadNoteRequest.SerializeToString,
response_deserializer=notes__pb2.UploadNoteResponse.FromString,
)
self.RetrieveNote = channel.unary_unary(
'/NoteService/RetrieveNote',
request_serializer=notes__pb2.RetrieveNoteRequest.SerializeToString,
response_deserializer=notes__pb2.RetrieveNoteResponse.FromString,
)


class NoteServiceServicer(object):
"""Define a service with RPC methods
"""

def UploadNote(self, request, context):
"""Define an RPC method to upload a note
"""
context.set_code(grpc.StatusCode.UNIMPLEMENTED)
context.set_details('Method not implemented!')
raise NotImplementedError('Method not implemented!')

def RetrieveNote(self, request, context):
"""Define an RPC method to retrieve a note
"""
context.set_code(grpc.StatusCode.UNIMPLEMENTED)
context.set_details('Method not implemented!')
raise NotImplementedError('Method not implemented!')


def add_NoteServiceServicer_to_server(servicer, server):
rpc_method_handlers = {
'UploadNote': grpc.unary_unary_rpc_method_handler(
servicer.UploadNote,
request_deserializer=notes__pb2.UploadNoteRequest.FromString,
response_serializer=notes__pb2.UploadNoteResponse.SerializeToString,
),
'RetrieveNote': grpc.unary_unary_rpc_method_handler(
servicer.RetrieveNote,
request_deserializer=notes__pb2.RetrieveNoteRequest.FromString,
response_serializer=notes__pb2.RetrieveNoteResponse.SerializeToString,
),
}
generic_handler = grpc.method_handlers_generic_handler(
'NoteService', rpc_method_handlers)
server.add_generic_rpc_handlers((generic_handler,))


# This class is part of an EXPERIMENTAL API.
class NoteService(object):
"""Define a service with RPC methods
"""

@staticmethod
def UploadNote(request,
target,
options=(),
channel_credentials=None,
call_credentials=None,
insecure=False,
compression=None,
wait_for_ready=None,
timeout=None,
metadata=None):
return grpc.experimental.unary_unary(request, target, '/NoteService/UploadNote',
notes__pb2.UploadNoteRequest.SerializeToString,
notes__pb2.UploadNoteResponse.FromString,
options, channel_credentials,
insecure, call_credentials, compression, wait_for_ready, timeout, metadata)

@staticmethod
def RetrieveNote(request,
target,
options=(),
channel_credentials=None,
call_credentials=None,
insecure=False,
compression=None,
wait_for_ready=None,
timeout=None,
metadata=None):
return grpc.experimental.unary_unary(request, target, '/NoteService/RetrieveNote',
notes__pb2.RetrieveNoteRequest.SerializeToString,
notes__pb2.RetrieveNoteResponse.FromString,
options, channel_credentials,
insecure, call_credentials, compression, wait_for_ready, timeout, metadata)
Loading

0 comments on commit 06dd47b

Please sign in to comment.