Class: PairProgrammer::Api::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/pairprogrammer/api/client.rb

Instance Method Summary collapse

Constructor Details

#initializeClient

Returns a new instance of Client.



9
10
11
12
13
14
15
# File 'lib/pairprogrammer/api/client.rb', line 9

def initialize
    @api_key = PairProgrammer::Configuration.api_key
    # TODO api key is not always required, ie checking version
    raise "Missing api key" if @api_key.nil?

    @domain = PairProgrammer::Configuration.development? ? 'http://localhost:8000' : 'https://www.pearprogrammer.dev'
end

Instance Method Details

#get(endpoint, query_params = {}) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/pairprogrammer/api/client.rb', line 17

def get(endpoint, query_params={})
    encoded_params = URI.encode_www_form(query_params)
    uri = URI.parse(@domain + endpoint + "?" + encoded_params)

    # Create a new instance of Net::HTTP for the specified URI
    http = Net::HTTP.new(uri.host, uri.port)

    # Use SSL if the URI scheme is HTTPS
    http.use_ssl = true if uri.scheme == 'https'

    # Set the open_timeout and read_timeout values (in seconds)
    # http.open_timeout = 5 # Timeout for opening the connection
    http.read_timeout = 605 # matches openai timeout

    # Create a GET request
    request = Net::HTTP::Get.new(uri)
    request['Pairprogrammer-Api-Key'] = @api_key

    # Make the HTTP GET request and handle the response
    response = http.request(request)
    handle_response(response)
end

#post(endpoint, body) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/pairprogrammer/api/client.rb', line 40

def post(endpoint, body)
    uri = URI.parse(@domain + endpoint)
    request = Net::HTTP::Post.new(uri)
    request.body = body.to_json
    request['Content-Type'] = 'application/json'
    request['Pairprogrammer-Api-Key'] = @api_key

    # Create a new instance of Net::HTTP for the specified URI
    http = Net::HTTP.new(uri.host, uri.port)

    # Use SSL if the URI scheme is HTTPS
    http.use_ssl = true if uri.scheme == 'https'

    # Set the open_timeout and read_timeout values (in seconds)
    # http.open_timeout = 5 # Timeout for opening the connection
    http.read_timeout = 300 # Timeout for reading the response

    # Make the HTTP POST request and handle the response
    response = http.request(request)
    handle_response(response)
end