Skip to content

hosseinpirhadi/go-challenge

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

14 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

GitHub go.mod Go version

Go Challenge: Simple Restful API on AWS

This is a simple Restful API on AWS using the following tech stack:

The API accepts the following JSON requests and produces the corresponding HTTP responses:

Request 1 (HTTP POST)

URL: https://<api-gateway-url>/api/devices

{
  "id": "/devices/id1",
  "deviceModel": "/devicemodels/id1",
  "name": "Sensor",
  "note": "Testing a sensor.",
  "serial": "A020000102"
}

Response 1 - Success

HTTP 201 Created

Response 1 - Failure 1

HTTP 400 Bad Request (If any of the payload fields are missing)

The response body has a descriptive error message for the client to be able to detect the problem.

Response 1 - Failure 2

HTTP 500 Internal Server Error (If any exceptional situation occurs on the server side)

Request 2 (HTTP GET)

URL: https://<api-gateway-url>/api/devices/{id}

Example: https://api123.amazonaws.com/api/devices/id1

Response 2 - Success

HTTP 200 OK

{
  "id": "/devices/id1",
  "deviceModel": "/devicemodels/id1",
  "name": "Sensor",
  "note": "Testing a sensor.",
  "serial": "A020000102"
}

Response 2 - Failure 1

HTTP 404 Not Found (If the request-id does not exist)

Response 2 - Failure 2

HTTP 500 Internal Server Error (If any exceptional situation occurs on the server side)

Usage

Prerequisites

Testing

  • Build the project using sam build command
  • To run the project locally, use sam local start-api command
  • It can be tested using PostMan

testing challenge via postman

Use this link to test project on AWS like above example.

pkg

Package device

variables

Possible errors are listed below.

var (
    ErrorInvalidDeviceData       = "Invalid user data"
    ErrorFailedToFetchRecord     = "Failed to fetch record"
    ErrorFailedToUnmarshalRecord = "Failed to unmarshal record"
    ErrorDeviceAlreadyExists     = "Device already exists"
    ErrorCouldNotMarshalItem     = "Could not marshal item"
    ErrorCouldNotDynamoPutItem   = "Could not put item in dynamo"
    ErrorItemWasNotInTheDataBase = "Item was not in the DataBase"
)

func FetchDevices

Obtain all devices in the database.

func FetchDevices(tableName string, dynaClient dynamodbiface.DynamoDBAPI) (*[]Device, error)

type Device

type Device struct {
    Id          string `json:"id"`
    DeviceModel string `json:"deviceModel"`
    Name        string `json:"name"`
    Note        string `json:"note"`
    Serial      string `json:"serial"`
}

func CreateDevice

Generates a new device with the aforementioned features.

func CreateDevice(req events.APIGatewayProxyRequest, tableName string, dynaClient dynamodbiface.DynamoDBAPI) (
    *Device,
    error,
)

func FetchDevice

Get a particular device from the database.

func FetchDevice(id, tableName string, dynaClient dynamodbiface.DynamoDBAPI) (*Device, error)

Package handlers

Variables

When a user requests a method that does not exist, the error "method not allowed" appears.

var ErrorMethodNotAllowed = "method not allowed"

func CreateDevice

Handle the situation once the user requests the creation of a new device.

func CreateDevice(req events.APIGatewayProxyRequest, tableName string, dynaClient dynamodbiface.DynamoDBAPI) (*events.APIGatewayProxyResponse, error)

func GetDevice

Whenever the user requests a specific device, this method invokes the "FetchDevice" operation.

func GetDevice(req events.APIGatewayProxyRequest, tableName string, dynaClient dynamodbiface.DynamoDBAPI) (*events.APIGatewayProxyResponse, error)

func UnhandledMethod

If a user requests a method that does not exist, an error will be sent to the API response.

func UnhandledMethod() (*events.APIGatewayProxyResponse, error)

type ErrorBody

type ErrorBody struct {
    ErrorMsg *string `json:"error,omitempty"`
}

About

Implementing simple aws-lambda project

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages