GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — GitHub - umarF/ServerRequest: A simple file that deals with your request management and uses protocols to return server responses.

BranchesTags
Open more actions menu

Latest commit

 

History

6 Commits
6 Commits

Folders and files

NameName
Last commit message
Last commit date
README.md
README.md
 
 
ServerRequest.swift
ServerRequest.swift
 
 

Repository files navigation

ServerRequest

How to use ?

Add ServerRequest.swift to your project

Drag and drop or copy the file and add it to your project.

Setup ServerRequst.swift:

ENTER YOUR SERVER URL:
//ServerRequest.swift
var BASE_URL = ""    
SETUP YOUR ENUM NAMES:
//ServerRequest.swift

enum API_TYPES_NAME: Int {
        
        //EXAMPLE
        case loginAPI
        case logoutAPI
        case initialPayloadAPI
 }
 
SETUP YOUR REQUEST TIMEOUT INTERVAL:
var TIME_OUT = 120.0
ENTER YOUR API ENDPOINT URL:
Inside your generateUrlRequestWithURLPartParameters function in ServerRequest.swift , add your URL strings.
//ServerRequest.swift

//DEPENDING ON THE CALL, ADD APPROPRIATE URL IN CASES
   
    switch apiType! {
            
    	case .initialPayloadAPI:
   		//replace with your URL string
    	urlStr = "/version/app?&access_token=\(ACCESS_TOKEN)&refresh_token=\(REFRESH_TOKEN)"
    	request.httpMethod = "GET"
    
    	case .loginAPI:
    	//replace with your URL string
    	//date param derived from urlPartParam
    	urlStr = "/login?&date=\(urlPartParam?["asOfDate"] as? String ?? "")" 
    	request.httpMethod = "POST"
    	request.setValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type")
            
    	case .logoutAPI:
    	//replace with your URL string
    	urlStr = "/logout?domain=self&access_token=\(ACCESS_TOKEN)&refresh_token=\(REFRESH_TOKEN)"
    	request.httpMethod = "GET"

    }
    

Initialize the request :

//ViewController.swift

let serverObj = ServerRequest()
serverObj.apiType = ServerRequest.API_TYPES_NAME.loginAPI
serverObj.delegate = self
serverObj.generateUrlRequestWithURLPartParameters(["email":"[email protected]"], postParam: nil)

Conform the class to ServerRequestDelegate using an extention:

//ViewController.swift

extension ViewController:ServerRequestDelegate{
    
    func requestFinishedWithResult(_ responseDictionary: [String : Any], apiCallType: ServerRequest.API_TYPES_NAME, response: URLResponse) {
      //example
      if apiCallType == ServerRequest.API_TYPES_NAME.loginAPI {
          // your parsing
      }else if apiCallType == ServerRequest.API_TYPES_NAME.logoutAPI {
          // your parsing
      }
      
    }
    
    func requestFinishedWithResultArray(_ responseArray: Array<Any>, apiCallType: ServerRequest.API_TYPES_NAME, response: URLResponse) {
      //example
      if apiCallType == ServerRequest.API_TYPES_NAME.loginAPI {
          // your parsing
      }else if apiCallType == ServerRequest.API_TYPES_NAME.logoutAPI {
          // your parsing
      }
    }
    
    func requestFinishedWithResponse(_ response: URLResponse, message: String, apiCallType: ServerRequest.API_TYPES_NAME) {
      //example
      if apiCallType == ServerRequest.API_TYPES_NAME.loginAPI {
          // your parsing
      }else if apiCallType == ServerRequest.API_TYPES_NAME.logoutAPI {
          // your parsing
      }
    }
    
    func requestFailedWithError(_ error: Error, apiCallType: ServerRequest.API_TYPES_NAME, response: URLResponse?) {
      //example
      if apiCallType == ServerRequest.API_TYPES_NAME.loginAPI {
          // your parsing
      }else if apiCallType == ServerRequest.API_TYPES_NAME.logoutAPI {
          // your parsing
      }
    }
    
}

Differentiate between the response using the 'apiCallType' param:

func requestFailedWithError(_ error: Error, apiCallType: ServerRequest.API_TYPES_NAME, response: URLResponse?) {
      //example
      if apiCallType == ServerRequest.API_TYPES_NAME.loginAPI {
          // your parsing
      }else if apiCallType == ServerRequest.API_TYPES_NAME.logoutAPI {
          // your parsing
      }
}

About

A simple file that deals with your request management and uses protocols to return server responses.

Topics

ios-swiftswifturlrequesturlsession

Resources

Readme
Activity

Stars

1 star

Watchers

1 watching

Forks

0 forks
Report repository

Releases

Packages

Contributors

Languages

You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.