-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
243 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
" | ||
A book | ||
" | ||
Class { | ||
#name : #TLBook, | ||
#superclass : #Object, | ||
#instVars : [ | ||
'id', | ||
'title' | ||
], | ||
#category : #'Tealight-Bookstore-Example-Base' | ||
} | ||
|
||
{ #category : #'instance creation' } | ||
TLBook class >> id: anID title: aString [ | ||
|
||
^ (self new) | ||
id: anID; | ||
title: aString; | ||
yourself | ||
|
||
] | ||
|
||
{ #category : #accessing } | ||
TLBook >> id [ | ||
|
||
^ id | ||
] | ||
|
||
{ #category : #accessing } | ||
TLBook >> id: anObject [ | ||
|
||
id := anObject | ||
] | ||
|
||
{ #category : #printing } | ||
TLBook >> printOn: aStream [ | ||
|
||
aStream | ||
nextPutAll: self id asString; | ||
nextPut: $:; | ||
nextPutAll: self title asString | ||
] | ||
|
||
{ #category : #accessing } | ||
TLBook >> title [ | ||
|
||
^ title | ||
] | ||
|
||
{ #category : #accessing } | ||
TLBook >> title: anObject [ | ||
|
||
title := anObject | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
" | ||
A book store | ||
" | ||
Class { | ||
#name : #TLBookStore, | ||
#superclass : #Object, | ||
#instVars : [ | ||
'books' | ||
], | ||
#classVars : [ | ||
'Default' | ||
], | ||
#category : #'Tealight-Bookstore-Example-Base' | ||
} | ||
|
||
{ #category : #'accessing - instances and variables' } | ||
TLBookStore class >> default [ | ||
<sampleInstance> | ||
|
||
^ Default ifNil: [ Default := self new ] | ||
] | ||
|
||
{ #category : #'class initialization' } | ||
TLBookStore class >> initialize [ | ||
"Give an initial book" | ||
|
||
self default books add: (TLBook id: 1 title: 'Hitchhikers Guide to Tealight') | ||
] | ||
|
||
{ #category : #'class initialization' } | ||
TLBookStore class >> reset [ | ||
<script> | ||
|
||
Default := nil | ||
|
||
] | ||
|
||
{ #category : #accessing } | ||
TLBookStore >> books [ | ||
|
||
^ books ifNil: [ books := OrderedCollection new ] | ||
] | ||
|
||
{ #category : #accessing } | ||
TLBookStore >> books: anObject [ | ||
|
||
books := anObject | ||
] |
139 changes: 139 additions & 0 deletions
139
src/Tealight-Bookstore-Example/TLBookStoreRestAPI.class.st
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,139 @@ | ||
" | ||
A REST API for the book store | ||
openapi: 3.0.3 | ||
info: | ||
title: Tealight Bookstore Example | ||
description: >- | ||
This is a simple example for an OpenAPI description file to be used with | ||
Teapot/Tealight | ||
termsOfService: http://www.pharo.org/community/ | ||
contact: | ||
email: [email protected] | ||
license: | ||
name: MIT License | ||
url: https://opensource.org/license/mit | ||
version: 1.0.0 | ||
externalDocs: | ||
description: Learn about Teapot/Tealight | ||
url: https://github.com/astares/Tealight | ||
servers: | ||
- url: http://localhost:9094/api/v1 | ||
- url: http://localhost/api/v1 | ||
tags: | ||
- name: Common | ||
description: General infos | ||
paths: | ||
/version: | ||
get: | ||
tags: | ||
- Common | ||
summary: Version Info | ||
description: Retrieve the version | ||
operationId: versionInfo | ||
responses: | ||
'200': | ||
description: Successful operation | ||
/books: | ||
get: | ||
tags: | ||
- Common | ||
summary: Book Info | ||
description: Retrieve the collection of books | ||
operationId: bookRetrieval | ||
responses: | ||
'200': | ||
description: Successful operation | ||
/book: | ||
post: | ||
summary: store book | ||
description: Storing a book | ||
operationId: storeBook | ||
requestBody: | ||
required: true | ||
content: | ||
application/json: | ||
schema: | ||
type: object | ||
properties: | ||
id: | ||
type: integer | ||
title: | ||
type: string | ||
responses: | ||
default: | ||
description: Default error sample response | ||
tags: | ||
- Common | ||
" | ||
Class { | ||
#name : #TLBookStoreRestAPI, | ||
#superclass : #Object, | ||
#category : #'Tealight-Bookstore-Example-API' | ||
} | ||
|
||
{ #category : #common } | ||
TLBookStoreRestAPI class >> book: aRequest [ | ||
<REST_API: 'POST' versions: #('v1') pattern: 'book'> | ||
|
||
|dic newBook| | ||
dic := STON fromString: aRequest entity contents. | ||
newBook := TLBook id: (dic at: #id) asInteger title: (dic at: #title). | ||
TLBookStore default books add: newBook. | ||
^ TeaResponse ok | ||
|
||
] | ||
|
||
{ #category : #common } | ||
TLBookStoreRestAPI class >> books: aRequest [ | ||
<REST_API: 'GET' versions: #('v1') pattern: 'books'> | ||
|
||
^ self jsonResponse: self jsonForBooks | ||
] | ||
|
||
{ #category : #utilities } | ||
TLBookStoreRestAPI class >> generateAPI [ | ||
<script> | ||
|
||
TLRESTAPIBuilder buildAPI. | ||
TLWebserver teapot. | ||
TLWebserver start | ||
|
||
] | ||
|
||
{ #category : #'private - books' } | ||
TLBookStoreRestAPI class >> jsonForBook: each [ | ||
"Return a JSON string for the given book. Typically one would use NeoJSON" | ||
|
||
^ '\{ id: "{1}", title: "{2}" \}"' format: (Array with: each id with: each title) | ||
] | ||
|
||
{ #category : #'private - books' } | ||
TLBookStoreRestAPI class >> jsonForBooks [ | ||
"Write manually a JSON for the books. Typically one would use NeoJSON." | ||
<sampleInstance> | ||
|
||
^ String streamContents: [ :s | | ||
s nextPut: $[. | ||
TLBookStore default books | ||
do: [ :each | s nextPutAll: (self jsonForBook: each) ] | ||
separatedBy: [ s nextPut: $, ]. | ||
s nextPut: $] ] | ||
] | ||
|
||
{ #category : #private } | ||
TLBookStoreRestAPI class >> jsonResponse: aJsonString [ | ||
|
||
^ ZnResponse new | ||
statusLine: ZnStatusLine ok; | ||
entity: (ZnEntity json: aJsonString); | ||
yourself | ||
] | ||
|
||
{ #category : #common } | ||
TLBookStoreRestAPI class >> version: aRequest [ | ||
<REST_API: 'GET' versions: #('v1') pattern: 'version'> | ||
|
||
^ self jsonResponse: '{ version: "1.0.0" }' | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Package { #name : #'Tealight-Bookstore-Example' } |