Skip to content

Commit

Permalink
Add basic HTTP client [feenkcom/gtoolkit#4322]
Browse files Browse the repository at this point in the history
  • Loading branch information
hellerve committed Mar 5, 2025
1 parent 4a72d57 commit 1f4b9b2
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 40 deletions.
6 changes: 6 additions & 0 deletions src/GToolkit-GemStone-GemStone/DateAndTime.extension.st
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
Extension { #name : 'DateAndTime' }

{ #category : '*GToolkit-GemStone-GemStone' }
DateAndTime class >> fromUnixTime: anInteger [
^ self posixSeconds: anInteger offset: Duration new
]
16 changes: 13 additions & 3 deletions src/GToolkit-GemStone-GemStone/Duration.extension.st
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
Extension { #name : #Duration }
Extension { #name : 'Duration' }

{ #category : #'*GToolkit-GemStone-GemStone' }
{ #category : '*GToolkit-GemStone-GemStone' }
Duration class >> nanoSeconds: nanoSeconds [
^ self seconds: nanoSeconds / 1000000
^ self seconds: nanoSeconds / 1000000000
]

{ #category : '*GToolkit-GemStone-GemStone' }
Duration >> asDelay [
^ Delay forSeconds: seconds
]

{ #category : '*GToolkit-GemStone-GemStone' }
Duration >> wait [
^ self asDelay wait
]
91 changes: 63 additions & 28 deletions src/GToolkit-GemStone-GemStone/GtGemstoneHttpClient.class.st
Original file line number Diff line number Diff line change
@@ -1,66 +1,96 @@
Class {
#name : #GtGemstoneHttpClient,
#superclass : #Object,
#name : 'GtGemstoneHttpClient',
#superclass : 'Object',
#instVars : [
'url',
'contents',
'contentType'
'headers',
'query'
],
#category : 'GToolkit-GemStone-GemStone'
}

{ #category : #other }
{ #category : 'other' }
GtGemstoneHttpClient class >> new [
^ self basicNew initialize
]

{ #category : #other }
GtGemstoneHttpClient >> beOneShot [
]

{ #category : #other }
GtGemstoneHttpClient >> contentType [
^ contentType
{ #category : 'other' }
GtGemstoneHttpClient >> authorization: aString [
headers at: 'Authorization' put: aString
]

{ #category : #other }
GtGemstoneHttpClient >> contentType: aString [
contentType := aString
{ #category : 'other' }
GtGemstoneHttpClient >> beOneShot [
]

{ #category : #other }
{ #category : 'other' }
GtGemstoneHttpClient >> contents [
^ contents
]

{ #category : #other }
{ #category : 'other' }
GtGemstoneHttpClient >> contents: aDict [
contents := aDict
]

{ #category : #other }
{ #category : 'other' }
GtGemstoneHttpClient >> contentType: aString [
headers at: 'Content-Type' put: aString
]

{ #category : 'other' }
GtGemstoneHttpClient >> defaultContentType [
^ 'application/json'
]

{ #category : #other }
{ #category : 'other' }
GtGemstoneHttpClient >> entity: aDict [
contents := aDict
]

{ #category : 'other' }
GtGemstoneHttpClient >> get [
^ STONJSON fromString: (self performMethod: 'GET')
]

{ #category : 'other' }
GtGemstoneHttpClient >> headerAt: aKey put: aValue [
headers at: aKey put: aValue
]

{ #category : 'other' }
GtGemstoneHttpClient >> initialize [
super initialize.
headers := Dictionary new.
query := Dictionary new.
self contentType: self defaultContentType
]

{ #category : #other }
{ #category : 'other' }
GtGemstoneHttpClient >> performMethod: aMethod [
| curlArguments |
| callUrl curlArguments |
callUrl := self url , '?'.

query
keysAndValuesDo: [ :aKey :aValue | callUrl := callUrl , aKey , '=' , aValue , '&' ].

callUrl := callUrl allButLast.

curlArguments := {'curl'.
'-s'.
'--post301'.
'-L'.
(self url).
'''', callUrl, ''''.
'-X'.
aMethod.
'-H'.
('''Content-Type: ' , self contentType, '''')} asOrderedCollection.
aMethod} asOrderedCollection.

headers
keysAndValuesDo: [ :aKey :aValue |
curlArguments
addAll:
{'-H'.
('''' , aKey , ': ' , aValue , '''')} ].

self contents
ifNotNil: [ :aContents |
Expand All @@ -72,22 +102,27 @@ GtGemstoneHttpClient >> performMethod: aMethod [
^ System performOnServer: (' ' join: curlArguments)
]

{ #category : #other }
{ #category : 'other' }
GtGemstoneHttpClient >> post [
^ STONJSON fromString: (self performMethod: 'POST')
]

{ #category : #other }
{ #category : 'other' }
GtGemstoneHttpClient >> postStreaming [
^ (Character cr split: (self performMethod: 'POST')) collect: [:aLine | STONJSON fromString: aLine]
]

{ #category : #other }
{ #category : 'other' }
GtGemstoneHttpClient >> queryAt: aKey put: aValue [
query at: aKey put: aValue asString
]

{ #category : 'other' }
GtGemstoneHttpClient >> url [
^ url
]

{ #category : #other }
{ #category : 'other' }
GtGemstoneHttpClient >> url: aString [
url := aString
]
9 changes: 7 additions & 2 deletions src/GToolkit-GemStone-GemStone/Number.extension.st
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
Extension { #name : #Number }
Extension { #name : 'Number' }

{ #category : #'*GToolkit-GemStone-GemStone' }
{ #category : '*GToolkit-GemStone-GemStone' }
Number >> nanoSeconds [
^ Duration nanoSeconds: self
]

{ #category : '*GToolkit-GemStone-GemStone' }
Number >> seconds [
^ Duration seconds: self
]
19 changes: 12 additions & 7 deletions src/GToolkit-GemStone-GemStone/String.extension.st
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
Extension { #name : #String }
Extension { #name : 'String' }

{ #category : #'*GToolkit-GemStone-GemStone' }
{ #category : '*GToolkit-GemStone-GemStone' }
String class >> cr [
^ self with: Character cr
]

{ #category : '*GToolkit-GemStone-GemStone' }
String >> / anotherString [
^ self , '/', anotherString
]

{ #category : #'*GToolkit-GemStone-GemStone' }
String class >> cr [
^ self with: Character cr
{ #category : '*GToolkit-GemStone-GemStone' }
String >> asZnUrl [
^ self
]

{ #category : #'*GToolkit-GemStone-GemStone' }
{ #category : '*GToolkit-GemStone-GemStone' }
String >> repeat: aNumber [
"Returns a new string concatenated by itself repeated n times"
"('abc' repeat: 3) >>> 'abcabcabc'"
Expand All @@ -22,7 +27,7 @@ String >> repeat: aNumber [
1 to: aNumber do: [ :idx | stringStream nextPutAll: self ] ]
]

{ #category : #'*GToolkit-GemStone-GemStone' }
{ #category : '*GToolkit-GemStone-GemStone' }
String >> utf8Encoded [
"Answer a ByteArray of the receiver in UTF8 format"

Expand Down

0 comments on commit 1f4b9b2

Please sign in to comment.