Skip to content

Commit

Permalink
Refactor and serverless implementation to api gateway
Browse files Browse the repository at this point in the history
  • Loading branch information
NahumDEV committed Mar 1, 2024
1 parent 2634937 commit f38933b
Show file tree
Hide file tree
Showing 11 changed files with 275 additions and 145 deletions.
3 changes: 3 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
USERNAME=
PASSWORD=
API_URL=
40 changes: 40 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# Lambda Deployment Guide

Follow these steps to deploy your Lambda function using the Serverless Framework.

## Prerequisites

- Make sure you have Node.js and npm installed on your machine.
- You need to have an AWS account and obtain your AWS access key and secret.

## Installation Steps

### 1. Install Serverless Framework

```bash
npm install -g serverless
```

### 2. Install Serverless Plugins

```bash
serverless plugin install -n serverless-python-requirements
```

### 3. Configure AWS Credentials

```bash
serverless config credentials --provider aws --key YOUR_KEY --secret YOUR_SECRET
```

### 4. Configure Environment Variables

```bash
cp .env.example .env
```

### 5. Deploy Lambda

```bash
sls deploy
```
Empty file added common/__init__.py
Empty file.
Empty file added common/constants/__init__.py
Empty file.
7 changes: 7 additions & 0 deletions common/constants/environment_variables.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from os import getenv

ENVIRONMENT_VARIABLES = {
"USERNAME": getenv("API_USERNAME"),
"PASSWORD": getenv("API_PASSWORD"),
"API_URL": getenv("API_URL"),
}
36 changes: 36 additions & 0 deletions lambda_handlers/history.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import json
import os

import requests

from common.constants.environment_variables import ENVIRONMENT_VARIABLES

from requests.auth import HTTPBasicAuth


def handle_history(event, context):
# Logic for /servicenow/history
url = f"{ENVIRONMENT_VARIABLES['API_URL']}/sysapproval_approver?sysparm_limit=10"


try:
response = requests.get(
url,
auth=HTTPBasicAuth(ENVIRONMENT_VARIABLES['USERNAME'], ENVIRONMENT_VARIABLES['PASSWORD'])
)
response.raise_for_status() # Raise an exception for bad responses (4xx, 5xx)

data = response.json()
# Process the data as needed
print(data)

return {
'statusCode': 200,
'body': json.dumps(data)
}
except Exception as e:
print(f"Error: {e}")
return {
'statusCode': 500,
'body': json.dumps({'error': str(e)})
}
41 changes: 41 additions & 0 deletions lambda_handlers/item.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import json

import requests
from requests.auth import HTTPBasicAuth

from common.constants.environment_variables import ENVIRONMENT_VARIABLES


def handle_item(event, context):
if 'path' not in event:
return {
'statusCode': 400,
'body': json.dumps({'error': 'Invalid request'})
}
item_id = event['path'].split('/')[-1]

# Logic for /servicenow/item/<item_id>
url = f"{ENVIRONMENT_VARIABLES['API_URL']}/x_rnod_csod_serv_0_service_ops_request/{item_id}"


try:
response = requests.get(
url,
auth=HTTPBasicAuth(ENVIRONMENT_VARIABLES['USERNAME'], ENVIRONMENT_VARIABLES['PASSWORD'])
)
response.raise_for_status() # Raise an exception for bad responses (4xx, 5xx)

data = response.json()
# Process the data as needed
print(data)

return {
'statusCode': 200,
'body': json.dumps(data)
}
except Exception as e:
print(f"Error: {e}")
return {
'statusCode': 500,
'body': json.dumps({'error': str(e)})
}
34 changes: 34 additions & 0 deletions lambda_handlers/items.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import json

import requests
from requests.auth import HTTPBasicAuth

from common.constants.environment_variables import ENVIRONMENT_VARIABLES


def handle_items():
# Logic for /servicenow/items
url = f"{ENVIRONMENT_VARIABLES['API_URL']}/sysapproval_approver?sysparm_limit=10&sysparm_display_value=true"


try:
response = requests.get(
url,
auth=HTTPBasicAuth(ENVIRONMENT_VARIABLES['USERNAME'], ENVIRONMENT_VARIABLES['PASSWORD'])
)
response.raise_for_status() # Raise an exception for bad responses (4xx, 5xx)

data = response.json()
# Process the data as needed
print(data)

return {
'statusCode': 200,
'body': json.dumps(data)
}
except Exception as e:
print(f"Error: {e}")
return {
'statusCode': 500,
'body': json.dumps({'error': str(e)})
}
42 changes: 42 additions & 0 deletions lambda_handlers/patch_items.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import json

import requests
from requests.auth import HTTPBasicAuth

from common.constants.environment_variables import ENVIRONMENT_VARIABLES


def handle_patch_item(event, context):
# Logic for handling PATCH on /servicenow/item/<item_id>
if 'path' not in event or 'body' not in event:
return {
'statusCode': 400,
'body': json.dumps({'error': 'Invalid request'})
}
item_id = event['path'].split('/')[-1]
patch_data = event['body']
url = f"{ENVIRONMENT_VARIABLES['API_URL']}/sysapproval_approver/{item_id}"

try:
response = requests.patch(
url,
auth=HTTPBasicAuth(ENVIRONMENT_VARIABLES['USERNAME'], ENVIRONMENT_VARIABLES['PASSWORD']),
headers={'Content-Type': 'application/json'},
data=patch_data
)
response.raise_for_status() # Raise an exception for bad responses (4xx, 5xx)

patched_data = response.json()
# Process the patched data as needed
print(patched_data)

return {
'statusCode': 200,
'body': json.dumps(patched_data)
}
except Exception as e:
print(f"Error: {e}")
return {
'statusCode': 500,
'body': json.dumps({'error': str(e)})
}
145 changes: 0 additions & 145 deletions main.py

This file was deleted.

Loading

0 comments on commit f38933b

Please sign in to comment.