-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbasic_usage.rb
More file actions
87 lines (66 loc) · 2.18 KB
/
basic_usage.rb
File metadata and controls
87 lines (66 loc) · 2.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#!/usr/bin/env ruby
require_relative '../lib/servicestack'
# Basic usage example of ServiceStack Ruby Client
puts "ServiceStack Ruby Client - Basic Usage"
puts "=" * 50
# 1. Create a client
puts "\n1. Creating client..."
client = ServiceStack::JsonServiceClient.new('https://api.example.org')
puts " Client created with base URL: #{client.base_url}"
# 2. Configure timeout
puts "\n2. Configuring timeout..."
client.timeout = 30
puts " Timeout set to #{client.timeout} seconds"
# 3. Add authentication
puts "\n3. Adding authentication..."
client.set_bearer_token('your-token-here')
puts " Bearer token configured"
# Alternative: Basic Auth
# client.set_credentials('username', 'password')
# 4. Add custom headers
puts "\n4. Adding custom headers..."
client.headers['X-API-Version'] = '1.0'
client.headers['X-Client'] = 'Ruby'
puts " Custom headers added: #{client.headers}"
# 5. Define a simple DTO
puts "\n5. Defining a simple DTO..."
class HelloRequest
attr_accessor :name
def initialize(name:)
@name = name
end
def to_hash
{ 'name' => @name }
end
end
class HelloResponse
attr_accessor :result
def initialize(hash = {})
@result = hash['result'] || hash[:result]
end
end
puts " DTOs defined: HelloRequest, HelloResponse"
# 6. Make a request (would work with a real API)
puts "\n6. Making a request..."
puts " (This would send a POST request to /hello)"
request = HelloRequest.new(name: 'World')
puts " Request data: #{request.to_hash}"
# With a real ServiceStack API, you would do:
# begin
# response = client.post(request, HelloResponse, path: '/hello')
# puts " Response: #{response.result}"
# rescue ServiceStack::WebServiceException => e
# puts " Error: #{e.error_message}"
# end
# 7. Using Hash-based requests
puts "\n7. Using Hash-based requests..."
puts " You can also use plain hashes:"
hash_request = { name: 'Ruby' }
puts " Request: #{hash_request}"
# With a real API:
# response = client.post(hash_request, path: '/hello')
# puts " Response: #{response}"
puts "\n" + "=" * 50
puts "Basic usage example complete!"
puts "\nFor a real-world usage, replace the base URL with your"
puts "ServiceStack API endpoint and uncomment the request code."